golang 时间戳封装

type STime struct {
	NowAt	int64	// 当前时间戳
	TMStart	int64	// 当前时间戳上个月的1号的0晨时间戳
	MStart	int64	// 当前时间戳所在月的1号的0晨时间戳
	Day     int64	// 几号
	MEnd	int64	// 当前时间戳所在月末
	WStart	int64	// 当前时间戳所在周周一的0晨时间戳
	Week	int64	// 星期几
	WEnd	int64	// 当前时间戳所在周下周周一的0晨时间戳
	ZUnix	int64	// 当前时间戳的凌晨	
}

func ( this *STime )Zeller( at time.Time )int{
	var y, m, c int
	if at.Month() >= 3 {
		m = int(at.Month())
		y = at.Year() % 100
		c = at.Year() / 100
        } else {
		m = int(at.Month()) + 12
		y = (at.Year() - 1) % 100
		c = (at.Year() - 1) / 100
	}
	week := y + (y / 4) + (c / 4) - 2*c + ((26 * (m + 1)) / 10) + at.Day() - 1
	if week < 0 {
		week = 7 - (-week) % 7
	} else {
               week = week % 7
        }
	if week == 0 {
		return 7
	}
	return week
}

func ( this *STime )Weeks( unix time.Time )( int64, int64 ){
	this.Week  = int64(this.Zeller( unix ))
	z_now := unix.Unix() - ( unix.Unix() + 28800 ) % 86400
	this.WStart = z_now - int64( ( this.Week - 1 ) * 86400 )
	this.WEnd   = this.WStart + 604800
	return this.WStart, this.WEnd
}

func ( this *STime )Month( unix time.Time )( int64, int64 ){
	this.Day = int64(unix.Day())
	this.MStart = time.Date( unix.Year(), unix.Month(), 1, 0, 0, 0, 0, time.Local).Unix()
	if unix.Month() != 12 {
		this.MEnd   = time.Date( unix.Year(), unix.Month() + 1, 1, 0, 0, 0, 0, time.Local).Unix()
	}else{
		this.MEnd   = time.Date( unix.Year() + 1, 1, 1, 0, 0, 0, 0, time.Local).Unix()
	}
	if unix.Month() != 1 {
		this.TMStart = time.Date( unix.Year(), unix.Month() - 1, 1, 0, 0, 0, 0, time.Local ).Unix()
	}else{
		this.TMStart = time.Date( unix.Year() - 1, 12, 1, 0, 0, 0, 0, time.Local ).Unix()
	}
	return this.MStart, this.MEnd
}

func ( this *STime )Zero( unix time.Time )int64{
	this.ZUnix = unix.Unix() - ( unix.Unix() + 28800 ) % 86400
	return this.ZUnix
}

func ( this *STime )All( at int64 ){
	unix := time.Unix( at, 0 )
	this.Weeks( unix )
	this.Month( unix )
	this.Zero( unix )
}


func ( this *STime )GetTime()(int64,int64){
	s_at  := fmt.Sprintf("%d-%02d-%02d",time.Now().Year(),time.Now().Month() ,time.Now().Day()-1 )+" 00:00:00"
	s := StringToTimeEx(s_at)
	e_at  := fmt.Sprintf("%d-%02d-%02d",time.Now().Year(),time.Now().Month() ,time.Now().Day()-1 )+" 23:59:59"
	e := StringToTimeEx(e_at)
	return s,e
}

/*
func main(){
	var val STime
	val.All( time.Now().Unix() )
	fmt.Printf( "%+v\n", val )
}
*/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值