按周数划分的日期范围 Golang [英] Date range by week number Golang

查看:30
本文介绍了按周数划分的日期范围 Golang的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过这个简单的函数,我可以得到周数.现在,有了星期数,我怎样才能得到日期范围,从星期日开始?

With this simple function, I can get the week number. Now, with the number of the week, how can I get the date range, started on Sunday?

import (
    "fmt"
    "time"
)
func main() {
    Week(time.Now().UTC())
}

func Week(now time.Time) string {
    _, thisWeek := now.ISOWeek()
    return "S" + strconv.Itoa(thisWeek)
}

欢迎任何帮助.谢谢.

推荐答案

前言: Time.ISOWeek() 返回您从星期一开始的周数,因此我将回答您的问题,该问题也处理从星期一开始的周数.如果您希望它在周日开始的几周内工作,请根据您的需要进行更改.

Foreword: Time.ISOWeek() returns you the week number that starts on Monday, so I will answer your question that also handles weeks starting on Monday. Alter it to your needs if you want it to work with weeks starting on Sunday.

我在 github.com/icza/gox 中发布了这个实用程序,请参阅 timex.WeekStart().

标准库不提供返回给定周的日期范围(年+周数)的函数.所以我们必须自己构建一个.

The standard library does not provide a function that would return you the date range of a given week (year+week number). So we have to construct one ourselves.

这并不难.我们可以从年中开始,与一周的第一天(星期一)对齐,获取该时间值所在的周,然后修正:添加与周差乘以 7 一样多的天.

And it's not that hard. We can start off from the middle of the year, align to the first day of the week (Monday), get the week of this time value, and corrigate: add as many days as the week difference multiplied by 7.

这就是它的样子:

func WeekStart(year, week int) time.Time {
    // Start from the middle of the year:
    t := time.Date(year, 7, 1, 0, 0, 0, 0, time.UTC)

    // Roll back to Monday:
    if wd := t.Weekday(); wd == time.Sunday {
        t = t.AddDate(0, 0, -6)
    } else {
        t = t.AddDate(0, 0, -int(wd)+1)
    }

    // Difference in weeks:
    _, w := t.ISOWeek()
    t = t.AddDate(0, 0, (week-w)*7)

    return t
}

测试它:

fmt.Println(WeekStart(2018, 1))
fmt.Println(WeekStart(2018, 2))
fmt.Println(WeekStart(2019, 1))
fmt.Println(WeekStart(2019, 2))

输出(在 Go Playground 上试试):

Output (try it on the Go Playground):

2018-01-01 00:00:00 +0000 UTC
2018-01-08 00:00:00 +0000 UTC
2018-12-31 00:00:00 +0000 UTC
2019-01-07 00:00:00 +0000 UTC

这个WeekStart() 实现的一个很好的特性是它可以很好地处理超出范围 周.也就是说,如果你通过 0 为一周,它将被解释为上一年的最后一周.如果一周通过-1,它将指定上一年的倒数第二周.同样,如果您通过一年中的最大周加 1,它将被解释为下一年的第一周等.

One nice property of this WeekStart() implementation is that it handles out-of-range weeks nicely. That is, if you pass 0 for the week, it will be interpreted as the last week of the previous year. If you pass -1 for the week, it will designate the second to last week of the previous year. Similarly, if you pass max week of the year plus 1, it will be interpreted as the first week of the next year etc.

上面的WeekStart()函数只返回给定一周的第一天(星期一),因为一周的最后一天总是它的第一天+6天.

The above WeekStart() function only returns the given week's first day (Monday), because the last day of the week is always its first day + 6 days.

如果我们还需要最后一天:

If we also need the last day:

func WeekRange(year, week int) (start, end time.Time) {
    start = WeekStart(year, week)
    end = start.AddDate(0, 0, 6)
    return
}

测试它:

fmt.Println(WeekRange(2018, 1))
fmt.Println(WeekRange(2018, 2))
fmt.Println(WeekRange(2019, 1))
fmt.Println(WeekRange(2019, 2))

输出(在 Go Playground 上试试):

Output (try it on the Go Playground):

2018-01-01 00:00:00 +0000 UTC 2018-01-07 00:00:00 +0000 UTC
2018-01-08 00:00:00 +0000 UTC 2018-01-14 00:00:00 +0000 UTC
2018-12-31 00:00:00 +0000 UTC 2019-01-06 00:00:00 +0000 UTC
2019-01-07 00:00:00 +0000 UTC 2019-01-13 00:00:00 +0000 UTC

这篇关于按周数划分的日期范围 Golang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