Commit ff845040c172c5856d6cfbfe943cf132985e7209
1 parent
3173b06a41
Exists in
master
unix/date nullables
Showing
1 changed file
with
27 additions
and
1 deletions
Show diff stats
format.go
1 | package webutility | 1 | package webutility |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | "time" | 5 | "time" |
6 | ) | 6 | ) |
7 | 7 | ||
8 | // UnixToDate converts given Unix time to local time in format and returns result: | 8 | // UnixToDate converts given Unix time to local time in format and returns result: |
9 | // YYYY-MM-DD hh:mm:ss +zzzz UTC | 9 | // YYYY-MM-DD hh:mm:ss +zzzz UTC |
10 | func UnixToDate(unix int64) time.Time { | 10 | func UnixToDate(unix int64) time.Time { |
11 | return time.Unix(unix, 0) | 11 | return time.Unix(unix, 0) |
12 | } | 12 | } |
13 | 13 | ||
14 | // DateToUnix converts given date in Unix timestamp. | 14 | // DateToUnix converts given date in Unix timestamp. |
15 | func DateToUnix(date interface{}) int64 { | 15 | func DateToUnix(date interface{}) int64 { |
16 | if date != nil { | 16 | if date != nil { |
17 | t, ok := date.(time.Time) | 17 | t, ok := date.(time.Time) |
18 | if !ok { | 18 | if !ok { |
19 | return 0 | 19 | return 0 |
20 | } | 20 | } |
21 | return t.Unix() | 21 | return t.Unix() |
22 | 22 | ||
23 | } | 23 | } |
24 | return 0 | 24 | return 0 |
25 | } | 25 | } |
26 | 26 | ||
27 | // UnixPtrToDate converts given Unix time to local time in format and returns result: | ||
28 | // YYYY-MM-DD hh:mm:ss +zzzz UTC | ||
29 | func UnixPtrToDatePtr(unix *int64) *time.Time { | ||
30 | var t time.Time | ||
31 | if unix == nil { | ||
32 | return nil | ||
33 | } | ||
34 | t = time.Unix(*unix, 0) | ||
35 | return &t | ||
36 | } | ||
37 | |||
38 | // DateToUnix converts given date in Unix timestamp. | ||
39 | func DatePtrToUnixPtr(date interface{}) *int64 { | ||
40 | var unix int64 | ||
41 | |||
42 | if date != nil { | ||
43 | t, ok := date.(time.Time) | ||
44 | if !ok { | ||
45 | return nil | ||
46 | } | ||
47 | unix = t.Unix() | ||
48 | return &unix | ||
49 | |||
50 | } | ||
51 | return nil | ||
52 | } | ||
53 | |||
27 | // EqualQuotes encapsulates given string in SQL 'equal' statement and returns result. | 54 | // EqualQuotes encapsulates given string in SQL 'equal' statement and returns result. |
28 | // Example: "hello" -> " = 'hello'" | 55 | // Example: "hello" -> " = 'hello'" |
29 | func EqualQuotes(stmt string) string { | 56 | func EqualQuotes(stmt string) string { |
30 | if stmt != "" { | 57 | if stmt != "" { |
31 | stmt = fmt.Sprintf(" = '%s'", stmt) | 58 | stmt = fmt.Sprintf(" = '%s'", stmt) |
32 | } | 59 | } |
33 | return stmt | 60 | return stmt |
34 | } | 61 | } |
35 | 62 | ||
36 | // EqualString ... | 63 | // EqualString ... |
37 | func EqualString(stmt string) string { | 64 | func EqualString(stmt string) string { |
38 | if stmt != "" { | 65 | if stmt != "" { |
39 | stmt = fmt.Sprintf(" = %s", stmt) | 66 | stmt = fmt.Sprintf(" = %s", stmt) |
40 | } | 67 | } |
41 | return stmt | 68 | return stmt |
42 | } | 69 | } |
43 | 70 | ||
44 | // LikeQuotes encapsulates given string in SQL 'like' statement and returns result. | 71 | // LikeQuotes encapsulates given string in SQL 'like' statement and returns result. |
45 | // Example: "hello" -> " LIKE UPPER('%hello%')" | 72 | // Example: "hello" -> " LIKE UPPER('%hello%')" |
46 | func LikeQuotes(stmt string) string { | 73 | func LikeQuotes(stmt string) string { |
47 | if stmt != "" { | 74 | if stmt != "" { |
48 | stmt = fmt.Sprintf(" LIKE UPPER('%s%s%s')", "%", stmt, "%") | 75 | stmt = fmt.Sprintf(" LIKE UPPER('%s%s%s')", "%", stmt, "%") |
49 | } | 76 | } |
50 | return stmt | 77 | return stmt |
51 | } | 78 | } |
52 |