日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

Go 日期時間包裝器:15條更便捷的時間處理

來源: 責編: 時間:2024-01-19 09:18:50 221觀看
導讀在Go編程中,處理日期和時間是一項常見任務,涉及到精確性和靈活性。盡管Go的標準庫提供了時間包(time)用于處理時間相關操作,但在某些情況下,我們需要額外的實用函數來簡化這些任務。本文將介紹一系列實用函數,它們充當time包

在Go編程中,處理日期和時間是一項常見任務,涉及到精確性和靈活性。盡管Go的標準庫提供了時間包(time)用于處理時間相關操作,但在某些情況下,我們需要額外的實用函數來簡化這些任務。本文將介紹一系列實用函數,它們充當time包的包裝器,提供了更便捷的操作方式。b6l28資訊網——每日最新資訊28at.com

獲取月初和月底

獲取月初

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())將返回當前月份的第一天的時間戳。b6l28資訊網——每日最新資訊28at.com

獲取月底

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函數,我們確保了準確的計算。b6l28資訊網——每日最新資訊28at.com

獲取每周的開始日和結束日

獲取每周的開始日

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

上述函數接受一個日期,返回該日期所在周的第一天。b6l28資訊網——每日最新資訊28at.com

獲取每周的結束日

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

上述函數接受一個日期,返回該日期所在周的最后一天。b6l28資訊網——每日最新資訊28at.com

獲取給定月份每周的開始日和結束日

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}]

上述函數接受年份和月份,返回一個包含給定月份中每周的開始日和結束日的切片。通過調用前述的獲取每周開始日和結束日的函數,我們得到了全面的每周視圖。b6l28資訊網——每日最新資訊28at.com

獲取從日期開始的一個月的周數

func WeekNumberInMonth(date time.Time) int { startOfMonth := StartOfMonth(date) _, week := date.ISOWeek() _, startWeek := startOfMonth.ISOWeek() return week - startWeek + 1}// output:3

上述函數接受一個日期,返回該日期所在月份的相對周數。通過利用ISO周數的概念,我們實現了簡便的計算。b6l28資訊網——每日最新資訊28at.com

獲取新年伊始和年底

獲取新年伊始

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

上述函數接受一個日期,返回該日期所在年份的第一天。b6l28資訊網——每日最新資訊28at.com

獲取年底

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

上述函數接受一個日期,返回該日期所在年份的最后一天的最后一秒。b6l28資訊網——每日最新資訊28at.com

獲取季度初數據和季度末

獲取季度初數據

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

上述函數接受一個日期,返回該日期所在季度的第一天。b6l28資訊網——每日最新資訊28at.com

獲取季度末

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

上述函數接受一個日期,返回該日期所在季度的最后一天的最后一秒。b6l28資訊網——每日最新資訊28at.com

獲取當前周范圍

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

上述函數接受一個時區字符串,返回該時區中當前周的開始時間和結束時間。通過調用前述的獲取每周開始日和結束日的函數,我們獲得了當前周的范圍。b6l28資訊網——每日最新資訊28at.com

計算兩個日期之間的持續時間

func DurationBetween(start, end time.Time) time.Duration { return end.Sub(start)}// output:10.000000101s

上述函數接受兩個日期,返回它們之間的持續時間。這個函數在測量兩個事件之間經過的時間時非常有用。b6l28資訊網——每日最新資訊28at.com

獲取給定月份的星期幾的日期

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]

上述函數接受年份、月份和目標星期幾,返回給定月份中指定日期的所有出現情況。這為獲取一個月中特定日期的出現提供了通用的解決方案。b6l28資訊網——每日最新資訊28at.com

將工作日添加到日期

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

上述函數接受一個起始日期和要添加的工作日數,返回加上指定工作日后的日期。這對于處理只涉及工作日的應用程序非常有用。b6l28資訊網——每日最新資訊28at.com

將持續時間格式化為人類可讀的字符串

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秒

上述函數接受一個持續時間,返回一個格式化的字符串,以便更友好地顯示。這對于向用戶呈現持續時間時非常實用。b6l28資訊網——每日最新資訊28at.com

通過使用這些高級實用函數,我們擴展了日期時間包裝器的功能,為開發人員提供了一套全面的工具來處理各種與時間相關的操作。這些函數可以輕松集成到您的代碼庫中,簡化了復雜的日期和時間操作,無論是構建計劃應用程序、生成報告,還是處理各種時間敏感的任務。b6l28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-64972-0.htmlGo 日期時間包裝器:15條更便捷的時間處理

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 不再手動編輯配置文件:Python助您輕松應對TOML

下一篇: PowerShell Cmdlet高級參數介紹,你都知道多少?

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 和龙市| 安徽省| 屏南县| 江北区| 吕梁市| 祥云县| 肇东市| 常宁市| 东兰县| 依兰县| 五常市| 蒙阴县| 交口县| 张北县| 阳春市| 兰坪| 婺源县| 和硕县| 尤溪县| 荥经县| 通河县| 永嘉县| 德州市| 绥棱县| 阳高县| 长汀县| 萨迦县| 育儿| 外汇| 巴马| 临潭县| 河西区| 南充市| 祁连县| 克山县| 湟中县| 保德县| 松阳县| 驻马店市| 陈巴尔虎旗| 资溪县|