// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package util import ( "time" ) // IsToday 是否为今天, ts:秒时间戳 func IsToday(ts int64) bool { that := time.Unix(ts, 0) now := time.Now() if that.Year() == now.Year() && that.Month() == now.Month() && that.Day() == now.Day() { return true } return false } // DayInternal 计算两个时间间隔的自然天数 // 输入:begin、end 为开始与截止的时间戳 // 返回:自然天数,2001-01-01 23:23:59 与 2001-01-02 00:00:00 间隔为1天 func DayInternal(begin, end int64) int { if end <= begin { return 0 } bt := time.Unix(begin, 0) et := time.Unix(end, 0) bt = time.Date(bt.Year(), bt.Month(), bt.Day(), 0, 0, 0, 0, time.Local) et = time.Date(et.Year(), et.Month(), et.Day(), 0, 0, 0, 0, time.Local) return int(et.Sub(bt).Hours() / 24) } // DayBeginInfo 一天的开始时间信息 // 输入:ts 为输入的时间信息 // 返回:978278410 => 978278400, 2001-01-01 func DayBeginTimestamp(ts int64) (int64, string) { if ts < 0 { return 0, "" } t := time.Unix(ts, 0) t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local) return t.Unix(), t.Format("2006-01-02") }