dongyu3967 2019-06-21 19:45
浏览 32
已采纳

如何从第二天开始获取特定时间?

I want to create a time.Time for an exact point in time the following day (tomorrow). For now I would like to set the hour and minute.

This is the code I use at the moment:

now := time.Now()
tomorrow := time.Date(now.Year(), now.Month(), now.Day(), 15, 0, 0, 0, time.UTC).AddDate(0,0,1)

This will create a Date for today with the exact time (hour and minute) I am looking for and then adds one day to that Date. This works fine.


Example:

Imagine time.Now() is 2009-11-10 23:00:00 +0000 UTC.

The result of the following code would be: 2009-11-10 15:00:00 +0000 UTC

tomorrow := time.Date(now.Year(), now.Month(), now.Day(), 15, 0, 0, 0, time.UTC)

To this date I add one day using AddDate(0, 0, 1). The result is then the desired time the next day: 2009-11-11 15:00:00 +0000 UTC.

See: https://play.golang.org/p/OKR9V1HN50x


Question:

Is there a shorter way to write this code?

  • 写回答

1条回答 默认 最新

  • dongyan7172 2019-06-21 20:20
    关注

    Package time

    import "time" 
    

    The month, day, hour, min, sec, and nsec values may be outside their usual ranges and will be normalized during the conversion. For example, October 32 converts to November 1.


    This is more efficient. It minimizes calls to package time functions and methods.

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        now := time.Now()
        fmt.Println(now.Round(0))
        yyyy, mm, dd := now.Date()
        tomorrow := time.Date(yyyy, mm, dd+1, 15, 0, 0, 0, now.Location())
        fmt.Println(tomorrow)
    }
    

    Output:

    2019-06-21 16:23:06.525478162 -0400 EDT
    2019-06-22 15:00:00 -0400 EDT
    

    Some benchmarks:

    BenchmarkNow-8                  31197811            36.6 ns/op
    BenchmarkTomorrowPeterSO-8      29852671            38.4 ns/op
    BenchmarkTomorrowJens-8          9523422           124 ns/op
    

    bench_test.go:

    package main
    
    import (
        "testing"
        "time"
    )
    
    func BenchmarkNow(b *testing.B) {
        for N := 0; N < b.N; N++ {
            now := time.Now()
            _ = now
        }
    }
    
    var now = time.Now()
    
    func BenchmarkTomorrowPeterSO(b *testing.B) {
        for N := 0; N < b.N; N++ {
            // now := time.Now()
            yyyy, mm, dd := now.Date()
            tomorrow := time.Date(yyyy, mm, dd+1, 15, 0, 0, 0, now.Location())
            _ = tomorrow
        }
    }
    
    func BenchmarkTomorrowJens(b *testing.B) {
        for N := 0; N < b.N; N++ {
            // now := time.Now()
            tomorrow := time.Date(now.Year(), now.Month(), now.Day(), 15, 0, 0, 0, now.Location()).AddDate(0, 0, 1)
            _ = tomorrow
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染