From: Dave Benjamin <dave@ramenlabs.com>
To: Luca de Alfaro <luca@dealfaro.org>
Cc: Inria Ocaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Ocaml can't convert a GMT time into a float!
Date: Sat, 06 Mar 2010 20:27:32 -0700 [thread overview]
Message-ID: <4B931D24.6050700@ramenlabs.com> (raw)
In-Reply-To: <28fa90931003060153y5c4d1590j97fcc0267ee2dc1e@mail.gmail.com>
Luca de Alfaro wrote:
> I need to convert a time, expressed in yyyy/mm/dd hh:mm:ss form, into a
> floating point.
> The conversion has to be done in GMT, but the real point is, the
> conversion must NOT change due to daylight savings time.
>
> Ocaml seems to have only one conversion function, however: Unix.mktime,
> which takes a time, and makes a float in the local time zone.
>
> No problem, I thought: I will simply add 3600 if the conversion result
> tells me that dst is active (and then convert for the difference between
> GMT and winter time).
> NO! This does not work! Look at the two conversions below. The tmrec
> differs by one hour.
> However, the two floating point numbers returned are identical, and
> tm_isdst is set to true in both cases!
>
> This means that I have no way of using the standard libraries to convert
> a time expressed in yyyy/mm/ss hh:mm:ss to a float!
You are absolutely right, and I unfortunately did not notice this
subtlety when I wrote the XmlRpcDateTime module that is part of
XmlRpc-Light, so this means there is a bug in
XmlRpcDateTime.to_unixfloat_utc on systems with time zones that observe
daylight savings. I did not notice the problem because I live in
Arizona, one of only two states in the US that do not observe daylight
savings!
The culprit can be seen here in the C implementation of Unix.mktime:
CAMLprim value unix_mktime(value t)
{
struct tm tm;
time_t clock;
value res;
value tmval = Val_unit, clkval = Val_unit;
Begin_roots2(tmval, clkval);
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 = -1; /* tm.tm_isdst = Bool_val(Field(t, 8)); */
clock = mktime(&tm);
if (clock == (time_t) -1) unix_error(ERANGE, "mktime", Nothing);
tmval = alloc_tm(&tm);
clkval = copy_double((double) clock);
res = alloc_small(2, 0);
Field(res, 0) = clkval;
Field(res, 1) = tmval;
End_roots ();
return res;
}
The field tm.tm_isdst is not really a boolean from C's perspective. It
can be one of *three* states: positive for DST, zero for non-DST, and
negative to query the system timezone database for the value. It looks
like at one point Unix.mktime was written to take the value you gave it,
but this was commented out and the value was fixed to -1. This is why it
uses the time zone's daylight savings correction regardless of what you
pass in.
Would it be possible to have a new function in the standard library with
the commented-out behavior instead? As it stands now I don't see any
reasonable way to get a UTC float from a Unix.tm.
As far as XmlRpc-Light is concerned, I will probably rewrite this
function in terms of Netdate, since Ocamlnet is already one of my
dependencies. Apologies to anyone who is affected by this bug
(hopefully, no one).
Thanks,
Dave
next prev parent reply other threads:[~2010-03-07 3:27 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-03-06 9:53 Luca de Alfaro
2010-03-06 11:09 ` [Caml-list] " blue storm
2010-03-06 12:47 ` Erik de Castro Lopo
2010-03-06 13:19 ` Yaron Minsky
2010-03-07 3:27 ` Dave Benjamin [this message]
2010-03-07 18:05 ` Luca de Alfaro
2010-03-07 18:12 ` Luca de Alfaro
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4B931D24.6050700@ramenlabs.com \
--to=dave@ramenlabs.com \
--cc=caml-list@inria.fr \
--cc=luca@dealfaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox