Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] date manipulation library
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Pierre Weis <pierre.weis@i...>
Subject: Re: [Caml-list] date manipulation library
Hi Alan,

> I am writing an application that needs to manipulate dates. More 
> precisely, it needs a function that, given a date and a duration (12 
> days, 2 weeks, 3 months ...) returns the date at the end of the 
> duration. Is there a library providing such a thing ? (I have looked at 
> NetDate in ocamlnet, but I cannot find a way to do it that is not a hack 
> (convert the date to float, add the number of seconds corresponding to 
> the duration (tricky in the case of months), and convert back to date 
> format)).
> 
> Alan Schmitt
> 
> -- 
> The hacker: someone who figured things out and made something cool happen.
> 
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

As part of the bazar-ocaml, the ``htmlc'' template files compiler for
HTML is distributed for years now.
As part of htmlc, a complete (i.e. fairly rich) date manipulation
library is distributed.
It has been used for years in various contexts, is properly
documented, and has no known bugs.

If this library approximately satisfies your needs, let me know if
something is missing, or if you want to add more functionalities: we
can share the development!

To give you an idea, here is the interface for the (auxilliary) date module:

-------------------------------------------------------------------------
(* Getting the time of day. *)

val local_date_of_day : Lang.t -> string;;
 (* Return the current date as a string according to the [lang] argument.
    Assumes the local time zone. *)

val date_of_day : Lang.t -> string;;
 (* Return the current date as a string according to the [lang] argument.
    Assumes Greenwich meridian time zone, also known as UTC. *)

val date_uk_of_day : unit -> string;;
val date_fr_of_day : unit -> string;;
 (* The string representation of the current time of day, respectively
    in english and french. Assumes the local time zone. *)

val date : unit -> string;;
 (* Synomym for [date_of_day Uk]. *)

val local_date : unit -> string;;
 (* Synomym for [local_date_of_day Uk]. *)

val time_of_day : unit -> tm;;
 (* Return the time of day as a Unix time. Assumes Greenwich meridian
    time zone, also known as UTC. *)

val string_of_time : Lang.t -> tm -> string;;
 (* Translate a Unix time to a string according to the [lang]
    argument. *)

 (* Last modification time of a file. *)
val last_modification_time_of_file : string -> tm;;
val last_modification_date_of_file : Lang.t -> string -> string;;
 (* Assumes Greenwich meridian time zone. *)
-------------------------------------------------------------------------

And now, the interface file for lib_date library:

-------------------------------------------------------------------------
val year_is_leap : int -> bool;;
 (* Checks if the int argument represents a leap year. *)
val last_day_of_month : int -> int -> int;;
 (* [last_day_of_month month year] returns the number of the last day
    of the month for the given year. *)
val date_is_valid : int -> int -> int -> bool;;
 (* [date_is_valid month_day month year] checks if the given arguments
    represent a valid date. *)
val compare_date : (int * int * int) -> (int * int * int) -> int;;
 (* Compares two dates represented as triple of integers [(month_day,
    month, year)], with the same convention as the polymorphic
    comparison [compare]. [compare_date (d1, m1, y1) (d2, m2, y2)]
    returns respectively [0], [1], or [-1], if date [(d1, m1, y1)] is
    respectively equal to, greater than, or lower than
    the date [(d2, m2, y2)]. *)
val week_day_of_date : int -> int -> int -> int;;
 (* [week_day_of_date month_day month year] return the number of the
    day in the week that corresponds to the date
    [month_day, month, year].
    (Week days are numbered from [0] for sunday to [6] for saturday.) *)

(* Translation from integer representation to day and month names. *)

val string_of_month : Lang.t -> int -> string;;
val string_of_week_day : Lang.t -> int -> string;;
 (* Mapping from days and months, represented as theirs numbers, to
    their usual character string names.
    The numbering starts from 0: thus Sunday and January have number 0.
    These functions raise [Invalid_argument] if their arguments are out of
    the proper range ([0..6] for day numbers and [0..11] for month
    numbers), or if the language is not supported. *)
