github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/util/time.go (about)

     1  package util
     2  
     3  import (
     4  	"math"
     5  	"net/http"
     6  	"time"
     7  )
     8  
     9  var cst *time.Location
    10  
    11  func init() {
    12  	var err error
    13  	if cst, err = time.LoadLocation("Asia/Shanghai"); err != nil {
    14  		panic(err)
    15  	}
    16  
    17  	// 默认设置为中国时区
    18  	time.Local = cst
    19  }
    20  
    21  // CSTLayoutString 格式化时间
    22  // 返回 "2006-01-02 15:04:05" 格式的时间
    23  func CSTLayoutString() string {
    24  	return time.Now().In(cst).Format(time.DateTime)
    25  }
    26  
    27  // ParseCSTInLocation 格式化时间
    28  func ParseCSTInLocation(date string) (time.Time, error) {
    29  	return time.ParseInLocation(time.DateTime, date, cst)
    30  }
    31  
    32  // CSTLayoutStringToUnix 返回 unix 时间戳
    33  // 2020-01-24 21:11:11 => 1579871471
    34  func CSTLayoutStringToUnix(cstLayoutString string) (int64, error) {
    35  	stamp, err := time.ParseInLocation(time.DateTime, cstLayoutString, cst)
    36  	if err != nil {
    37  		return 0, err
    38  	}
    39  	return stamp.Unix(), nil
    40  }
    41  
    42  // GMTLayoutString 格式化时间
    43  // 返回 "Mon, 02 Jan 2006 15:04:05 GMT" 格式的时间
    44  func GMTLayoutString() string {
    45  	return time.Now().In(cst).Format(http.TimeFormat)
    46  }
    47  
    48  // ParseGMTInLocation 格式化时间
    49  func ParseGMTInLocation(date string) (time.Time, error) {
    50  	return time.ParseInLocation(http.TimeFormat, date, cst)
    51  }
    52  
    53  // BeginOfDay 当天 0 点
    54  func BeginOfDay(t time.Time) time.Time {
    55  	y, m, d := t.Date()
    56  	return time.Date(y, m, d, 0, 0, 0, 0, t.Location())
    57  }
    58  
    59  // EndOfDay 当天最后时刻
    60  func EndOfDay(t time.Time) time.Time {
    61  	return BeginOfTomorrow(t).Add(-time.Nanosecond)
    62  }
    63  
    64  // BeginOfYesterday 昨天 0 点
    65  func BeginOfYesterday(t time.Time) time.Time {
    66  	return BeginOfDay(t.AddDate(0, 0, -1))
    67  }
    68  
    69  // EndOfYesterday 昨天最后时刻
    70  func EndOfYesterday(t time.Time) time.Time {
    71  	return EndOfDay(t.AddDate(0, 0, -1))
    72  }
    73  
    74  // BeginOfTomorrow 明天 0 点
    75  func BeginOfTomorrow(t time.Time) time.Time {
    76  	return BeginOfDay(t.AddDate(0, 0, 1))
    77  }
    78  
    79  // EndOfTomorrow 明天 0 点
    80  func EndOfTomorrow(t time.Time) time.Time {
    81  	return EndOfDay(t.AddDate(0, 0, 1))
    82  }
    83  
    84  // BeginOfSecond 0 毫秒
    85  func BeginOfSecond(t time.Time) time.Time {
    86  	y, m, d := t.Date()
    87  	return time.Date(y, m, d, t.Hour(), t.Minute(), t.Second(), 0, t.Location())
    88  }
    89  
    90  // EndOfSecond 最后一毫秒
    91  func EndOfSecond(t time.Time) time.Time {
    92  	return BeginOfSecond(t).Add(time.Second - time.Nanosecond)
    93  }
    94  
    95  // BeginOfMinute 0 秒
    96  func BeginOfMinute(t time.Time) time.Time {
    97  	y, m, d := t.Date()
    98  	return time.Date(y, m, d, t.Hour(), t.Minute(), 0, 0, t.Location())
    99  }
   100  
   101  // EndOfMinute 最后一秒
   102  func EndOfMinute(t time.Time) time.Time {
   103  	return BeginOfMinute(t).Add(time.Minute - time.Nanosecond)
   104  }
   105  
   106  // BeginOfHour 0 分
   107  func BeginOfHour(t time.Time) time.Time {
   108  	y, m, d := t.Date()
   109  	return time.Date(y, m, d, t.Hour(), 0, 0, 0, t.Location())
   110  }
   111  
   112  // EndOfHour 最后一分
   113  func EndOfHour(t time.Time) time.Time {
   114  	return BeginOfHour(t).Add(time.Hour - time.Nanosecond)
   115  }
   116  
   117  // BeginOfWeek 本周一 0 点
   118  func BeginOfWeek(t time.Time) time.Time {
   119  	offset := int(time.Monday - t.Weekday())
   120  	if offset > 0 {
   121  		offset = -6
   122  	}
   123  	return BeginOfDay(t).AddDate(0, 0, offset)
   124  }
   125  
   126  // EndOfWeek 本周末最后一刻
   127  func EndOfWeek(t time.Time) time.Time {
   128  	return BeginOfNextWeek(t).Add(-time.Nanosecond)
   129  }
   130  
   131  // BeginOfLastWeek 上周一 0 点
   132  func BeginOfLastWeek(t time.Time) time.Time {
   133  	return BeginOfWeek(t.AddDate(0, 0, -7))
   134  }
   135  
   136  // EndOfLastWeek 上周一最后一刻
   137  func EndOfLastWeek(t time.Time) time.Time {
   138  	return EndOfWeek(t.AddDate(0, 0, -7))
   139  }
   140  
   141  // BeginOfNextWeek 下周一 0 点
   142  func BeginOfNextWeek(t time.Time) time.Time {
   143  	return BeginOfWeek(t.AddDate(0, 0, 7))
   144  }
   145  
   146  // EndOfNextWeek 下周一最后一刻
   147  func EndOfNextWeek(t time.Time) time.Time {
   148  	return EndOfWeek(t.AddDate(0, 0, 7))
   149  }
   150  
   151  // BeginOfMonth 当月第一天 0 点
   152  func BeginOfMonth(t time.Time) time.Time {
   153  	y, m, _ := t.Date()
   154  	return time.Date(y, m, 1, 0, 0, 0, 0, t.Location())
   155  }
   156  
   157  // EndOfMonth 当月最后一刻
   158  func EndOfMonth(t time.Time) time.Time {
   159  	return BeginOfNextMonth(t).Add(-time.Nanosecond)
   160  }
   161  
   162  // BeginOfLastMonth 上月第一天 0 点
   163  func BeginOfLastMonth(t time.Time) time.Time {
   164  	return BeginOfMonth(BeginOfMonth(t).AddDate(0, 0, -1))
   165  }
   166  
   167  // EndOfLastMonth 上月最后一刻
   168  func EndOfLastMonth(t time.Time) time.Time {
   169  	return BeginOfMonth(t).Add(-time.Nanosecond)
   170  }
   171  
   172  // BeginOfNextMonth 下月第一天 0 点
   173  func BeginOfNextMonth(t time.Time) time.Time {
   174  	return BeginOfMonth(BeginOfMonth(t).AddDate(0, 0, 31))
   175  }
   176  
   177  // EndOfNextMonth 下月最后一刻
   178  func EndOfNextMonth(t time.Time) time.Time {
   179  	return BeginOfMonth(BeginOfMonth(t).AddDate(0, 0, 62)).Add(-time.Nanosecond)
   180  }
   181  
   182  // GetMonthDays 当月天数
   183  func GetMonthDays(t time.Time) int {
   184  	return int(BeginOfNextMonth(t).Sub(BeginOfMonth(t)).Hours() / 24)
   185  }
   186  
   187  // BeginOfYear 本年第一天 0 点
   188  func BeginOfYear(t time.Time) time.Time {
   189  	return time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location())
   190  }
   191  
   192  // EndOfYear 本年最后一刻
   193  func EndOfYear(t time.Time) time.Time {
   194  	return BeginOfYear(t).AddDate(1, 0, 0).Add(-time.Nanosecond)
   195  }
   196  
   197  // IsLeapYear 判断是否为闰年
   198  func IsLeapYear(year int) bool {
   199  	return year%4 == 0 && (year%100 != 0 || year%400 == 0)
   200  }
   201  
   202  // DaysInYear 返回年份天数
   203  func DaysInYear(year int) int {
   204  	if IsLeapYear(year) {
   205  		return 366
   206  	}
   207  	return 365
   208  }
   209  
   210  // SubInLocation 计算时间差
   211  func SubInLocation(ts time.Time) float64 {
   212  	return math.Abs(time.Now().In(cst).Sub(ts).Seconds())
   213  }