Blame view

format.go 1.53 KB
ea858b8a7   Marko Tikvić   refactoring
1
  package webutility
514fa9dd6   Marko Tikvić   added formating u...
2
3
  
  import (
4b4ea384f   Marko Tikvić   hmm
4
  	"fmt"
a205e8f40   Marko Tikvić   changes
5
  	"time"
514fa9dd6   Marko Tikvić   added formating u...
6
  )
e1fbb41f9   Marko Tikvić   added comments
7
8
  // UnixToDate converts given Unix time to local time in format and returns result:
  // YYYY-MM-DD hh:mm:ss +zzzz UTC
33fd58161   markotikvic   minor changes, sh...
9
10
  func UnixToDate(unix int64) time.Time {
  	return time.Unix(unix, 0)
514fa9dd6   Marko Tikvić   added formating u...
11
  }
e1fbb41f9   Marko Tikvić   added comments
12
  // DateToUnix converts given date in Unix timestamp.
33fd58161   markotikvic   minor changes, sh...
13
14
  func DateToUnix(date interface{}) int64 {
  	if date != nil {
a205e8f40   Marko Tikvić   changes
15
16
17
18
  		t, ok := date.(time.Time)
  		if !ok {
  			return 0
  		}
514fa9dd6   Marko Tikvić   added formating u...
19
20
21
22
23
  		return t.Unix()
  
  	}
  	return 0
  }
ff845040c   Marko Tikvić   unix/date nullables
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
  // 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
  }
e1fbb41f9   Marko Tikvić   added comments
50
51
  // EqualQuotes encapsulates given string in SQL 'equal' statement and returns result.
  // Example: "hello" -> " = 'hello'"
33fd58161   markotikvic   minor changes, sh...
52
53
  func EqualQuotes(stmt string) string {
  	if stmt != "" {
4b4ea384f   Marko Tikvić   hmm
54
  		stmt = fmt.Sprintf(" = '%s'", stmt)
514fa9dd6   Marko Tikvić   added formating u...
55
  	}
33fd58161   markotikvic   minor changes, sh...
56
  	return stmt
514fa9dd6   Marko Tikvić   added formating u...
57
  }
707782344   Marko Tikvić   lint; vet
58
  // EqualString ...
da1d1d418   Marko Tikvić   SQL EqualString f...
59
60
61
62
63
64
  func EqualString(stmt string) string {
  	if stmt != "" {
  		stmt = fmt.Sprintf(" = %s", stmt)
  	}
  	return stmt
  }
e1fbb41f9   Marko Tikvić   added comments
65
66
  // LikeQuotes encapsulates given string in SQL 'like' statement and returns result.
  // Example: "hello" -> " LIKE UPPER('%hello%')"
33fd58161   markotikvic   minor changes, sh...
67
68
  func LikeQuotes(stmt string) string {
  	if stmt != "" {
e77b75ec6   Marko Tikvić   proper LIKE forma...
69
  		stmt = fmt.Sprintf(" LIKE UPPER('%s%s%s')", "%", stmt, "%")
514fa9dd6   Marko Tikvić   added formating u...
70
  	}
33fd58161   markotikvic   minor changes, sh...
71
  	return stmt
514fa9dd6   Marko Tikvić   added formating u...
72
  }