* Ocaml can't convert a GMT time into a float! @ 2010-03-06 9:53 Luca de Alfaro 2010-03-06 11:09 ` [Caml-list] " blue storm ` (2 more replies) 0 siblings, 3 replies; 7+ messages in thread From: Luca de Alfaro @ 2010-03-06 9:53 UTC (permalink / raw) To: Inria Ocaml Mailing List [-- Attachment #1: Type: text/plain, Size: 1696 bytes --] 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! This is a basic operation, and it is a big let down that there is no such library function in Ocaml. I will have to write it from basic principles. Anybody has a better suggestion? Many thanks, Luca # let tmrec = {tm_sec = 38; tm_min = 57; tm_hour = 3; tm_mday = 11; tm_mon = 2; tm_year = 107; tm_wday = 0; tm_yday = 0; tm_isdst = false};; # Unix.mktime tmrec;; - : float * Unix.tm = (1173610658., {tm_sec = 38; tm_min = 57; tm_hour = 3; tm_mday = 11; tm_mon = 2; tm_year = 107; tm_wday = 0; tm_yday = 69; tm_isdst = true}) # let tmrec = {tm_sec = 38; tm_min = 57; tm_hour = 2; tm_mday = 11; tm_mon = 2; tm_year = 107; tm_wday = 0; tm_yday = 0; tm_isdst = false};; # Unix.mktime tmrec;; - : float * Unix.tm = (1173610658., {tm_sec = 38; tm_min = 57; tm_hour = 3; tm_mday = 11; tm_mon = 2; tm_year = 107; tm_wday = 0; tm_yday = 69; tm_isdst = true}) [-- Attachment #2: Type: text/html, Size: 1814 bytes --] ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] Ocaml can't convert a GMT time into a float! 2010-03-06 9:53 Ocaml can't convert a GMT time into a float! Luca de Alfaro @ 2010-03-06 11:09 ` 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 2 siblings, 1 reply; 7+ messages in thread From: blue storm @ 2010-03-06 11:09 UTC (permalink / raw) To: Luca de Alfaro; +Cc: Inria Ocaml Mailing List [-- Attachment #1: Type: text/plain, Size: 243 bytes --] If you want a fine-grained timezones and daylight saving times handling, you may use a dedicated library for date and time, such that Calendar (wich is used by PG'OCaml to represent SQL date/time values) : http://calendar.forge.ocamlcore.org/ [-- Attachment #2: Type: text/html, Size: 303 bytes --] ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] Ocaml can't convert a GMT time into a float! 2010-03-06 11:09 ` [Caml-list] " blue storm @ 2010-03-06 12:47 ` Erik de Castro Lopo 0 siblings, 0 replies; 7+ messages in thread From: Erik de Castro Lopo @ 2010-03-06 12:47 UTC (permalink / raw) To: caml-list blue storm wrote: > If you want a fine-grained timezones and daylight saving times handling, you > may use a dedicated library for date and time, such that Calendar (wich is > used by PG'OCaml to represent SQL date/time values) : > http://calendar.forge.ocamlcore.org/ I really like the way the Netdate module handles times and dates. Erik -- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/ ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] Ocaml can't convert a GMT time into a float! 2010-03-06 9:53 Ocaml can't convert a GMT time into a float! Luca de Alfaro 2010-03-06 11:09 ` [Caml-list] " blue storm @ 2010-03-06 13:19 ` Yaron Minsky 2010-03-07 3:27 ` Dave Benjamin 2 siblings, 0 replies; 7+ messages in thread From: Yaron Minsky @ 2010-03-06 13:19 UTC (permalink / raw) To: Luca de Alfaro; +Cc: Inria Ocaml Mailing List [-- Attachment #1: Type: text/plain, Size: 2219 bytes --] The TZ module in Core could also be helpful here. y On Sat, Mar 6, 2010 at 4:53 AM, Luca de Alfaro <luca@dealfaro.org> 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! > > This is a basic operation, and it is a big let down that there is no such > library function in Ocaml. > I will have to write it from basic principles. > Anybody has a better suggestion? > > Many thanks, > > Luca > > # let tmrec = {tm_sec = 38; tm_min = 57; tm_hour = 3; tm_mday = 11; tm_mon > = 2; tm_year = 107; tm_wday = 0; tm_yday = 0; tm_isdst = false};; > # Unix.mktime tmrec;; > - : float * Unix.tm = > (1173610658., > {tm_sec = 38; tm_min = 57; tm_hour = 3; tm_mday = 11; tm_mon = 2; tm_year > = 107; tm_wday = 0; tm_yday = 69; tm_isdst = true}) > > # let tmrec = {tm_sec = 38; tm_min = 57; tm_hour = 2; tm_mday = 11; tm_mon > = 2; tm_year = 107; tm_wday = 0; tm_yday = 0; tm_isdst = false};; > # Unix.mktime tmrec;; > - : float * Unix.tm = > (1173610658., > {tm_sec = 38; tm_min = 57; tm_hour = 3; tm_mday = 11; tm_mon = 2; > tm_year = 107; tm_wday = 0; tm_yday = 69; tm_isdst = true}) > > > _______________________________________________ > 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 > > [-- Attachment #2: Type: text/html, Size: 2835 bytes --] ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] Ocaml can't convert a GMT time into a float! 2010-03-06 9:53 Ocaml can't convert a GMT time into a float! Luca de Alfaro 2010-03-06 11:09 ` [Caml-list] " blue storm 2010-03-06 13:19 ` Yaron Minsky @ 2010-03-07 3:27 ` Dave Benjamin 2010-03-07 18:05 ` Luca de Alfaro 2 siblings, 1 reply; 7+ messages in thread From: Dave Benjamin @ 2010-03-07 3:27 UTC (permalink / raw) To: Luca de Alfaro; +Cc: Inria Ocaml Mailing List 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 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] Ocaml can't convert a GMT time into a float! 2010-03-07 3:27 ` Dave Benjamin @ 2010-03-07 18:05 ` Luca de Alfaro 2010-03-07 18:12 ` Luca de Alfaro 0 siblings, 1 reply; 7+ messages in thread From: Luca de Alfaro @ 2010-03-07 18:05 UTC (permalink / raw) To: Dave Benjamin; +Cc: Inria Ocaml Mailing List [-- Attachment #1: Type: text/plain, Size: 3924 bytes --] Thanks to all! I see, I am glad there are good alternative libraries! For my problem, I preferred to avoid having dependence on one more library for only 10 lines of code, so I wrote some code to do the conversion. It works only for dates after 1970, and it is somewhat inelegant; it is here: http://wghstrfg.blogspot.com/2010/03/i-hate-daylight-savings-time.html If you want to know why this bug drove me crazy for a couple of evenings, you can read this blog post<http://wghstrfg.blogspot.com/2010/03/i-hate-daylight-savings-time.html> . Many thanks, and I am glad my email helped Dave. Daylight savings time is a huge headache for anyone working on historically-timestamped data. All the best, Luca On Sat, Mar 6, 2010 at 7:27 PM, Dave Benjamin <dave@ramenlabs.com> wrote: > 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 > [-- Attachment #2: Type: text/html, Size: 4677 bytes --] ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] Ocaml can't convert a GMT time into a float! 2010-03-07 18:05 ` Luca de Alfaro @ 2010-03-07 18:12 ` Luca de Alfaro 0 siblings, 0 replies; 7+ messages in thread From: Luca de Alfaro @ 2010-03-07 18:12 UTC (permalink / raw) To: Dave Benjamin; +Cc: Inria Ocaml Mailing List [-- Attachment #1: Type: text/plain, Size: 4229 bytes --] Sorry: wrong link. The code is here: http://luca.dealfaro.org/converting-gmt-time-into-unix-time-in-ocaml On Sun, Mar 7, 2010 at 10:05 AM, Luca de Alfaro <luca@dealfaro.org> wrote: > Thanks to all! > > I see, I am glad there are good alternative libraries! > For my problem, I preferred to avoid having dependence on one more library > for only 10 lines of code, so I wrote some code to do the conversion. It > works only for dates after 1970, and it is somewhat inelegant; it is here: > http://wghstrfg.blogspot.com/2010/03/i-hate-daylight-savings-time.html > > If you want to know why this bug drove me crazy for a couple of evenings, > you can read this blog post<http://wghstrfg.blogspot.com/2010/03/i-hate-daylight-savings-time.html> > . > > Many thanks, and I am glad my email helped Dave. > > Daylight savings time is a huge headache for anyone working on > historically-timestamped data. > > All the best, > > Luca > > > On Sat, Mar 6, 2010 at 7:27 PM, Dave Benjamin <dave@ramenlabs.com> wrote: > >> 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 >> > > [-- Attachment #2: Type: text/html, Size: 5323 bytes --] ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2010-03-07 18:12 UTC | newest] Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2010-03-06 9:53 Ocaml can't convert a GMT time into a float! 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 2010-03-07 18:05 ` Luca de Alfaro 2010-03-07 18:12 ` Luca de Alfaro
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox