This repository has been archived by the owner on Sep 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
date.go
51 lines (41 loc) · 1.6 KB
/
date.go
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
package main
// This code is copy-paste from https://github.com/lomik/carbon-clickhouse/blob/f659eb147017213f22de8ba12657abc0a6e3a07d/receiver/days.go
// Copyright (c) 2016 Roman Lomonosov
// License: MIT (https://github.com/lomik/carbon-clickhouse/blob/f659eb147017213f22de8ba12657abc0a6e3a07d/LICENSE)
import "time"
// Helper for fast calculate days count from 1970-01-01 in local timezone
// Not thread-safe!
type DaysFrom1970 struct {
todayStartTimestamp uint32
todayEndTimestamp uint32
todayDays uint16
}
func (dd *DaysFrom1970) fromTimestamp(timestamp uint32) uint16 {
t := time.Unix(int64(timestamp), 0)
return uint16(time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.UTC).Unix() / 86400)
}
func (dd *DaysFrom1970) Timestamp(timestamp uint32) uint16 {
if timestamp >= dd.todayStartTimestamp && timestamp <= dd.todayEndTimestamp {
return dd.todayDays
}
return dd.fromTimestamp(timestamp)
}
func (dd *DaysFrom1970) TimestampWithNow(timestamp uint32, now uint32) uint16 {
if timestamp < dd.todayStartTimestamp {
return dd.fromTimestamp(timestamp)
}
// timestamp >= pp.todayStartTimestamp
if timestamp <= dd.todayEndTimestamp {
return dd.todayDays
}
// timestamp > dd.todayEndTimestamp
// check now date
if now > dd.todayEndTimestamp {
// update "today" required
d := time.Unix(int64(now), 0)
dd.todayStartTimestamp = uint32(time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, time.Local).Unix())
dd.todayEndTimestamp = uint32(time.Date(d.Year(), d.Month(), d.Day(), 23, 59, 59, 0, time.Local).Unix())
dd.todayDays = dd.fromTimestamp(now)
}
return dd.Timestamp(timestamp)
}