2019-05-19 21:06:07 +03:00
|
|
|
package utils
|
|
|
|
|
2020-09-06 13:15:46 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
2022-03-25 14:48:56 +03:00
|
|
|
"github.com/duke-git/lancet/v2/datetime"
|
2020-09-06 13:15:46 +03:00
|
|
|
"time"
|
|
|
|
)
|
2019-05-19 21:06:07 +03:00
|
|
|
|
2022-03-25 14:48:56 +03:00
|
|
|
func BeginOfToday(tz *time.Location) time.Time {
|
|
|
|
return datetime.BeginOfDay(time.Now().In(tz))
|
2019-05-19 21:06:07 +03:00
|
|
|
}
|
|
|
|
|
2022-03-25 14:48:56 +03:00
|
|
|
func BeginOfThisWeek(tz *time.Location) time.Time {
|
|
|
|
return datetime.BeginOfWeek(time.Now().In(tz))
|
2019-05-19 21:06:07 +03:00
|
|
|
}
|
|
|
|
|
2022-03-25 14:48:56 +03:00
|
|
|
func BeginOfThisMonth(tz *time.Location) time.Time {
|
|
|
|
return datetime.BeginOfMonth(time.Now().In(tz))
|
2019-05-19 21:06:07 +03:00
|
|
|
}
|
|
|
|
|
2022-03-25 14:48:56 +03:00
|
|
|
func BeginOfThisYear(tz *time.Location) time.Time {
|
|
|
|
return datetime.BeginOfYear(time.Now().In(tz))
|
2021-06-26 13:42:51 +03:00
|
|
|
}
|
|
|
|
|
2021-04-16 12:53:37 +03:00
|
|
|
// CeilDate rounds date up to the start of next day if date is not already a start (00:00:00)
|
|
|
|
func CeilDate(date time.Time) time.Time {
|
2022-03-25 14:48:56 +03:00
|
|
|
floored := datetime.BeginOfDay(date)
|
2021-04-16 12:53:37 +03:00
|
|
|
if floored == date {
|
|
|
|
return floored
|
|
|
|
}
|
2022-01-02 15:17:30 +03:00
|
|
|
return floored.AddDate(0, 0, 1)
|
2021-04-16 12:53:37 +03:00
|
|
|
}
|
|
|
|
|
2021-04-25 21:53:17 +03:00
|
|
|
// SplitRangeByDays creates a slice of intervals between from and to, each of which is at max of 24 hours length and has its split at midnight
|
2020-09-12 00:24:51 +03:00
|
|
|
func SplitRangeByDays(from time.Time, to time.Time) [][]time.Time {
|
|
|
|
intervals := make([][]time.Time, 0)
|
|
|
|
|
|
|
|
for t1 := from; t1.Before(to); {
|
2022-03-25 14:48:56 +03:00
|
|
|
t2 := datetime.BeginOfDay(t1).AddDate(0, 0, 1)
|
2020-09-12 00:24:51 +03:00
|
|
|
if t2.After(to) {
|
|
|
|
t2 = to
|
|
|
|
}
|
|
|
|
intervals = append(intervals, []time.Time{t1, t2})
|
|
|
|
t1 = t2
|
|
|
|
}
|
|
|
|
|
|
|
|
return intervals
|
|
|
|
}
|
|
|
|
|
2020-09-06 13:15:46 +03:00
|
|
|
func FmtWakatimeDuration(d time.Duration) string {
|
|
|
|
d = d.Round(time.Minute)
|
|
|
|
h := d / time.Hour
|
|
|
|
d -= h * time.Hour
|
|
|
|
m := d / time.Minute
|
|
|
|
return fmt.Sprintf("%d hrs %d mins", h, m)
|
|
|
|
}
|
|
|
|
|
2022-01-02 22:25:07 +03:00
|
|
|
// LocalTZOffset returns the time difference between server local time and UTC
|
|
|
|
func LocalTZOffset() time.Duration {
|
|
|
|
_, offset := time.Now().Zone()
|
|
|
|
return time.Duration(offset * int(time.Second))
|
|
|
|
}
|