time.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package util
  4. import (
  5. "time"
  6. )
  7. // IsToday 是否为今天, ts:秒时间戳
  8. func IsToday(ts int64) bool {
  9. that := time.Unix(ts, 0)
  10. now := time.Now()
  11. if that.Year() == now.Year() && that.Month() == now.Month() && that.Day() == now.Day() {
  12. return true
  13. }
  14. return false
  15. }
  16. // DayInternal 计算两个时间间隔的自然天数
  17. // 输入:begin、end 为开始与截止的时间戳
  18. // 返回:自然天数,2001-01-01 23:23:59 与 2001-01-02 00:00:00 间隔为1天
  19. func DayInternal(begin, end int64) int {
  20. if end <= begin {
  21. return 0
  22. }
  23. bt := time.Unix(begin, 0)
  24. et := time.Unix(end, 0)
  25. bt = time.Date(bt.Year(), bt.Month(), bt.Day(), 0, 0, 0, 0, time.Local)
  26. et = time.Date(et.Year(), et.Month(), et.Day(), 0, 0, 0, 0, time.Local)
  27. return int(et.Sub(bt).Hours() / 24)
  28. }
  29. // DayBeginInfo 一天的开始时间信息
  30. // 输入:ts 为输入的时间信息
  31. // 返回:978278410 => 978278400, 2001-01-01
  32. func DayBeginTimestamp(ts int64) (int64, string) {
  33. if ts < 0 {
  34. return 0, ""
  35. }
  36. t := time.Unix(ts, 0)
  37. t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
  38. return t.Unix(), t.Format("2006-01-02")
  39. }