在Go編程中,處理日期和時間是一項常見任務,涉及到精確性和靈活性。盡管Go的標準庫提供了時間包(time)用于處理時間相關操作,但在某些情況下,我們需要額外的實用函數來簡化這些任務。本文將介紹一系列實用函數,它們充當time包的包裝器,提供了更便捷的操作方式。
func StartOfMonth(date time.Time) time.Time { return time.Date(date.Year(), date.Month(), 1, 0, 0, 0, 0, date.Location())}// output:2024-01-01 00:00:00 +0800 CST
上述函數接受一個日期,返回該日期所在月份的第一天。例如,StartOfMonth(time.Now())將返回當前月份的第一天的時間戳。
func EndOfMonth(date time.Time) time.Time { firstDayOfNextMonth := StartOfMonth(date).AddDate(0, 1, 0) return firstDayOfNextMonth.Add(-time.Second)}// output:2024-01-31 23:59:59 +0800 CST
上述函數接受一個日期,返回該日期所在月份的最后一天的最后一秒。通過結合StartOfMonth函數,我們確保了準確的計算。
func StartOfDayOfWeek(date time.Time) time.Time { daysSinceSunday := int(date.Weekday()) return date.AddDate(0, 0, -daysSinceSunday+1)}// output:2024-01-15 00:19:42.869678 +0800 CST
上述函數接受一個日期,返回該日期所在周的第一天。
func EndOfDayOfWeek(date time.Time) time.Time { daysUntilSaturday := 7 - int(date.Weekday()) return date.AddDate(0, 0, daysUntilSaturday)}// output:2024-01-21 00:22:06.955558 +0800 CST
上述函數接受一個日期,返回該日期所在周的最后一天。
func StartAndEndOfWeeksOfMonth(year, month int) []struct{ Start, End time.Time } { startOfMonth := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC) weeks := make([]struct{ Start, End time.Time }, 0) for current := startOfMonth; current.Month() == time.Month(month); current = current.AddDate(0, 0, 7) { startOfWeek := StartOfDayOfWeek(current) endOfWeek := EndOfDayOfWeek(current) if endOfWeek.Month() != time.Month(month) { endOfWeek = EndOfMonth(current) } weeks = append(weeks, struct{ Start, End time.Time }{startOfWeek, endOfWeek}) } return weeks}// output:[ {2024-01-01 00:00:00 +0000 UTC 2024-01-07 00:00:00 +0000 UTC} {2024-01-08 00:00:00 +0000 UTC 2024-01-14 00:00:00 +0000 UTC} {2024-01-15 00:00:00 +0000 UTC 2024-01-21 00:00:00 +0000 UTC} {2024-01-22 00:00:00 +0000 UTC 2024-01-28 00:00:00 +0000 UTC} {2024-01-29 00:00:00 +0000 UTC 2024-01-31 23:59:59 +0000 UTC}]
上述函數接受年份和月份,返回一個包含給定月份中每周的開始日和結束日的切片。通過調用前述的獲取每周開始日和結束日的函數,我們得到了全面的每周視圖。
func WeekNumberInMonth(date time.Time) int { startOfMonth := StartOfMonth(date) _, week := date.ISOWeek() _, startWeek := startOfMonth.ISOWeek() return week - startWeek + 1}// output:3
上述函數接受一個日期,返回該日期所在月份的相對周數。通過利用ISO周數的概念,我們實現了簡便的計算。
func StartOfYear(date time.Time) time.Time { return time.Date(date.Year(), time.January, 1, 0, 0, 0, 0, date.Location())}// output:2024-01-01 00:00:00 +0800 CST
上述函數接受一個日期,返回該日期所在年份的第一天。
func EndOfYear(date time.Time) time.Time { startOfNextYear := StartOfYear(date).AddDate(1, 0, 0) return startOfNextYear.Add(-time.Second)}// output:2024-12-31 23:59:59 +0800 CST
上述函數接受一個日期,返回該日期所在年份的最后一天的最后一秒。
func StartOfQuarter(date time.Time) time.Time { // you can directly use 0, 1, 2, 3 quarter quarter := (int(date.Month()) - 1) / 3 startMonth := time.Month(quarter*3 + 1) return time.Date(date.Year(), startMonth, 1, 0, 0, 0, 0, date.Location())}// output:2024-01-01 00:00:00 +0800 CST
上述函數接受一個日期,返回該日期所在季度的第一天。
func EndOfQuarter(date time.Time) time.Time { startOfNextQuarter := StartOfQuarter(date).AddDate(0, 3, 0) return startOfNextQuarter.Add(-time.Second)}// output:2024-03-31 23:59:59 +0800 CST
上述函數接受一個日期,返回該日期所在季度的最后一天的最后一秒。
func CurrentWeekRange(timeZone string) (startOfWeek, endOfWeek time.Time) { loc, _ := time.LoadLocation(timeZone) now := time.Now().In(loc) startOfWeek = StartOfDayOfWeek(now) endOfWeek = EndOfDayOfWeek(now) return startOfWeek, endOfWeek}// output:2024-01-15 00:37:18.812985 +0800 CST 2024-01-21 00:37:18.812985 +0800 CST
上述函數接受一個時區字符串,返回該時區中當前周的開始時間和結束時間。通過調用前述的獲取每周開始日和結束日的函數,我們獲得了當前周的范圍。
func DurationBetween(start, end time.Time) time.Duration { return end.Sub(start)}// output:10.000000101s
上述函數接受兩個日期,返回它們之間的持續時間。這個函數在測量兩個事件之間經過的時間時非常有用。
func GetDatesForDayOfWeek(year, month int, day time.Weekday) []time.Time { var dates []time.Time firstDayOfMonth := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC) diff := int(day) - int(firstDayOfMonth.Weekday()) if diff < 0 { diff += 7 } firstDay := firstDayOfMonth.AddDate(0, 0, diff) for current := firstDay; current.Month() == time.Month(month); current = current.AddDate(0, 0, 7) { dates = append(dates, current) } return dates}// output:[2024-01-05 00:00:00 +0000 UTC 2024-01-12 00:00:00 +0000 UTC 2024-01-19 00:00:00 +0000 UTC 2024-01-26 00:00:00 +0000 UTC]
上述函數接受年份、月份和目標星期幾,返回給定月份中指定日期的所有出現情況。這為獲取一個月中特定日期的出現提供了通用的解決方案。
func AddBusinessDays(startDate time.Time, daysToAdd int) time.Time { currentDate := startDate for i := 0; i < daysToAdd; { currentDate = currentDate.AddDate(0, 0, 1) if currentDate.Weekday() != time.Saturday && currentDate.Weekday() != time.Sunday { i++ } } return currentDate}// output:2024-03-01 00:46:38.131747 +0800 CST
上述函數接受一個起始日期和要添加的工作日數,返回加上指定工作日后的日期。這對于處理只涉及工作日的應用程序非常有用。
func FormatDuration(duration time.Duration) string { days := int(duration.Hours() / 24) hours := int(duration.Hours()) % 24 minutes := int(duration.Minutes()) % 60 seconds := int(duration.Seconds()) % 60 return fmt.Sprintf("%d天 %02d小時 %02d分 %02d秒", days, hours, minutes, seconds)}// output:3天 04小時 15分 30秒
上述函數接受一個持續時間,返回一個格式化的字符串,以便更友好地顯示。這對于向用戶呈現持續時間時非常實用。
通過使用這些高級實用函數,我們擴展了日期時間包裝器的功能,為開發人員提供了一套全面的工具來處理各種與時間相關的操作。這些函數可以輕松集成到您的代碼庫中,簡化了復雜的日期和時間操作,無論是構建計劃應用程序、生成報告,還是處理各種時間敏感的任務。
本文鏈接:http://www.www897cc.com/showinfo-26-64972-0.htmlGo 日期時間包裝器:15條更便捷的時間處理
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com