val string_of_date : Lang.t ->
    int -> int -> int ->
    string * string * string * string;;
 (* [string_of_date lang d m y] calculates the week day associated
    with the date [d, m, y], and returns the string representations of
    the tuple [(week_day, d, m, y)].
    For instance [string_of_date Uk (21, 3, 2000)] is
    [("Tuesday", "21", "March", "2000")]. *)

val string_of_full_date : Lang.t ->
    int -> int -> int -> int ->
    string * string * string * string;;
 (* [string_of_full_date lang w d m y] returns a string representation
    of the tuple [(w, d, m, y)], supposed to be a date [d, m, y] with
    week day [w]. *)

val string_of_mois : int -> string;;
val string_of_jour : int -> string;;
 (* Same as above for french days and months. *)

val week_day_of_string : Lang.t -> string -> int;;
val month_of_string : Lang.t -> string -> int;;
 (* Map a day  (resp. a month) represented as a string for the language
    [lang] to the corresponding number. Recognition of valid strings is
    case unsensitive and usual abbreviations are accepted.
    Raise [Invalid_argument "unknown language"], if the language is
    not supported. *)

val format_date : Lang.t -> int -> int -> int -> int -> string;;
 (* [format_date lang month_day month week_day year] returns a string
    representation of a date according to the language [lang]. The
    date is represented by 4 integers: [week_day] and
    [month] are the numbers encoding the names of the day and
    the name of the month. Their should be in a range suitable for calling
    [string_of_month] and [string_of_week_day].
    The [month_day] and [year] arguments are the number of the day in
    the month and the number of the year. If the year number is lesser
    than 1000, then 1900 is added to it.
    There is no verification that the date is valid.
    For instance,
    [format_date Uk 31 12 5 00] is ["Friday, December 31, 1900"],
    [format_date Uk 31 12 5 101] is ["Friday, December 31, 2001"],
    [format_date Uk 31 12 5 2000] is ["Friday, December 31, 2000"]. *)
val string_of_Uk_date : int -> int -> int -> int -> string;;
val string_of_Fr_date : int -> int -> int -> int -> string;;
 (* Same as above for french or english translations. *)

val parse_date : Lang.t -> string -> int * int * int * int;;
 (* [parse_date lang s] parse the string [s] according to the language
 [lang], and return a date, the validity of which is not verified.
 Separators between words in [s] can be any of [' '], ['\t'], ['/'], [','],
 ['-'], or ['.'] and can be repeated.
 - If only one word is found in [s], this word is supposed to be made of
 digits that encode the date. Eight digits means [ddmmyyyy] (as
 [01032000] to mean first of March 2000), six digits means [ddmmyy]
 (as [010300] to mean first of March 1900), less than six digits means
 [yy] or [yyyy] (only the year number is provided, day and month
 default to first of January).
 - If two words are found, they are supposed to be the month and the year
 in that order, and the resulting date defaults to the first day of the
 given month and year.
 - If three words are found, they are supposed to be the month day, the
 month and the year. Order is language dependant, and follows the
 conventions of [format_date].
 - If four words are found, they are supposed to be the week day, the
 month day, the month and the year. Order is language dependant, and
 follows the conventions of [format_date].

 Note: if necessary the week day of the date may be found using the
 function [week_day_of_date]. *)

val parse : string -> int * int * int * int;;
 (* [parse s] is equivalent to [parse_date Uk s]. *)
val format : int -> int -> int -> int -> string;;
 (* [format month_day month week_day year] is equivalent to
    [format_date Uk month_day month week_day year]. *)

val valid_date : Lang.t -> string -> int * int * int * int;;
 (* [valid_date lang s] parse the string [s] according to the
 language [lang] and return a valid date or raises [invalid_arg]. *)

val normalize_date : Lang.t -> string -> string * string * string * string;;
----------------------------------------------------------------------------

Hope this helps,

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners