| package xtime
|
|
|
| import "time"
|
|
|
| func UTCNow() time.Time {
|
| return time.Now().UTC()
|
| }
|
|
|
| var utcNowFunc = UTCNow
|
|
|
|
|
|
|
| func setUTCNowFunc(f func() time.Time) {
|
| utcNowFunc = f
|
| }
|
|
|
|
|
|
|
| func resetUTCNowFunc() {
|
| utcNowFunc = UTCNow
|
| }
|
|
|
|
|
|
|
| type Period struct {
|
| Start time.Time
|
| End time.Time
|
| }
|
|
|
|
|
| type CalendarPeriods struct {
|
|
|
| Today Period
|
|
|
|
|
| ThisWeek Period
|
|
|
|
|
| LastWeek Period
|
|
|
|
|
| ThisMonth Period
|
| }
|
|
|
|
|
|
|
|
|
| func GetCalendarPeriods(loc *time.Location) CalendarPeriods {
|
| nowLocal := utcNowFunc().In(loc)
|
|
|
|
|
| todayStart := time.Date(nowLocal.Year(), nowLocal.Month(), nowLocal.Day(), 0, 0, 0, 0, loc)
|
| todayEnd := todayStart.AddDate(0, 0, 1)
|
|
|
|
|
| weekday := int(nowLocal.Weekday())
|
| if weekday == 0 {
|
| weekday = 7
|
| }
|
|
|
| thisWeekStart := todayStart.AddDate(0, 0, -(weekday - 1))
|
| thisWeekEnd := thisWeekStart.AddDate(0, 0, 7)
|
|
|
|
|
| lastWeekStart := thisWeekStart.AddDate(0, 0, -7)
|
| lastWeekEnd := thisWeekStart
|
|
|
|
|
| thisMonthStart := time.Date(nowLocal.Year(), nowLocal.Month(), 1, 0, 0, 0, 0, loc)
|
| thisMonthEnd := thisMonthStart.AddDate(0, 1, 0)
|
|
|
| return CalendarPeriods{
|
| Today: Period{
|
| Start: todayStart.UTC(),
|
| End: todayEnd.UTC(),
|
| },
|
| ThisWeek: Period{
|
| Start: thisWeekStart.UTC(),
|
| End: thisWeekEnd.UTC(),
|
| },
|
| LastWeek: Period{
|
| Start: lastWeekStart.UTC(),
|
| End: lastWeekEnd.UTC(),
|
| },
|
| ThisMonth: Period{
|
| Start: thisMonthStart.UTC(),
|
| End: thisMonthEnd.UTC(),
|
| },
|
| }
|
| }
|
|
|