1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Objective Caml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
|
|
|
/* under the terms of the GNU Library General Public License. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-05-08 08:18:32 -07:00
|
|
|
#include <mlvalues.h>
|
|
|
|
#include <alloc.h>
|
1996-06-25 02:55:26 -07:00
|
|
|
#include <memory.h>
|
1996-09-04 07:15:31 -07:00
|
|
|
#include "unixsupport.h"
|
1995-05-08 08:18:32 -07:00
|
|
|
#include <time.h>
|
1999-10-14 06:52:13 -07:00
|
|
|
#include <errno.h>
|
1995-05-08 08:18:32 -07:00
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static value alloc_tm(struct tm *tm)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
|
|
|
value res;
|
1998-10-26 11:19:32 -08:00
|
|
|
res = alloc_small(9, 0);
|
1995-05-08 08:18:32 -07:00
|
|
|
Field(res,0) = Val_int(tm->tm_sec);
|
|
|
|
Field(res,1) = Val_int(tm->tm_min);
|
|
|
|
Field(res,2) = Val_int(tm->tm_hour);
|
|
|
|
Field(res,3) = Val_int(tm->tm_mday);
|
|
|
|
Field(res,4) = Val_int(tm->tm_mon);
|
|
|
|
Field(res,5) = Val_int(tm->tm_year);
|
|
|
|
Field(res,6) = Val_int(tm->tm_wday);
|
|
|
|
Field(res,7) = Val_int(tm->tm_yday);
|
|
|
|
Field(res,8) = tm->tm_isdst ? Val_true : Val_false;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value unix_gmtime(value t) /* ML */
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
1995-08-08 06:37:34 -07:00
|
|
|
time_t clock;
|
1999-10-14 06:52:13 -07:00
|
|
|
struct tm * tm;
|
1998-06-09 08:06:39 -07:00
|
|
|
clock = (time_t) Double_val(t);
|
1999-10-14 06:52:13 -07:00
|
|
|
tm = gmtime(&clock);
|
|
|
|
if (tm == NULL) unix_error(EINVAL, "gmtime", Nothing);
|
|
|
|
return alloc_tm(tm);
|
1995-05-08 08:18:32 -07:00
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value unix_localtime(value t) /* ML */
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
1995-08-08 06:37:34 -07:00
|
|
|
time_t clock;
|
1999-10-14 06:52:13 -07:00
|
|
|
struct tm * tm;
|
1998-06-09 08:06:39 -07:00
|
|
|
clock = (time_t) Double_val(t);
|
1999-10-14 06:52:13 -07:00
|
|
|
tm = localtime(&clock);
|
|
|
|
if (tm == NULL) unix_error(EINVAL, "localtime", Nothing);
|
|
|
|
return alloc_tm(tm);
|
1995-05-08 08:18:32 -07:00
|
|
|
}
|
1996-06-25 02:55:26 -07:00
|
|
|
|
|
|
|
#ifdef HAS_MKTIME
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value unix_mktime(value t) /* ML */
|
1996-06-25 02:55:26 -07:00
|
|
|
{
|
|
|
|
struct tm tm;
|
|
|
|
time_t clock;
|
|
|
|
value res;
|
1998-06-09 08:06:39 -07:00
|
|
|
value tmval = Val_unit, clkval = Val_unit;
|
1996-06-25 02:55:26 -07:00
|
|
|
|
1998-06-09 08:06:39 -07:00
|
|
|
Begin_roots2(tmval, clkval);
|
1997-05-26 10:16:31 -07:00
|
|
|
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));
|
1998-04-30 06:30:03 -07:00
|
|
|
tm.tm_isdst = -1; /* tm.tm_isdst = Bool_val(Field(t, 8)); */
|
1997-05-26 10:16:31 -07:00
|
|
|
clock = mktime(&tm);
|
2001-02-22 00:59:19 -08:00
|
|
|
if (clock == (time_t) -1) unix_error(ERANGE, "mktime", Nothing);
|
1997-05-26 10:16:31 -07:00
|
|
|
tmval = alloc_tm(&tm);
|
1998-06-09 08:06:39 -07:00
|
|
|
clkval = copy_double((double) clock);
|
1998-10-26 11:19:32 -08:00
|
|
|
res = alloc_small(2, 0);
|
1998-06-09 08:06:39 -07:00
|
|
|
Field(res, 0) = clkval;
|
1997-05-26 10:16:31 -07:00
|
|
|
Field(res, 1) = tmval;
|
|
|
|
End_roots ();
|
1996-06-25 02:55:26 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value unix_mktime(value t) { invalid_argument("mktime not implemented"); }
|
1996-06-25 02:55:26 -07:00
|
|
|
|
|
|
|
#endif
|