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