package daterange import ( "git.ichub.com/general/webcli120/goconfig/base/basedto" "github.com/gogf/gf/v2/os/gtime" "icd/utils" "time" ) type DateRange struct { basedto.BaseEntity Start time.Time //2021-01-01 00:00:00 End time.Time //2021-01-01 23:59:59 End2 time.Time ///2021-01-02 00:00:00 第二天00:00:00 ZoneShanghai *time.Location `json:"-"` ZoneUtc *time.Location } func NewDateRange() *DateRange { var dr = &DateRange{} dr.InitProxy(dr) dr.ZoneShanghai, _ = time.LoadLocation("Asia/Shanghai") dr.ZoneUtc = time.UTC return dr } func (self *DateRange) Yesterday() DateRange { return DateRange{} } func (self *DateRange) DayBefore() DateRange { return DateRange{} } func (self *DateRange) ToDateRangeStr() *DateRangeStr { var strday = FindBeanDateRangeStr() strday.Start = gtime.NewFromTime(self.Start).Layout("2006-01-02") strday.End = gtime.NewFromTime(self.End).Layout("2006-01-02") return strday } func (self *DateRange) ToId(memberId int64) string { return self.Start.Format("2006-01-02") + "|" + utils.FormatInt(memberId) + "|0|0" } func (self *DateRange) Clone() *DateRange { var c = *self return &c } func (self DateRange) BeforeDay() *DateRange { var c = self.Clone() c.Start = c.Start.Add(-48 * time.Hour) c.End = c.End.Add(-48 * time.Hour) return c } func (self DateRange) YesterDay() *DateRange { var c = self.Clone() c.Start = c.Start.Add(-24 * time.Hour) c.End = c.End.Add(-24 * time.Hour) return c } func (self DateRange) Tommorow() *DateRange { var c = self.Clone() c.Start = c.Start.Add(24 * time.Hour) c.End = c.End.Add(24 * time.Hour) return c } func (self DateRange) ToUtcTime() *DateRange { var c = self.Clone() c.Start = c.Start.In(self.ZoneUtc) c.End = c.End.In(self.ZoneUtc) return c } func (self DateRange) ToShanghaiTime() *DateRange { var c = self.Clone() c.Start = c.Start.In(self.ZoneShanghai) c.End = c.End.In(self.ZoneShanghai) return c } func (self DateRange) ToShanghaiDateOnly() *DateRange { var c = self.ToShanghaiTime() c.Start = time.Date(c.Start.Year(), c.Start.Month(), c.Start.Day(), 0, 0, 0, 0, time.UTC) c.End = time.Date(c.End.Year(), c.End.Month(), c.End.Day(), 23, 59, 59, 0, time.UTC) c.End2 = c.Start.Add(time.Hour * 24) return c } func (self DateRange) ToUtcDateOnly() *DateRange { var c = self.ToUtcTime() c.Start = time.Date(c.Start.Year(), c.Start.Month(), c.Start.Day(), 0, 0, 0, 0, time.UTC) c.End = time.Date(c.End.Year(), c.End.Month(), c.End.Day(), 23, 59, 59, 0, time.UTC) c.End2 = c.Start.Add(time.Hour * 24) return c } // shanghai datestr used by es func (self DateRange) ToShanghaiDateStr() *DateRangeStr { var c = self.ToShanghaiDateOnly() var dayStr = NewDateRangeStr() dayStr.Start = gtime.NewFromTime(c.Start).Layout("2006-01-02") + "T00:00:00+08:00" dayStr.End = gtime.NewFromTime(c.End).Layout("2006-01-02") + "T23:59:59+08:00" var end2 = c.Start.Add(24 * time.Hour) dayStr.End2 = gtime.NewFromTime(end2).Layout("2006-01-02") + "T23:59:59+08:00" return dayStr }标签:End,Start,对象,self,区间,time,go,return,DateRange From: https://blog.csdn.net/leijmdas/article/details/144323490