Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] Time stamp module floating around?

27 views
Skip to first unread message

Denis Bueno

unread,
Jan 20, 2007, 9:44:55 PM1/20/07
to OCaml Mailing List
I'm looking for simple bit of code that will print human-readable
timestamps. Something suitable for a log file.

I'm generating some logs for unit tests, and I would like the preamble
to a particular unit test's log file to read: '/foo/bar test ran 20
Jan 2007 at 21:41" or something similar.

I could write this module, but I figure there's *got* to be something
out there better than I would make in the time I have.

Thanks in advance.

-Denis

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Chris King

unread,
Jan 20, 2007, 10:50:48 PM1/20/07
to Denis Bueno
On 1/20/07, Denis Bueno <dbu...@gmail.com> wrote:
> I'm looking for simple bit of code that will print human-readable
> timestamps. Something suitable for a log file.

I noticed Calendar [1] on the Hump a while ago... it looks pretty
useful. The API includes a pretty printer.

[1] http://www.lri.fr/~signoles/prog.en.html

- Chris

skaller

unread,
Jan 21, 2007, 3:45:58 AM1/21/07
to Denis Bueno
On Sat, 2007-01-20 at 21:42 -0500, Denis Bueno wrote:
> I'm looking for simple bit of code that will print human-readable
> timestamps. Something suitable for a log file.

(*
Scrap used in Felix for lines like:

//Timestamp: 2007/1/12 18:36:37 UTC
//Timestamp: 2007/1/13 5:36:37 (local)
*)


let tim() =
let now = (Unix.times()).Unix.tms_utime in
let elapsed = now -. !last_time in
last_time := now;
elapsed
;;

let format_time tm =
si (tm.Unix.tm_year + 1900) ^ "/" ^
si (tm.Unix.tm_mon + 1) ^ "/" ^
si tm.Unix.tm_mday ^ " " ^
si tm.Unix.tm_hour ^ ":" ^
si tm.Unix.tm_min ^ ":" ^
si tm.Unix.tm_sec
;;
try
(* Time initialisation *)
let compile_start = Unix.time () in
let compile_start_gm = Unix.gmtime compile_start in
let compile_start_local = Unix.localtime compile_start in
let compile_start_gm_string = format_time compile_start_gm ^ " UTC" in
let compile_start_local_string = format_time compile_start_local ^
" (local)" in


--
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net

Nicolas George

unread,
Jan 21, 2007, 4:27:00 AM1/21/07
to Caml mailing list
Le duodi 2 pluviôse, an CCXV, skaller a écrit :

> //Timestamp: 2007/1/12 18:36:37 UTC

I would strongly advise you tu use fixed-length fields with leading zeros:
the big advantage of using the year-month-day order, instead of
month-day-year or day-month-year, is that the lexical order of the string is
the same as the chronological order of the date. Using variable-length
fields breaks this. Furthermore, it makes it harder to parse the timestamp.

On the other hand, it is important not to forget "UTC".

While I am at it, I believe that yyyy-mm-dd is a more common format as
yyyy/mm/dd.

> //Timestamp: 2007/1/13 5:36:37 (local)

It is a shame that whoever designed the tm structure in the first place
forgot the tm_gmtoff field.

Regards,

--
Nicolas George

signature.asc

Robert Roessler

unread,
Jan 21, 2007, 4:55:28 AM1/21/07
to Caml mailing list
Nicolas George wrote:
> Le duodi 2 pluviôse, an CCXV, skaller a écrit :
>> //Timestamp: 2007/1/12 18:36:37 UTC
>
> I would strongly advise you tu use fixed-length fields with leading zeros:
> the big advantage of using the year-month-day order, instead of
> month-day-year or day-month-year, is that the lexical order of the string is
> the same as the chronological order of the date. Using variable-length
> fields breaks this. Furthermore, it makes it harder to parse the timestamp.
>
> On the other hand, it is important not to forget "UTC".
>
> While I am at it, I believe that yyyy-mm-dd is a more common format as
> yyyy/mm/dd.

Indeed... it is called ISO 8601. :)

Robert Roessler
roes...@rftp.com
http://www.rftp.com

0 new messages