如何从第二天开始获取特定时间 [英] How to get specific time from next day

查看:80
本文介绍了如何从第二天开始获取特定时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为第二天(明天)的确切时间创建一个time.Time.现在,我想设置小时和分钟.

这是我目前使用的代码:

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

这将为今天的Date创建一个我要查找的确切时间(小时和分钟),然后在该Date中添加一天.这很好.


示例:

想象time.Now()2009-11-10 23:00:00 +0000 UTC.

以下代码的结果将是:2009-11-10 15:00:00 +0000 UTC

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

到目前为止,我使用AddDate(0, 0, 1)添加了一天.结果就是第二天的所需时间:2009-11-11 15:00:00 +0000 UTC.

请参阅: https://play.golang.org/p/OKR9V1HN50x


问题:

有没有更短的编写代码的方法?

解决方案

打包时间

import "time" 

月份,日期,小时,分钟,秒和nsec值可能不在其范围内 通常的范围,并将在转换过程中进行标准化.为了 例如,10月32日转换为11月1日.


这更有效.它将对包time函数和方法的调用减至最少.

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

输出:

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


一些基准:

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

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?

解决方案

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

这篇关于如何从第二天开始获取特定时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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