-2

I'm working on a Golang example that requires some date calculations. I was rather hoping that Go would provide some nice date libraries similar to the excellent Python datetime module, but that doesn't appear to be the case.

How can I represent this python example in Go ?

from datetime import date

d0 = date(2013, 8, 18)
d1 = date(2018, 9, 26)
delta = d0 - d1
print delta.days
>>-1865

I've spent a fair bit of time looking around on how to do this I can't seem to find a definitive answer that is clear and concise and without caveats such as not properly calculating leap years etc.

This seems to be a fairly big limitation to what is becoming an excellent little language for building cross platform prototypes and eventually production applications.

user1513388
  • 7,165
  • 14
  • 69
  • 111

1 Answers1

2

I don't know how you've spent your fair bit of time looking and not finding anything, but the time package of the standard library has everything you want.

Here is your example coded in Go:

d0 := time.Date(2013, 8, 18, 0, 0, 0, 0, time.UTC)
d1 := time.Date(2018, 9, 26, 0, 0, 0, 0, time.UTC)

delta := d0.Sub(d1)

fmt.Println(delta.Hours() / 24)

Output (as expected):

-1865

Try it on the Go Playground.

icza
  • 389,944
  • 63
  • 907
  • 827
  • Hmm, nice thank you. I can't believe I missed that. Is there a `time.strptime(string[, format])` e.g. `time.strptime('11/24/2013', "%m/%d/%Y")` equivalent ? – user1513388 Apr 02 '15 at 10:05
  • @user1513388 Yes, there is. Check out the [`time.Parse()`](http://golang.org/pkg/time/#Parse) function. – icza Apr 02 '15 at 10:05
  • Thanks I've been just trying that. I've been hitting this issue though when trying to parse dates like this "24/11/2013" or this "11/24/2013" http://stackoverflow.com/questions/14106541/go-parsing-date-time-strings-which-are-not-standard-formats though which is rather confusing. – user1513388 Apr 02 '15 at 10:56
  • @user1513388 I don't think it's confusing. First you have to specify the format/layout in which the parseable input is presented. This is done by specifying a predefined date in using the format you want to parse: `time.Parse("01/02/2006", "10/15/1983")` - first is the reference date, 2nd is the input `string` containing the date you want to parse. – icza Apr 02 '15 at 10:59
  • Yeah get that bit but how can I parse a european formatted date. This fails: fmt.Print(time.Parse("23/02/06", "15/06/13")) with 0001-01-01 00:00:00 +0000 UTC parsing time "15/06/13" as "23/02/06": cannot parse "/06/13" as "3" – user1513388 Apr 02 '15 at 11:04
  • 1
    @user1513388 Looks you don't fully grasped it. The layout **must** contain the reference date formatted which is `2006-01-02`. And you specified `2006-02-26` - that is not a valid layout. – icza Apr 02 '15 at 11:08
  • Arr OK. Thanks this works now fmt.Print(time.Parse("02/01/06", "23/10/13")) >> 2013-10-23 00:00:00 +0000 UTC . Only other question is what does the mean ? – user1513388 Apr 02 '15 at 11:17
  • 1
    @user1513388 Please check the documentation of [`time.Parse()`](http://golang.org/pkg/time/#Parse). It returns 2 values, the 2nd one is an `error` which will be `nil` if parsing succeeds. – icza Apr 02 '15 at 11:24
  • Thanks very much - its starting to make sense to me now. – user1513388 Apr 02 '15 at 11:37