返回当地的一天开始时间对象

huangapple go评论89阅读模式
英文:

Return local beginning of day time object

问题

获取今天的本地时间对象,我提取年月日并重新构建新的日期。这看起来有点笨拙。我是否错过了其他标准库函数?

以下是可运行的代码:http://play.golang.org/p/OSRl0nxyB7

func Bod(t time.Time) time.Time {
    year, month, day := t.Date()
    return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
}

func main() {
    fmt.Println(Bod(time.Now()))
}
英文:

To get a local beginning of today time object I extract YMD and reconstruct the new date. That looks like a kludge. Do I miss some other standard library function?

code also runnable at http://play.golang.org/p/OSRl0nxyB7 :

func Bod(t time.Time) time.Time {
	year, month, day := t.Date()
	return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
}

func main() {
	fmt.Println(Bod(time.Now()))
}

答案1

得分: 72

问题的标题和正文都要求“一个当地[芝加哥]的今天开始时间”。问题中的Bod函数正确地实现了这一要求。被接受的Truncate函数声称是一个更好的解决方案,但它返回了不同的结果;它并没有返回一个当地[芝加哥]的今天开始时间。例如,

package main

import (
	"fmt"
	"time"
)

func Bod(t time.Time) time.Time {
	year, month, day := t.Date()
	return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
}

func Truncate(t time.Time) time.Time {
	return t.Truncate(24 * time.Hour)
}

func main() {
	chicago, err := time.LoadLocation("America/Chicago")
	if err != nil {
		fmt.Println(err)
		return
	}
	now := time.Now().In(chicago)
	fmt.Println(Bod(now))
	fmt.Println(Truncate(now))
}

输出结果:

2014-08-11 00:00:00 -0400 EDT
2014-08-11 20:00:00 -0400 EDT

time.Truncate方法截断了UTC时间。

被接受的Truncate函数还假设一天有24小时。芝加哥的一天可能有23、24或25个小时。

英文:

Both the title and the text of the question asked for "a local [Chicago] beginning of today time." The Bod function in the question did that correctly. The accepted Truncate function claims to be a better solution, but it returns a different result; it doesn't return a local [Chicago] beginning of today time. For example,

package main

import (
	"fmt"
	"time"
)

func Bod(t time.Time) time.Time {
	year, month, day := t.Date()
	return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
}

func Truncate(t time.Time) time.Time {
	return t.Truncate(24 * time.Hour)
}

func main() {
	chicago, err := time.LoadLocation("America/Chicago")
	if err != nil {
		fmt.Println(err)
		return
	}
	now := time.Now().In(chicago)
	fmt.Println(Bod(now))
	fmt.Println(Truncate(now))
}

Output:

2014-08-11 00:00:00 -0400 EDT
2014-08-11 20:00:00 -0400 EDT

The time.Truncate method truncates UTC time.

The accepted Truncate function also assumes that there are 24 hours in a day. Chicago has 23, 24, or 25 hours in a day.

答案2

得分: 9

这只适用于UTC时间(它在playground中进行了测试,所以特定于位置的测试可能是错误的)。有关此解决方案在特定位置场景中的问题,请参阅PeterSO的答案。

您可以使用日期的Truncate方法,将持续时间设置为24 * time.Hour

func main() {
    // 测试特定位置也可以正常工作
    loc, _ := time.LoadLocation("Europe/Berlin")
    t1, _ := time.ParseInLocation("2006 Jan 02 15:04:05 (MST)", "2012 Dec 07 03:15:30 (CEST)", loc)
    t2, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 00:00:00")
    t3, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 23:15:30")
    t4, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 23:59:59")
    t5, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 08 00:00:01")
    times := []time.Time{t1, t2, t3, t4, t5}

    for _, d := range times {
        fmt.Printf("%s\n", d.Truncate(24*time.Hour))
    }
}

为了解释一下,这个方法之所以有效,是因为Truncate方法会将指定的持续时间“向下舍入到”从零时刻开始的倍数,而零时刻是公元1年1月1日00:00:00。因此,将时间截断到最近的24小时边界总是返回“一天的开始”。

英文:

EDIT: This only works for UTC times (it was tested in the playground, so the location-specific test was probably wrong). See PeterSO's answer for issues of this solution in location-specific scenarios.

You can use the Truncate method on the date, with 24 * time.Hour as duration:

http://play.golang.org/p/zJ8s9-6Pck

func main() {
    // Test with a location works fine too
    loc, _ := time.LoadLocation("Europe/Berlin")
    t1, _ := time.ParseInLocation("2006 Jan 02 15:04:05 (MST)", "2012 Dec 07 03:15:30 (CEST)", loc)
    t2, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 00:00:00")
    t3, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 23:15:30")
    t4, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 07 23:59:59")
    t5, _ := time.Parse("2006 Jan 02 15:04:05", "2012 Dec 08 00:00:01")
    times := []time.Time{t1, t2, t3, t4, t5}

    for _, d := range times {
	    fmt.Printf("%s\n", d.Truncate(24*time.Hour))
    }
}

To add some explanation, it works because truncate "rounds down to a multiple of" the specified duration since the zero time, and the zero time is January 1, year 1, 00:00:00. So truncating to the nearest 24-hour boundary always returns a "beginning of day".

答案3

得分: 0

我需要将实现速度加快一点。以下是一个减少对时间调用的示例用法。

package main

import (
	"fmt"
	"time"

	"github.com/tentorium-trading/api/conf"
)

var (
	NewYorkLocation, _ = time.LoadLocation("America/New_York")
)

func main() {
	fmt.Println(GetStartOfDayNY(1664337601))

	// fmt.Println(GetStartOfDayNY(time.Now().Unix()))
}

func GetStartOfDayNY(unixTS int64) int64 {
	tm := time.Unix(unixTS, 0).In(conf.NewYorkLocation)
	hour, minute, second := tm.Clock()

	return unixTS - int64(hour*3600+minute*60+second)
}

请注意,我只翻译了代码部分,其他内容不做翻译。

英文:

I needed the implementation to be a bit faster. Here's an example usage cutting down on calls to time.

package main

import (
	"fmt"
	"time"

	"github.com/tentorium-trading/api/conf"
)

var (
	NewYorkLocation, _ = time.LoadLocation("America/New_York")
)

func main() {
	fmt.Println(GetStartOfDayNY(1664337601))

	// fmt.Println(GetStartOfDayNY(time.Now().Unix()))
}

func GetStartOfDayNY(unixTS int64) int64 {
	tm := time.Unix(unixTS, 0).In(conf.NewYorkLocation)
	hour, minute, second := tm.Clock()

	return unixTS - int64(hour*3600+minute*60+second)
}

huangapple
  • 本文由 发表于 2014年8月12日 08:26:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/25254443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定