Commit befe8fcc1dc1e4b13d973c00813955612d073b20
Exists in
master
Merge branch 'master' of http://git.to-net.rs/marko.tikvic/webutility
Showing
2 changed files
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 |
int_util.go
1 | package webutility | 1 | package webutility |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | "strconv" | 5 | "strconv" |
6 | ) | 6 | ) |
7 | 7 | ||
8 | // ClampInt64 ... | 8 | // ClampInt64 ... |
9 | func ClampInt64(v, min, max int64) int64 { | 9 | func ClampInt64(v, min, max int64) int64 { |
10 | if v < min { | 10 | if v < min { |
11 | return min | 11 | return min |
12 | } else if v > max { | 12 | } else if v > max { |
13 | return max | 13 | return max |
14 | } | 14 | } |
15 | 15 | ||
16 | return v | 16 | return v |
17 | } | 17 | } |
18 | 18 | ||
19 | // InRangeInt64 ... | 19 | // InRangeInt64 ... |
20 | func InRangeInt64(v, min, max int64) bool { | 20 | func InRangeInt64(v, min, max int64) bool { |
21 | return (v >= min && v <= max) | 21 | return (v >= min && v <= max) |
22 | } | 22 | } |
23 | 23 | ||
24 | // StringToInt64 ... | 24 | // StringToInt64 ... |
25 | func StringToInt64(s string) int64 { | 25 | func StringToInt64(s string) int64 { |
26 | i, _ := strconv.ParseInt(s, 10, 64) | 26 | i, _ := strconv.ParseInt(s, 10, 64) |
27 | return i | 27 | return i |
28 | } | 28 | } |
29 | 29 | ||
30 | // StringToFloat64 ... | 30 | // StringToFloat64 ... |
31 | func StringToFloat64(s string) float64 { | 31 | func StringToFloat64(s string) float64 { |
32 | f, _ := strconv.ParseFloat(s, 64) | 32 | f, _ := strconv.ParseFloat(s, 64) |
33 | return f | 33 | return f |
34 | } | 34 | } |
35 | 35 | ||
36 | // Int64ToString ... | 36 | // Int64ToString ... |
37 | func Int64ToString(i int64) string { | 37 | func Int64ToString(i int64) string { |
38 | return fmt.Sprintf("%d", i) | 38 | return fmt.Sprintf("%d", i) |
39 | } | 39 | } |
40 | 40 | ||
41 | // BoolToInt64 ... | 41 | // BoolToInt64 ... |
42 | func BoolToInt64(b bool) int64 { | 42 | func BoolToInt64(b bool) int64 { |
43 | if b { | 43 | if b { |
44 | return 1 | 44 | return 1 |
45 | } | 45 | } |
46 | return 0 | 46 | return 0 |
47 | } | 47 | } |
48 | 48 | ||
49 | // Int64ToBool ... | 49 | // Int64ToBool ... |
50 | func Int64ToBool(i int64) bool { | 50 | func Int64ToBool(i int64) bool { |
51 | return i != 0 | 51 | return i != 0 |
52 | } | 52 | } |
53 | |||
54 | func StringToValidInt64(s string) (int64, bool) { | ||
55 | i, err := strconv.ParseInt(s, 10, 64) | ||
56 | if err != nil { | ||
57 | return i, false | ||
58 | } | ||
59 | return i, true | ||
60 | } | ||
53 | 61 |