time.daysIn(month, year)

5,092 views
Skip to first unread message

Peregrinati

unread,
May 9, 2012, 11:24:21 PM5/9/12
to golan...@googlegroups.com
Is there a reason time.daysIn(month, year) is not exported? Just now I wanted such a function and went to time.go looking for inspiration. Lo and behold, there it is, but it's inaccessible.

peterGo

unread,
May 10, 2012, 11:13:59 AM5/10/12
to golang-nuts
Peregrinati,

Since the time.daysIn(month, year) function is not exported, you have
to write your own function. To avoid replicating the
time.daysIn(month, year) logic and data, I write:

package main

import (
"fmt"
"time"
)

// daysIn returns the number of days in a month for a given year.
func daysIn(m time.Month, year int) int {
// This is equivalent to time.daysIn(m, year).
return time.Date(year, m+1, 0, 0, 0, 0, 0, time.UTC).Day()
}

func main() {
fmt.Println(daysIn(1, 2000))
fmt.Println(daysIn(2, 2000))
fmt.Println(daysIn(2, 2001))
fmt.Println(daysIn(6, 2000))
fmt.Println(daysIn(12, 2000))
}

/*
Output:
31
29
28
30
31
*/

Peter

Ben Barbour

unread,
May 10, 2012, 3:03:16 PM5/10/12
to peterGo, golang-nuts

Hi Peter,

Thanks for the function. I already wrote my own, but yours looks simpler.

What I was asking wasn't how to implement it though... I am curious if there's a reason the function isn't exported that I'm just not seeing. Did the developers just think nobody would need it given how simple it is to write your own with Date()?

Kyle Lemons

unread,
May 10, 2012, 5:11:41 PM5/10/12
to Ben Barbour, peterGo, golang-nuts
The more compelling reason is probably to keep the exported API simple.  The more functions that are exported that are helperish in nature, the harder it is to comprehend the entire API.  As it is, it's not that hard to know pretty much the entire standard library.  Perhaps a time/timeutil could be added with such helpers, but I don't think they would fit in time.

si guy

unread,
May 10, 2012, 6:34:53 PM5/10/12
to golan...@googlegroups.com
I have a continuous appreciation of this philosophy, it is a _massive_ time-saver.

pa...@daishisystems.com

unread,
Oct 12, 2015, 7:27:11 AM10/12/15
to golang-nuts
You can leverage https://github.com/daishisystems/month to achieve this.

Matt Harden

unread,
Oct 13, 2015, 2:30:43 AM10/13/15
to pa...@daishisystems.com, golang-nuts
It's trivial to implement in terms of the Time package. http://play.golang.org/p/H2cx1PLQRd

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

peterGo

unread,
Oct 14, 2015, 8:39:12 AM10/14/15
to golang-nuts, pa...@daishisystems.com

Peter Kleiweg

unread,
Oct 14, 2015, 2:37:29 PM10/14/15
to golang-nuts
I can see why this would not be exported.

Feeding an invalid date to get
something useful, that might brake when the implementation of Date changes.

Saver would be to feed a valid date, with first of the month, then add one month, then subtract one day.

Or simply use the know rules for number of days in a month. They are not difficult.

Matt Harden

unread,
Oct 14, 2015, 5:07:28 PM10/14/15
to Peter Kleiweg, golang-nuts
The documentation of Date specifically describes how invalid dates are normalized. This won't break when the implementation of Date changes.

Ben Barbour

unread,
Oct 14, 2015, 10:45:52 PM10/14/15
to golang-nuts
Ah, that's the kind of answer I was looking for, thanks Peter.
Reply all
Reply to author
Forward
0 new messages