返回当地的一天开始时间对象

huangapple go评论112阅读模式
英文:

Return local beginning of day time object

问题

获取今天的本地时间对象,我提取年月日并重新构建新的日期。这看起来有点笨拙。我是否错过了其他标准库函数?

以下是可运行的代码:http://play.golang.org/p/OSRl0nxyB7

  1. func Bod(t time.Time) time.Time {
  2. year, month, day := t.Date()
  3. return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
  4. }
  5. func main() {
  6. fmt.Println(Bod(time.Now()))
  7. }
英文:

To get a local beginning of today time object I extract YMD and reconstruct the new date. That looks like a kludge. Do I miss some other standard library function?

code also runnable at http://play.golang.org/p/OSRl0nxyB7 :

  1. func Bod(t time.Time) time.Time {
  2. year, month, day := t.Date()
  3. return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
  4. }
  5. func main() {
  6. fmt.Println(Bod(time.Now()))
  7. }

答案1

得分: 72

问题的标题和正文都要求“一个当地[芝加哥]的今天开始时间”。问题中的Bod函数正确地实现了这一要求。被接受的Truncate函数声称是一个更好的解决方案,但它返回了不同的结果;它并没有返回一个当地[芝加哥]的今天开始时间。例如,

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func Bod(t time.Time) time.Time {
  7. year, month, day := t.Date()
  8. return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
  9. }
  10. func Truncate(t time.Time) time.Time {
  11. return t.Truncate(24 * time.Hour)
  12. }
  13. func main() {
  14. chicago, err := time.LoadLocation("America/Chicago")
  15. if err != nil {
  16. fmt.Println(err)
  17. return
  18. }
  19. now := time.Now().In(chicago)
  20. fmt.Println(Bod(now))
  21. fmt.Println(Truncate(now))
  22. }

输出结果:

  1. 2014-08-11 00:00:00 -0400 EDT
  2. 2014-08-11 20:00:00 -0400 EDT

time.Truncate方法截断了UTC时间。

被接受的Truncate函数还假设一天有24小时。芝加哥的一天可能有23、24或25个小时。

英文:

Both the title and the text of the question asked for "a local [Chicago] beginning of today time." The Bod function in the question did that correctly. The accepted Truncate function claims to be a better solution, but it returns a different result; it doesn't return a local [Chicago] beginning of today time. For example,

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func Bod(t time.Time) time.Time {
  7. year, month, day := t.Date()
  8. return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
  9. }
  10. func Truncate(t time.Time) time.Time {
  11. return t.Truncate(24 * time.Hour)
  12. }
  13. func main() {
  14. chicago, err := time.LoadLocation("America/Chicago")
  15. if err != nil {
  16. fmt.Println(err)
  17. return
  18. }
  19. now := time.Now().In(chicago)
  20. fmt.Println(Bod(now))
  21. fmt.Println(Truncate(now))
  22. }

Output:

  1. 2014-08-11 00:00:00 -0400 EDT
  2. 2014-08-11 20:00:00 -0400 EDT

The time.Truncate method truncates UTC time.

The accepted Truncate function also assumes that there are 24 hours in a day. Chicago has 23, 24, or 25 hours in a day.

答案2

得分: 9

这只适用于UTC时间(它在playground中进行了测试,所以特定于位置的测试可能是错误的)。有关此解决方案在特定位置场景中的问题,请参阅PeterSO的答案。

您可以使用日期的Truncate方法,将持续时间设置为24 * time.Hour

  1. func main() {
  2. // 测试特定位置也可以正常工作
  3. loc, _ := time.LoadLocation("Europe/Berlin")
  4. t1, _ := time.ParseInLocation("2006 Jan 02 15:04:05 (MST)", "2012 Dec 07 03:15:30 (CEST)", loc)
  5. t2, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 00:00:00")
  6. t3, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 23:15:30")
  7. t4, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 23:59:59")
  8. t5, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 08 00:00:01")
  9. times := []time.Time{t1, t2, t3, t4, t5}
  10. for _, d := range times {
  11. fmt.Printf("%s\n", d.Truncate(24*time.Hour))
  12. }
  13. }

为了解释一下,这个方法之所以有效,是因为Truncate方法会将指定的持续时间“向下舍入到”从零时刻开始的倍数,而零时刻是公元1年1月1日00:00:00。因此,将时间截断到最近的24小时边界总是返回“一天的开始”。

英文:

EDIT: This only works for UTC times (it was tested in the playground, so the location-specific test was probably wrong). See PeterSO's answer for issues of this solution in location-specific scenarios.

You can use the Truncate method on the date, with 24 * time.Hour as duration:

http://play.golang.org/p/zJ8s9-6Pck

  1. func main() {
  2. // Test with a location works fine too
  3. loc, _ := time.LoadLocation("Europe/Berlin")
  4. t1, _ := time.ParseInLocation("2006 Jan 02 15:04:05 (MST)", "2012 Dec 07 03:15:30 (CEST)", loc)
  5. t2, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 00:00:00")
  6. t3, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 23:15:30")
  7. t4, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 23:59:59")
  8. t5, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 08 00:00:01")
  9. times := []time.Time{t1, t2, t3, t4, t5}
  10. for _, d := range times {
  11. fmt.Printf("%s\n", d.Truncate(24*time.Hour))
  12. }
  13. }

To add some explanation, it works because truncate "rounds down to a multiple of" the specified duration since the zero time, and the zero time is January 1, year 1, 00:00:00. So truncating to the nearest 24-hour boundary always returns a "beginning of day".

答案3

得分: 0

我需要将实现速度加快一点。以下是一个减少对时间调用的示例用法。

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/tentorium-trading/api/conf"
  6. )
  7. var (
  8. NewYorkLocation, _ = time.LoadLocation("America/New_York")
  9. )
  10. func main() {
  11. fmt.Println(GetStartOfDayNY(1664337601))
  12. // fmt.Println(GetStartOfDayNY(time.Now().Unix()))
  13. }
  14. func GetStartOfDayNY(unixTS int64) int64 {
  15. tm := time.Unix(unixTS, 0).In(conf.NewYorkLocation)
  16. hour, minute, second := tm.Clock()
  17. return unixTS - int64(hour*3600+minute*60+second)
  18. }

请注意,我只翻译了代码部分,其他内容不做翻译。

英文:

I needed the implementation to be a bit faster. Here's an example usage cutting down on calls to time.

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/tentorium-trading/api/conf"
  6. )
  7. var (
  8. NewYorkLocation, _ = time.LoadLocation("America/New_York")
  9. )
  10. func main() {
  11. fmt.Println(GetStartOfDayNY(1664337601))
  12. // fmt.Println(GetStartOfDayNY(time.Now().Unix()))
  13. }
  14. func GetStartOfDayNY(unixTS int64) int64 {
  15. tm := time.Unix(unixTS, 0).In(conf.NewYorkLocation)
  16. hour, minute, second := tm.Clock()
  17. return unixTS - int64(hour*3600+minute*60+second)
  18. }

huangapple
  • 本文由 发表于 2014年8月12日 08:26:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/25254443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定