format.go
1.04 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
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
}
// 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
}