[m-rev.] calendar module proposal

Paul Bone pbone at csse.unimelb.edu.au
Tue Jan 27 17:01:58 AEDT 2009


On Tue, Jan 27, 2009 at 04:02:11PM +1100, Ian MacLarty wrote:
> Hi,
> 
> Here's a proposal for a calendar module for the standard library.
> Could interested people take a look and give me some feedback?
> Since this module only implements the Gregorian calendar, it might not
> be suitable for working with dates prior to 1582.  Perhaps it should be
> renamed to gregorian_calendar or gcalendar?

I'd prefer gregorian_calendar.

> Index: library/calendar.m
> ===================================================================
> RCS file: library/calendar.m
> diff -N library/calendar.m
> --- /dev/null	1 Jan 1970 00:00:00 -0000
> +++ library/calendar.m	27 Jan 2009 03:51:25 -0000
> @@ -0,0 +1,961 @@
> +%-----------------------------------------------------------------------------%
> +% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
> +%-----------------------------------------------------------------------------%
> +% Copyright (C) 2009 The University of Melbourne.
> +% This file may only be copied under the terms of the GNU Library General
> +% Public License - see the file COPYING.LIB in the Mercury distribution.
> +%-----------------------------------------------------------------------------%
> +% 
> +% File: calendar.m.
> +% Main authors: maclarty
> +% Stability: low.
> +% 
> +% Proleptic Gregorian calendar utilities.
> +% 
> +%-----------------------------------------------------------------------------%
> +%-----------------------------------------------------------------------------%
> +
> +:- module calendar.
> +:- interface.
> +
> +:- import_module io.
> +
> +%-----------------------------------------------------------------------------%
> +
> +    % A point on the Proleptic Gregorian calendar, to the nearest second.
> +    %
> +:- type date.
> +
> +    % A period of time measured in years, months, days, hours, minutes and
> +    % seconds.
> +    %
> +:- type duration.
> +
> +:- type month
> +    --->    january
> +    ;       february
> +    ;       march
> +    ;       april
> +    ;       may
> +    ;       june
> +    ;       july
> +    ;       august
> +    ;       september
> +    ;       october
> +    ;       november
> +    ;       december.
> +
> +    
> +    % Date components.
> +    %
> +:- type year == int.         % Year 0 is 1 BC, -1 is 2 BC, etc.
> +:- type day_of_month == int. % 1..31 depending on the month and year
> +:- type hour == int.         % 0..23
> +:- type minute == int.       % 0..59
> +:- type second == int.       % 0..61 (60 and 61 are for leap seconds)

Should microseconds be supported?  Even if some system libraries don't
allow access to the absolute time in mincroseconds it might still be
useful to be able to use them.

> +
> +:- type day_of_week
> +    --->    sunday
> +    ;       monday
> +    ;       tuesday
> +    ;       wednesday
> +    ;       thursday
> +    ;       friday
> +    ;       saturday.

IMHO a week starts on Monday, so sunday should sort after monday (after
saturday), but I'm not sure what is 'official'.


> +%----------------------------------------------------------------------------%
> +
> +:- type date
> +    --->    date(
> +                dt_year         :: int,
> +                dt_month        :: int,
> +                dt_day          :: int,
> +                dt_hour         :: int,
> +                dt_minute       :: int,
> +                dt_second       :: int
> +            ).
> +
> +:- type duration
> +    --->    duration(
> +                dur_years   :: int,
> +                dur_months  :: int,
> +                dur_days    :: int,
> +                dur_hours   :: int,
> +                dur_minutes :: int,
> +                dur_seconds :: int
> +            ).
> +

The best representation of duration I've ever seen is Postgreql's
internal INTERVAL structure.  It represents durations in terms of just
three values (the minimum possible number of values): months, days, and
seconds, although it allows a user to use any unit to express the
interval.

I recommend taking a look at Postgresql's INTERVAL:

http://www.postgresql.org/docs/8.3/interactive/datatype-datetime.html#AEN4976


Good work.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.mercurylang.org/archives/reviews/attachments/20090127/a8280b14/attachment.sig>


More information about the reviews mailing list