英文:
why does time.Date() give different results when using hardcoded int values vs the values returned by another time.Date()?
问题
我正在尝试从一个API中检索数据,其中我的请求必须具有开始和结束的Unix时间戳,但是我使用的方法得到了非常奇怪的结果。
(我将在示例中使用基于Go Playground的日期,它始终将time.Now().UTC()报告为2009-11-10 23:00:00)
我将安排每天运行此任务,并希望它始终检索两天前的所有记录,因此我需要发送一个开始时间为2009-11-08 00:00:00和结束时间为2009-11-08 23:59:59。
以下是我的方法摘要:
// 获取当前的UTC时间
now := time.Now().UTC()
// 从当前时间减去两天
twoDaysAgo := now.AddDate(0, 0, -2)
// 分别提取天、月和年,用于“重建”或“重置”我们的时间为00:00:00
day, month, year := twoDaysAgo.Date()
// 使用提取的天、月和年构建开始时间
startTime := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
当打印startTime时,我期望得到2009-11-08 00:00:00,但实际得到的是0014-05-02 00:00:00
package main
import (
"fmt"
"time"
)
func main() {
// 我们试图在指定的日期上“重置时间”,即从今天开始过去的两天。
// 这个time.Time将作为我们要进行请求的“开始”时间戳。
// 首先,获取当前时间,Go Playground中硬编码为2009-11-10 23:00:00,并打印预期值
now := time.Now().UTC()
fmt.Println("当前时间:", now)
// 减去两天,应该是2009-11-08 23:00:00,并打印预期值
twoDaysAgo := now.AddDate(0, 0, -2)
fmt.Println("两天前:", twoDaysAgo)
// 从twoDaysAgo中提取天、月和年,应该是2009年11月8日
// 这将打印预期值,以确认day、month和year包含我们需要的内容
day, month, year := twoDaysAgo.Date()
fmt.Println("提取的天、月和年:", day, month, year)
// 使用time.Date()创建一个新的time.Time,以便将时间“重置”为00:00:00
// 我们使用之前提取的day、month和year作为参数来实现这一点。
// 这将打印出一个极不准确的日期0014-05-02 00:00:00,原因我不明白。
dateFromDateExtraction := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
fmt.Println("这个日期很奇怪:", dateFromDateExtraction)
// 如果我们硬编码time.Date()的参数,
// 结果将符合预期,这将打印出2009-11-08 00:00:00
dateHardcoded := time.Date(2009, time.November, 8, 0, 0, 0, 0, time.UTC)
fmt.Println("这个日期符合预期:", dateHardcoded)
}
英文:
I am trying to retrieve data from an API in which my request must have a start and end unix timestamp, and I a getting really strange results with my methodology.
(I'll use dates in my example based on the Go Playground, which always reports time.Now().UTC() as 2009-11-10 23:00:00)
I will be scheduling this to run daily, and I'd like it to always retrieve all records from 2 days ago, so I need to send a start time of 2009-11-08 00:00:00 and an end time of 2009-11-08 23:59:59.
Here's a summary of my methodology:
// Retrieve the time now in UTC
now := time.Now().UTC()
// Subtract two days from now
twoDaysAgo := now.AddDate(0, 0, -2)
// Extract the day, month, and year individually,
// which will be used to "rebuild" or "reset" our clock to 00:00:00
day, month, year := twoDaysAgo.Date()
// Build our start time using the extracted day, month, and year
startTime := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
when startTime is printed, I expect to get 2009-11-08 00:00:00 but instead get 0014-05-02 00:00:00
package main
import (
"fmt"
"time"
)
func main() {
// we are trying to "reset the clock" on a specified date, 2 days in the past from today.
// this time.Time will be used as a "start" timestamp for a request we want to make.
// first, get the current time, which is hardcoded in Go Playground to be 2009-11-10 23:00:00,
// and prints the expected value
now := time.Now().UTC()
fmt.Println("Now:", now)
// subtract two days, which should be 2009-11-08 23:00:00 and prints the expected value
twoDaysAgo := now.AddDate(0, 0, -2)
fmt.Println("Two days ago:", twoDaysAgo)
// extract the day, month, and year from twoDaysAgo, which should be 2009 November 8
// this prints the expected values so we can confirm that day, month, and year each contain what we need
day, month, year := twoDaysAgo.Date()
fmt.Println("Extracted day, month, and year:", day, month, year)
// create a new time.Time using time.Date() so we can "reset" the clock to 00:00:00
// we use day, month, and year extracted earlier as parameters to achieve this.
// this prints a wildly inaccurate date of 0014-05-02 00:00:00 for a reason I don't understand.
dateFromDateExtraction := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
fmt.Println("This date is weird: ", dateFromDateExtraction)
// if instead we hardcode the parameters for time.Date(),
// the result is what you expect and this prints 2009-11-08 00:00:00
dateHardcoded := time.Date(2009, time.November, 8, 0, 0, 0, 0, time.UTC)
fmt.Println("This date appears as expected: ", dateHardcoded)
}
答案1
得分: 0
你已经颠倒了Date()
的返回值。应该是这样的:
year, month, day := twoDaysAgo.Date()
英文:
You've reversed the return values from Date()
. It should be this:
year, month, day := twoDaysAgo.Date()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论