Blame view

date_util.go 2.14 KB
b80ee4b2b   Marko Tikvić   new stuff
1
2
3
4
  package webutility
  
  import (
  	"fmt"
1b51eed04   Marko Tikvić   pdf helper
5
  	"strings"
b80ee4b2b   Marko Tikvić   new stuff
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  	"time"
  )
  
  const (
  	YYYYMMDD_sl = "2006/01/02"
  	YYYYMMDD_ds = "2006-01-02"
  	YYYYMMDD_dt = "2006.01.02."
  
  	DDMMYYYY_sl = "02/01/2006"
  	DDMMYYYY_ds = "02-01-2006"
  	DDMMYYYY_dt = "02.01.2006."
  
  	YYYYMMDD_HHMMSS_sl = "2006/01/02 15:04:05"
  	YYYYMMDD_HHMMSS_ds = "2006-01-02 15:04:05"
  	YYYYMMDD_HHMMSS_dt = "2006.01.02. 15:04:05"
  
  	DDMMYYYY_HHMMSS_sl = "02/01/2006 15:04:05"
  	DDMMYYYY_HHMMSS_ds = "02-01-2006 15:04:05"
  	DDMMYYYY_HHMMSS_dt = "02.01.2006. 15:04:05"
  )
0c71c5487   Marko Tikvić   minor changes
26
  const DaySeconds = 24 * 60 * 60
1b51eed04   Marko Tikvić   pdf helper
27
28
29
30
  var (
  	regularYear = [12]int64{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  	leapYear    = [12]int64{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  )
b80ee4b2b   Marko Tikvić   new stuff
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  func Systime() int64 {
  	return time.Now().Unix()
  }
  
  func DateToEpoch(date, format string) int64 {
  	t, err := time.Parse(format, date)
  	if err != nil {
  		fmt.Println(err.Error())
  		return 0
  	}
  	return t.Unix()
  }
  
  func EpochToDate(e int64, format string) string {
  	return time.Unix(e, 0).Format(format)
  }
1b51eed04   Marko Tikvić   pdf helper
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  
  func EpochToDayMonthYear(timestamp int64) (d, m, y int64) {
  	datestring := EpochToDate(timestamp, DDMMYYYY_sl)
  	parts := strings.Split(datestring, "/")
  	d = StringToInt64(parts[0])
  	m = StringToInt64(parts[1])
  	y = StringToInt64(parts[2])
  	return d, m, y
  }
  
  func DaysInMonth(year, month int64) int64 {
  	if month < 1 || month > 12 {
  		return 0
  	}
  	if IsLeapYear(year) {
  		return leapYear[month-1]
  	}
  	return regularYear[month-1]
  }
  
  func IsLeapYear(year int64) bool {
  	return year%4 == 0 && !((year%100 == 0) && (year%400 != 0))
  }
2c237577d   Marko Tikvić   fixed int formati...
70
71
  
  // FirstDayOfNextMonthEpoch ...
0c71c5487   Marko Tikvić   minor changes
72
  func NextMonths1st(e int64) int64 {
2c237577d   Marko Tikvić   fixed int formati...
73
74
75
76
77
78
79
80
81
82
83
84
  	d, m, y := EpochToDayMonthYear(e)
  	m++
  	if m > 12 {
  		m = 1
  		y++
  	}
  	d = 1
  
  	date := fmt.Sprintf("%02d/%02d/%d", d, m, y)
  
  	return DateToEpoch(date, DDMMYYYY_sl)
  }
0c71c5487   Marko Tikvić   minor changes
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  
  func SameDate(e1, e2 int64) bool {
  	d1, m1, y1 := EpochToDayMonthYear(e1)
  	d2, m2, y2 := EpochToDayMonthYear(e2)
  
  	if d1 == d2 && m1 == m2 && y1 == y2 {
  		return true
  	}
  
  	return false
  }
  
  func ToStartOfDay(d int64) int64 {
  	rem := d % DaySeconds
  	if rem != 0 {
  		d -= rem
  	}
  	return d
  }
  
  func ParseTime(date, format string) time.Time {
  	t, err := time.Parse(format, date)
  	if err != nil {
  		fmt.Println(err.Error())
  	}
  	return t
  }