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 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题