Live data from Hacker News

Time in Go

bl.ocks.org

11–20 of 80 posts

Re: Time in Go

#11
post #7
post #6

Earlier quoted context omitted.

Do you have any examples of annoying to parse time formats?

http://play.golang.org/p/hCoZ3tdeM_ am I doing this correctly? Because it seems that golang does not recognize the abbreviations at all.

[deleted]

Re: Time in Go

#12
I'm surprised about the method to define a time format by example. There could be an ambiguity between month and day if the example is badly chosen.

I prefer the C way with the struct and format.

How do I get the time offset to UTC time at a given date and location ?

Re: Time in Go

#13
post #12

I'm surprised about the method to define a time format by example. There could be an ambiguity between month and day if the example is badly chosen. I prefer the C way with the struct and format. How do I get the time offset to UTC time at a given date and location ?

There is no ambiguity - you are to provide predefined date in your layout. That predefined date (Mon Jan 2 15:04:05 -0700 MST 2006) is chosen in a way so that there is no ambiguity.

Re: Time in Go

#14

More languages should copy Go's time library. As someone who has written a lot of Go for years, I am always very quick to say that "time" is my favorite Go standard library. I love it. The "time" library is to Go what the "requests" [non-standard] library is to Python, in terms of a great API and simplicity. For some reason in every other language I've used (important, I don't know every time library! :P) the time li…

> What I'm trying to say is: even if you don't like Go, take a look at how Go's "time" library works. I think it would be valuable for other languages. Other languages can probably even do it better (imagining better type systems around units), but I've never seen an API that feels as right for manipulating time as Go's time stdlib.

Then you might be interested in Joda-Time[1] and Boost.Date_time[2]. I'm not saying they are better or worse than Go's time library. They are quite powerful in their own right however.

Taking the example you presented:

  take the more generic problem of: I have
  a time, I have a duration, how do I tell
  if that duration has passed?
In Joda-Time, it could look like (all types are in org.joda.time):

  new Interval (yourStartDate, theDuration).contains (
    candidate
    );
And in Boost.Date_Time (loosely based on this example[3]):

  date d = day_clock::local_day ();
  time_period allDay (ptime (d), ptime (d, hours (24)));

  if (allDay.contains (someTimeInstance)) { ... }
1 - http://joda-time.sourceforge.net/userguide.html

2 - http://www.boost.org/doc/libs/1_59_0/doc/html/date_time.html

3 - http://www.boost.org/doc/libs/1_59_0/doc/html/date_time/exam...

Re: Time in Go

#15
Looking at the docs it looks fine; only thing I can remark is that it's too bad that all sorts of constants are in the same namespace; I'd prefer to have all the formatting formats in a separate namespace for intance to make it more clear.

Re: Time in Go

#16

    fmt.Printf("\n... and %v days after that ...\n", days)
    t2 := t1.Add(time.Duration(days) * time.Hour * 24)
    printTime(t2)
How is that supposed to work with daylight saving time? Won't t2 end up one hour off from t1 if there is a daylight saving shift?

Re: Time in Go

#17
post #7
post #6

Earlier quoted context omitted.

Do you have any examples of annoying to parse time formats?

http://play.golang.org/p/hCoZ3tdeM_ am I doing this correctly? Because it seems that golang does not recognize the abbreviations at all.

If you use time zones without offsets (just the name like PST instead of -0700) then you have to parse the time in a specific location http://play.golang.org/p/JzKAq09NtE

I can only assume that the time zone name alone is insufficient to resolve ambiguous times.

Re: Time in Go

#18
post #10
post #6

Earlier quoted context omitted.

Do you have any examples of annoying to parse time formats?

I can find it later, but I just had this issue this week with timestamps sent from a browser, supposedly in RFC3339, but Go's RFC3339 format was not compliant with it. So naturally I made my own format template. Alas, the milliseconds part had arbitrary precision, which Go's parser doesn't like. So I had to revert to trying the timestamp with one, two ,three trailing digits, etc. Still, this is the only issue I have…

I wasn't able to reproduce this issue, time.Parse(time.RFC3339Nano, "2015-09-10T06:31:39.442Z") seems to work correctly as long as the number of digits between "." and "Z" is 0-9

Re: Time in Go

#19
post #9

So I copied the example from http://golang.org/pkg/time/#Parse and it did NOT work as expected: http://play.golang.org/p/Xd9oEeSffd (timezone offset is zero)

This works correctly if your locale includes the PST timezone (works fine on my computer). They should probably have a better example though.

Re: Time in Go

#20
post #7
post #6

Earlier quoted context omitted.

Do you have any examples of annoying to parse time formats?

http://play.golang.org/p/hCoZ3tdeM_ am I doing this correctly? Because it seems that golang does not recognize the abbreviations at all.

[deleted]
Post reply on HN