From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 79589BB9A for ; Tue, 8 Nov 2005 05:09:25 +0100 (CET) Received: from wproxy.gmail.com (wproxy.gmail.com [64.233.184.199]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id jA849Ojx012641 for ; Tue, 8 Nov 2005 05:09:25 +0100 Received: by wproxy.gmail.com with SMTP id i13so63167wra for ; Mon, 07 Nov 2005 20:09:24 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:mime-version:content-type; b=JP0WIeWPvUAvqomW669YB2wxA3GE+S31z7rfOsHx7S3kuGnc4t+KGHwwkoJxA+Ggi+KyE1l1zL7hRq4nyDgxqi2RwXhyEHxiX9pJmq3oOoyl/RciHyPEsEF8LOBZuhAOsYw53BaMT0oyHUm8SW7L422zeinRnU4zXHdIGSRDyS0= Received: by 10.65.240.7 with SMTP id s7mr6200909qbr; Mon, 07 Nov 2005 20:09:23 -0800 (PST) Received: by 10.65.81.5 with HTTP; Mon, 7 Nov 2005 20:09:23 -0800 (PST) Message-ID: Date: Mon, 7 Nov 2005 23:09:23 -0500 From: Joshua Smith To: caml-list@yquem.inria.fr Subject: strftime/strptime and asctime MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_38172_13046211.1131422963829" X-Miltered: at nez-perce with ID 437024F4.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; patch:08 i've:11 things:12 joshua:13 attached:13 sources:14 add:14 there:17 code:17 tried:20 expressed:21 it's:22 well:22 addition:24 like:25 X-Attachments: type="application/octet-stream" name="ocaml-3.09.patchfile" name="ocaml-3.09.patchfile" X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_BY_IP autolearn=disabled version=3.0.3 ------=_Part_38172_13046211.1131422963829 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Well, There was one person who expressed interested, so I've attached the patch to the 3.09 sources to add strptime, strftime, and asctime. =20 It's a very simple addition (code-wise), and I tried to keep things looking like the existing code. Thank you. -jbs ------=_Part_38172_13046211.1131422963829 Content-Type: application/octet-stream; name=ocaml-3.09.patchfile Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="ocaml-3.09.patchfile" Index: otherlibs/unix/unix.mli =================================================================== --- otherlibs/unix/unix.mli (revision 1) +++ otherlibs/unix/unix.mli (revision 5) @@ -733,15 +733,30 @@ (** Convert a time in seconds, as returned by {!Unix.time}, into a date and a time. Assumes the local time zone. *) +val strptime: string -> string -> tm + (** This function is the converse of the {!Unix.strftime} function. + [strptime fmt data] convert a string containing time information [data] + into a [tm] struct according to the format specified by [fmt]. *) + +val asctime: tm -> string + (** Return the ascii representation of a given [tm] argument. The + ascii time is returned in the form of a string like + 'Wed Jun 30, 21:21:21 2005\n' *) + +val strftime: string -> tm -> string + (** This functions is the converse of the {!Unix.strptime} function. + [strftime fmt data] convert a a [tm] structure [data] into a string + according to the format specified by [fmt]. *) + val mktime : tm -> float * tm -(** Convert a date and time, specified by the [tm] argument, into - a time in seconds, as returned by {!Unix.time}. The [tm_isdst], - [tm_wday] and [tm_yday] fields of [tm] are ignored. Also return a - normalized copy of the given [tm] record, with the [tm_wday], - [tm_yday], and [tm_isdst] fields recomputed from the other fields, - and the other fields normalized (so that, e.g., 40 October is - changed into 9 November). The [tm] argument is interpreted in the - local time zone. *) + (** Convert a date and time, specified by the [tm] argument, into + a time in seconds, as returned by {!Unix.time}. The [tm_isdst], + [tm_wday] and [tm_yday] fields of [tm] are ignored. Also return a + normalized copy of the given [tm] record, with the [tm_wday], + [tm_yday], and [tm_isdst] fields recomputed from the other fields, + and the other fields normalized (so that, e.g., 40 October is + changed into 9 November). The [tm] argument is interpreted in the + local time zone. *) val alarm : int -> int (** Schedule a [SIGALRM] signal after the given number of seconds. *) Index: otherlibs/unix/gmtime.c =================================================================== --- otherlibs/unix/gmtime.c (revision 1) +++ otherlibs/unix/gmtime.c (revision 5) @@ -13,6 +13,7 @@ /* $Id: gmtime.c,v 1.17 2005/03/24 17:20:53 doligez Exp $ */ +#define _XOPEN_SOURCE #include #include #include @@ -20,7 +21,6 @@ #include "unixsupport.h" #include #include - static value alloc_tm(struct tm *tm) { value res; @@ -59,6 +59,56 @@ #ifdef HAS_MKTIME +CAMLprim value unix_strptime(value f,value d) { + + char *fmt = String_val(f); + char *data = String_val(d); + char *err; + struct tm tm; + err = strptime(data,fmt,&tm); + if (err == NULL) unix_error(EINVAL, "strptime", Nothing); + return alloc_tm(&tm); +} + +CAMLprim value unix_asctime(value t) { + struct tm tm; + char *res; + + tm.tm_sec = Int_val(Field(t, 0)); + tm.tm_min = Int_val(Field(t, 1)); + tm.tm_hour = Int_val(Field(t, 2)); + tm.tm_mday = Int_val(Field(t, 3)); + tm.tm_mon = Int_val(Field(t, 4)); + tm.tm_year = Int_val(Field(t, 5)); + tm.tm_wday = Int_val(Field(t, 6)); + tm.tm_yday = Int_val(Field(t, 7)); + tm.tm_isdst = tm.tm_isdst = Bool_val(Field(t, 8)); + res = asctime(&tm); + if (res == NULL) unix_error(EINVAL, "asctime", Nothing); + return caml_copy_string(asctime(&tm)); + +} + +CAMLprim value unix_strftime(value f,value t) { + + char *fmt = String_val(f); + struct tm tm; + char *output = (char *)caml_alloc_string(255); + + tm.tm_sec = Int_val(Field(t, 0)); + tm.tm_min = Int_val(Field(t, 1)); + tm.tm_hour = Int_val(Field(t, 2)); + tm.tm_mday = Int_val(Field(t, 3)); + tm.tm_mon = Int_val(Field(t, 4)); + tm.tm_year = Int_val(Field(t, 5)); + tm.tm_wday = Int_val(Field(t, 6)); + tm.tm_yday = Int_val(Field(t, 7)); + tm.tm_isdst = tm.tm_isdst = Bool_val(Field(t, 8)); + strftime(output,255,fmt,&tm); + return caml_copy_string(output); + +} + CAMLprim value unix_mktime(value t) { struct tm tm; @@ -89,6 +139,15 @@ #else +CAMLprim value unix_strptime(value f,value d) +{ invalid_argument("strptime not implemented"); } + +CAMLprim value unix_asctime(value t) { +{ invalid_argument("asctime not implemented"); } + +CAMLprim value unix_strftime(value f,value t) { +{ invalid_argument("strftime not implemented"); } + CAMLprim value unix_mktime(value t) { invalid_argument("mktime not implemented"); } Index: otherlibs/unix/unix.ml =================================================================== --- otherlibs/unix/unix.ml (revision 1) +++ otherlibs/unix/unix.ml (revision 5) @@ -337,6 +337,9 @@ external gettimeofday : unit -> float = "unix_gettimeofday" external gmtime : float -> tm = "unix_gmtime" external localtime : float -> tm = "unix_localtime" +external strptime : string -> string -> tm = "unix_strptime" +external asctime : tm -> string = "unix_asctime" +external strftime : string -> tm -> string = "unix_strftime" external mktime : tm -> float * tm = "unix_mktime" external alarm : int -> int = "unix_alarm" external sleep : int -> unit = "unix_sleep" Index: otherlibs/threads/unix.ml =================================================================== --- otherlibs/threads/unix.ml (revision 1) +++ otherlibs/threads/unix.ml (revision 5) @@ -457,6 +457,9 @@ external gettimeofday : unit -> float = "unix_gettimeofday" external gmtime : float -> tm = "unix_gmtime" external localtime : float -> tm = "unix_localtime" +external strptime : string -> string -> tm = "unix_strptime" +external asctime : tm -> string = "unix_asctime" +external strftime : string -> tm -> string = "unix_strftime" external mktime : tm -> float * tm = "unix_mktime" external alarm : int -> int = "unix_alarm" ------=_Part_38172_13046211.1131422963829--