Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add %z (timezone) handling in Date code. #34

Closed
wants to merge 1 commit into from

2 participants

@alokmenghrajani

This diff does not add total timezone handling (i.e. it doesn't allow you to parse the timezone from a string and/or mess around with timezone values). The main purpose of this diff is to have the ability to tell which timezone the server or client is running in, which is sometimes useful.

Alok Menghrajani Add %z (timezone) handling in Date code.
This diff does not add total timezone handling (i.e. it doesn't allow you to parse the timezone from a string and/or mess around with timezone values). The main purpose of this diff is to have the ability to tell which timezone the server or client is running in, which is sometimes useful.
90e2f5f
@alokmenghrajani

Note: I might have hit some bug with the bsl. If I try to make local_timezone_offset a nullary function, I end up with weird warnings:

opabsl/jsbsl/opabslgen_bslTime.js.pp:181: ERROR - Parse error. invalid return
return t.getTimezoneOffset();

(see https://gist.github.com/1734181).

@BourgerieQuentin

Alok, you have just missing ##args() on bsl register declaration.
Then I also correct Opa compile error (on date_private.opa)

see https://gist.github.com/1752783 it's just your https://gist.github.com/1734181 patch with corrections.

I commited the patch on our repository. (Sure I authored to you)

Thanks

@alokmenghrajani
@BourgerieQuentin
#register x \ y : 'a, 'b -> 'c

means "Register as 'x' the javascript function 'y' and it should respect the given signature ''a, 'b -> 'c'"
i.e is just like an alias.

This case is usefull when the function who want register is already defined.

In the case of 'now' indeed the implementation is just bellow the register declaration. And can be replaced by

##register now : -> time_t
##args()
{
...
}

2 -

##register x : 'a, 'b -> 'c
##args(a, b)
{
//code
}

means "Register as 'x' a javascript function with two arguments "a, b" and the following body"

Note : this way to register make some check (ex: cheking if the arity match).
And generates skeleton of javascript function.

I hope my explanations are clear enough.

Quentin.

@alokmenghrajani
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 4, 2012
  1. Add %z (timezone) handling in Date code.

    Alok Menghrajani authored
    This diff does not add total timezone handling (i.e. it doesn't allow you to parse the timezone from a string and/or mess around with timezone values). The main purpose of this diff is to have the ability to tell which timezone the server or client is running in, which is sometimes useful.
This page is out of date. Refresh to see the latest.
View
8 libbase/time.ml
@@ -105,6 +105,14 @@ let local_wday time = (localtime time).Unix.tm_wday
let local_yday time = (localtime time).Unix.tm_yday
let local_isdst time = (localtime time).Unix.tm_isdst
+let local_timezone_offset () =
+ let t = Unix.time() in
+ let gmt = Unix.gmtime(t) in
+ let local = Unix.localtime(t) in
+ let (gmt_s, _) = Unix.mktime(gmt) in
+ let (local_s, _) = Unix.mktime(local) in
+ int_of_float((gmt_s -. local_s) /. 60.0);;
+
let mktime ~year ~month ~day ~h ~min ~sec ~ms =
let res =
of_unix_time (
View
1  libbase/time.mli
@@ -52,6 +52,7 @@ val local_year : t -> int
val local_wday : t -> int
val local_yday : t -> int
val local_isdst : t -> bool
+val local_timezone_offset : unit -> int
val mktime : year:int -> month:int -> day:int -> h:int -> min:int -> sec:int -> ms:int -> t
View
7 opabsl/jsbsl/bslTime.js
@@ -159,6 +159,13 @@ function now()
return t.getHours();
}
+##register local_timezone_offset : time_t -> int
+ ##args(f)
+{
+ var t = new Date(); t.setTime(f)
+ return t.getTimezoneOffset();
+}
+
##register local_mday : time_t -> int
##args(f)
{
View
3  opabsl/mlbsl/bslTime.ml
@@ -83,6 +83,9 @@
##register local_hour : time_t -> int
let local_hour t = Time.local_hour (wrap t)
+ ##register local_timezone_offset : time_t -> int
+ let local_timezone_offset _ = Time.local_timezone_offset()
+
##register local_mday : time_t -> int
let local_mday t = Time.local_mday (wrap t)
View
27 stdlib/core/date/date.opa
@@ -424,6 +424,29 @@ type time_t = external
get_hour : Date.date -> int = Date_private.time_local_hour
/**
+ * Returns the local timezone (+hhmm or -hhmm).
+ *
+ * On the server side, this will be a constant. On the client side
+ * it will depend on whatever the user has set on their system.
+ *
+ * @return The local timezone (+hhmm or -hhmm).
+ **/
+ get_local_timezone : -> string = -> (
+ t : Date.date = Date.now()
+ delta : int = Date_private.time_local_timezone_offset(t)
+ // delta is the timezone in minutes, so we need to convert it into
+ // +hhmm or -hhmm. If delta is positive, the timezone is -hhmm. Otherwise
+ // the timezone is +hhmm.
+ h : int = Int.abs(delta) / 60
+ m : int = mod(Int.abs(delta), 60)
+ s : string = if delta > 0 then "-" else "+"
+ p = {pad_with_zeros}
+ h_str : string = Date_private.ToString.pad(h, p, p, 2)
+ m_str : string = Date_private.ToString.pad(m, p, p, 2)
+ "{s}{h_str}{m_str}"
+ )
+
+ /**
* Returns the weekday represented by this date interpreted in the local
* time zone.
* If you need to completely decompose a date consider using {!Date.to_human_readable}.
@@ -833,6 +856,7 @@ type time_t = external
* - [%w] day of week (0..6); 0 is Sunday
* - [%y] last two digits of year (00..99)
* - [%Y] year (ex. 2010)
+ * - [%z] +hhmm numeric timezone (e.g., -0400)
*
* By default, numeric fields are padded with zeroes. The following optional flags
* may follow `%':
@@ -849,7 +873,6 @@ type time_t = external
* - [%U] week number of year with Sunday as first day of week (00..53)
* - [%V] week number of year with Monday as first day of week (01..53)
* - [%W] week number of year with Monday as first day of week (00..53)
- * - [%z] +hhmm numeric timezone (e.g., -0400)
* - [%:z] +hh:mm numeric timezone (e.g., -04:00)
*
* Also the padding directives are not supported:
@@ -975,7 +998,7 @@ type time_t = external
*
* For the purpose of parsing there are few differences in the interpretations
* of the format, compared to the format used for printing, see {!Date.try_generate_printer}:
- * - the directives '%C', '%u' and '%w' are not recognized,
+ * - the directives '%C', '%u', '%w' and '%z' are not recognized,
* - a non-empty sequence of spaces in the [format] can be matched by any (non-empty)
* sequence of spaces in the input.
* - the padding flags ('_', '-' and '0') are ignored and all numerical values
View
5 stdlib/core/date/date_private.opa
@@ -105,6 +105,9 @@ import stdlib.core.parser
time_local_hour : Date.date -> int =
date_in(%%BslTime.local_hour%% : time_t -> int) // Hours 0..23
+ time_local_timezone_offset : Date.date -> int =
+ date_in(%%BslTime.local_timezone_offset%% : time_t -> int)
+
time_local_mday : Date.date -> int =
date_in(%%BslTime.local_mday%% : time_t -> int) // Day of month 1..31
@@ -181,6 +184,7 @@ import stdlib.core.parser
, ("x", true, ((p, d) -> pad(Date.get_msec(d), p, {pad_with_zeros}, 3)))
, ("y", false, ((_, d) -> pad(mod(Date.get_year(d), 100), {pad_with_zeros}, {pad_with_zeros}, 2)))
, ("Y", true, ((p, d) -> pad(Date.get_year(d), p, {pad_with_spaces}, 4)))
+ , ("z", true, ((_, _) -> Date.get_local_timezone()))
]
padding_flag_parser = parser
@@ -328,6 +332,7 @@ import stdlib.core.parser
, ("x", parse_num(v -> d -> {d with ms = v}))
, ("y", parse_num(v -> d -> {d with year=if v < 70 then 2000 + v else 1900 + v}))
, ("Y", parse_num(v -> d -> {d with year=v}))
+// , ("z", *not supported for parsing*
]
parse_directive_with((d, p)) =
Something went wrong with that request. Please try again.