Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return local beginning of day time object

Tags:

datetime

go

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())) } 
like image 588
Vlad Didenko Avatar asked Aug 12 '14 00:08

Vlad Didenko


Video Answer


2 Answers

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.

like image 97
peterSO Avatar answered Sep 19 '22 23:09

peterSO


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".

like image 41
mna Avatar answered Sep 19 '22 23:09

mna