Vigdís — LFE Friday, LiffyBot studying Erlang modules in a Mead-style command room

LFE Friday - calendar:date_to_gregorian_days/3

Heads up: This post was written for LFE 0.9 / Erlang/OTP 17. The current release is LFE 2.2.0. APIs and examples may be outdated.

This week's LFE Friday was translated with permission from the Erlang Thursday series by Steven Proctor. This week's translator: Robert Virding.

Today’s LFE Friday covers calendar:date_to_gregorian_days/3.

As we saw in last week’s LFE Friday on calendar:day_of_the_week/3 when we were looking at some error messages, we saw that the errors were coming from calendar:date_to_gregorian_days/3.

> (calendar:day_of_the_week 0 0 0)        
exception error: function_clause
  in (: calendar date_to_gregorian_days 0 0 0)
  in calendar:day_of_the_week/3 (calendar.erl, line 151)
> (calendar:day_of_the_week 1970 2 31)   
exception error: if_clause
  in calendar:date_to_gregorian_days/3 (calendar.erl, line 116)
  in calendar:day_of_the_week/3 (calendar.erl, line 151)
> (calendar:day_of_the_week 1970 13 2)
exception error: function_clause
  in (: calendar last_day_of_the_month1 1970 13)
  in calendar:date_to_gregorian_days/3 (calendar.erl, line 115)
  in calendar:day_of_the_week/3 (calendar.erl, line 151)

I promised you at the end of that post we would take a deeper look at calendar:date_to_gregorian_days/3 next time, so let’s fulfill that promise.

calendar:date_to_gregorian_days/3 takes three arguments, a non-negative integer for the year, an integer between 1 and 12 (inclusive) for the month, and an integer between 1 and 31 (inclusive) for the day of the month, and returns the number of days since 0000-01-01 in the Gregorian calendar.

> (calendar:date_to_gregorian_days 2015 4 19)
736072
> (calendar:date_to_gregorian_days 0 1 1)    
0
> (calendar:date_to_gregorian_days 1 1 1)
366
> (calendar:date_to_gregorian_days 1970 1 1)
719528
> (calendar:date_to_gregorian_days 1999 12 31)
730484

There is also a version calendar:date_to_gregorian_days/1, that takes a tuple of year, month, and day available if your code already has the date in tuple format.

> (calendar:date_to_gregorian_days #(2015 4 19))
736072
> (calendar:date_to_gregorian_days #(0 1 1))    
0
> (calendar:date_to_gregorian_days #(1 1 1))
366

And if we pass something invalid to calendar:date_to_gregorian_days/1, we see that it is calling calendar:date_to_gregorian_days/3. So it is just a nice helper function that does the pattern match destructing for us.

> (calendar:date_to_gregorian_days #(1 1 0))
exception error: function_clause
  in (: calendar date_to_gregorian_days 1 1 0)

-Proctor, Robert