其他分享
首页 > 其他分享> > GO Utils

GO Utils

作者:互联网

GO 常用工具函数

// 求两个时间戳(单位 秒)之间的自然天数
func diffNatureDays(t1, t2 int64) int64 {

	var (
		diffDays     int64
		SecondsOfDay int64 = 60 * 60 * 24
	)

	if t1 == t2 {
		return 0
	}

	if t1 > t2 {
		t1, t2 = t2, t1
	}

	secDiff := t2 - t1
	if secDiff > SecondsOfDay {
		tmpDays := secDiff / SecondsOfDay
		t1 += tmpDays * SecondsOfDay
		diffDays += tmpDays
	}

	st := time.Unix(t1, 0)
	et := time.Unix(t2, 0)
	dateFormatTpl := "20060102"
	if st.Format(dateFormatTpl) != et.Format(dateFormatTpl) {
		diffDays += 1
	}

	return diffDays
}

标签:diffDays,Utils,t2,t1,SecondsOfDay,int64,GO,dateFormatTpl
来源: https://www.cnblogs.com/dibtp/p/16433980.html