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

编程入门 行业动态 更新时间:2024-10-27 00:34:06
本文介绍了如何从第二天开始获取特定时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想为第二天(明天)的确切时间创建一个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.

请参阅: play.golang/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: play.golang/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 } }

更多推荐

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

本文发布于:2023-11-24 02:55:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623714.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第二天   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!