From 8492b32b68c817deb9556e377b0a1b7fbdef22bf Mon Sep 17 00:00:00 2001 From: Patrick Hurst Date: Thu, 5 Dec 2013 11:36:06 -0500 Subject: Add basic year/month/day/hour/minute/second <-> time functions. --- include/urweb/urweb_cpp.h | 7 +++++++ lib/ur/basis.urs | 9 +++++++++ src/c/urweb.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/include/urweb/urweb_cpp.h b/include/urweb/urweb_cpp.h index 4779b95a..be9f9d16 100644 --- a/include/urweb/urweb_cpp.h +++ b/include/urweb/urweb_cpp.h @@ -262,6 +262,13 @@ uw_Basis_int uw_Basis_diffInSeconds(struct uw_context *, uw_Basis_time, uw_Basis uw_Basis_int uw_Basis_toSeconds(struct uw_context *, uw_Basis_time); uw_Basis_int uw_Basis_diffInMilliseconds(struct uw_context *, uw_Basis_time, uw_Basis_time); uw_Basis_int uw_Basis_toMilliseconds(struct uw_context *, uw_Basis_time); +uw_Basis_time uw_Basis_fromDatetime(struct uw_context *, uw_Basis_int, uw_Basis_int, uw_Basis_int, uw_Basis_int, uw_Basis_int, uw_Basis_int); +uw_Basis_int uw_Basis_datetimeYear(struct uw_context *, uw_Basis_time); +uw_Basis_int uw_Basis_datetimeMonth(struct uw_context *, uw_Basis_time); +uw_Basis_int uw_Basis_datetimeDay(struct uw_context *, uw_Basis_time); +uw_Basis_int uw_Basis_datetimeHour(struct uw_context *, uw_Basis_time); +uw_Basis_int uw_Basis_datetimeMinute(struct uw_context *, uw_Basis_time); +uw_Basis_int uw_Basis_datetimeSecond(struct uw_context *, uw_Basis_time); extern const uw_Basis_time uw_Basis_minTime; void uw_register_transactional(struct uw_context *, void *data, uw_callback commit, uw_callback rollback, uw_callback_with_retry free); diff --git a/lib/ur/basis.urs b/lib/ur/basis.urs index 4931c97a..804f15b9 100644 --- a/lib/ur/basis.urs +++ b/lib/ur/basis.urs @@ -167,6 +167,15 @@ val diffInMilliseconds : time -> time -> int val timef : string -> time -> string (* Uses strftime() format string *) val readUtc : string -> option time +(* Takes a year, month, day, hour, minute, second. *) +val fromDatetime : int -> int -> int -> int -> int -> int -> time +val datetimeYear : time -> int +val datetimeMonth : time -> int +val datetimeDay : time -> int +val datetimeHour : time -> int +val datetimeMinute: time -> int +val datetimeSecond : time -> int + (** * Encryption *) diff --git a/src/c/urweb.c b/src/c/urweb.c index 8bd5ada9..cb71aa15 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -3851,6 +3851,51 @@ uw_Basis_int uw_Basis_toSeconds(uw_context ctx, uw_Basis_time tm) { return tm.seconds; } +uw_Basis_time uw_Basis_fromDatetime(uw_context ctx, uw_Basis_int year, uw_Basis_int month, uw_Basis_int day, uw_Basis_int hour, uw_Basis_int minute, uw_Basis_int second) { + struct tm tm = { .tm_year = year - 1900, .tm_mon = month, .tm_mday = day, + .tm_hour = hour, .tm_min = minute, .tm_sec = second }; + uw_Basis_time r = { timegm(&tm) }; + return r; +} + +uw_Basis_int uw_Basis_datetimeYear(uw_context ctx, uw_Basis_time time) { + struct tm tm; + gmtime_r(&time.seconds, &tm); + return tm.tm_year + 1900; +} + +uw_Basis_int uw_Basis_datetimeMonth(uw_context ctx, uw_Basis_time time) { + struct tm tm; + gmtime_r(&time.seconds, &tm); + return tm.tm_mon; +} + +uw_Basis_int uw_Basis_datetimeDay(uw_context ctx, uw_Basis_time time) { + struct tm tm; + gmtime_r(&time.seconds, &tm); + return tm.tm_mday; +} + +uw_Basis_int uw_Basis_datetimeHour(uw_context ctx, uw_Basis_time time) { + struct tm tm; + gmtime_r(&time.seconds, &tm); + return tm.tm_hour; +} + +uw_Basis_int uw_Basis_datetimeMinute(uw_context ctx, uw_Basis_time time) { + struct tm tm; + gmtime_r(&time.seconds, &tm); + return tm.tm_min; +} + +uw_Basis_int uw_Basis_datetimeSecond(uw_context ctx, uw_Basis_time time) { + struct tm tm; + gmtime_r(&time.seconds, &tm); + return tm.tm_sec; +} + + + void *uw_get_global(uw_context ctx, char *name) { int i; -- cgit v1.2.3 From 5892c59cc4dd947692ff1503b39ab7dc4e02037b Mon Sep 17 00:00:00 2001 From: Patrick Hurst Date: Thu, 5 Dec 2013 11:36:46 -0500 Subject: Basic datetime library. --- Makefile.am | 13 ++++++------- lib/ur/datetime.ur | 26 ++++++++++++++++++++++++++ lib/ur/datetime.urs | 12 ++++++++++++ 3 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 lib/ur/datetime.ur create mode 100644 lib/ur/datetime.urs diff --git a/Makefile.am b/Makefile.am index d626c267..652296ea 100644 --- a/Makefile.am +++ b/Makefile.am @@ -95,13 +95,12 @@ if USE_EMACS endif uninstall-local-main: - rm -f $(DESTDIR)$(BIN)/urweb \ - $(DESTDIR)$(LIB_UR)/basis.urs $(DESTDIR)$(LIB_UR)/char.urs $(DESTDIR)$(LIB_UR)/listPair.urs $(DESTDIR)$(LIB_UR)/list.urs \ - $(DESTDIR)$(LIB_UR)/monad.urs $(DESTDIR)$(LIB_UR)/option.urs $(DESTDIR)$(LIB_UR)/string.urs $(DESTDIR)$(LIB_UR)/top.urs \ - $(DESTDIR)$(LIB_UR)/char.ur $(DESTDIR)$(LIB_UR)/listPair.ur $(DESTDIR)$(LIB_UR)/list.ur \ - $(DESTDIR)$(LIB_UR)/monad.ur $(DESTDIR)$(LIB_UR)/option.ur $(DESTDIR)$(LIB_UR)/string.ur $(DESTDIR)$(LIB_UR)/top.ur \ - $(DESTDIR)$(LIB_JS)/urweb.js \ - $(DESTDIR)$(INCLUDE)/config.h $(DESTDIR)$(INCLUDE)/queue.h $(DESTDIR)$(INCLUDE)/request.h $(DESTDIR)$(INCLUDE)/types.h \ + rm -f $(DESTDIR)$(BIN)/urweb $(DESTDIR)$(LIB_UR)/basis.urs $(DESTDIR)$(LIB_UR)/char.urs $(DESTDIR)$(LIB_UR)/datetime.urs \ + $(DESTDIR)$(LIB_UR)/listPair.urs \ $(DESTDIR)$(LIB_UR)/list.urs $(DESTDIR)$(LIB_UR)/monad.urs \ + $(DESTDIR)$(LIB_UR)/option.urs $(DESTDIR)$(LIB_UR)/string.urs $(DESTDIR)$(LIB_UR)/top.urs $(DESTDIR)$(LIB_UR)/char.ur \ + $(DESTDIR)$(LIB_UR)/datetime.ur $(DESTDIR)$(LIB_UR)/listPair.ur $(DESTDIR)$(LIB_UR)/list.ur $(DESTDIR)$(LIB_UR)/monad.ur \ + $(DESTDIR)$(LIB_UR)/option.ur $(DESTDIR)$(LIB_UR)/string.ur $(DESTDIR)$(LIB_UR)/top.ur $(DESTDIR)$(LIB_JS)/urweb.js \ + $(DESTDIR)$(INCLUDE)/config.h $(DESTDIR)$(INCLUDE)/queue.h $(DESTDIR)$(INCLUDE)/request.h $(DESTDIR)$(INCLUDE)/types.h \ $(DESTDIR)$(INCLUDE)/urweb.h $(DESTDIR)$(INCLUDE)/types_cpp.h $(DESTDIR)$(INCLUDE)/urweb_cpp.h uninstall-local: uninstall-local-main uninstall-emacs diff --git a/lib/ur/datetime.ur b/lib/ur/datetime.ur new file mode 100644 index 00000000..56c29724 --- /dev/null +++ b/lib/ur/datetime.ur @@ -0,0 +1,26 @@ +type datetime = { + Year : int, + Month : int, + Day : int, + Hour : int, + Minute : int, + Second : int +} + +fun toTime dt : time = fromDatetime dt.Year dt.Month dt.Day + dt.Hour dt.Minute dt.Second + +fun fromTime t : datetime = { + Year = datetimeYear t, + Month = datetimeMonth t, + Day = datetimeDay t, + Hour = datetimeHour t, + Minute = datetimeMinute t, + Second = datetimeSecond t +} + +fun datetimef fmt dt : string = timef fmt (toTime dt) + +val now : transaction datetime = + n <- now; + return (fromTime n) diff --git a/lib/ur/datetime.urs b/lib/ur/datetime.urs new file mode 100644 index 00000000..9d99b9e3 --- /dev/null +++ b/lib/ur/datetime.urs @@ -0,0 +1,12 @@ +type datetime = { Year : int, + Month : int, + Day : int, + Hour : int, + Minute : int, + Second : int + } + +val toTime : datetime -> time +val fromTime : time -> datetime +val datetimef : string -> datetime -> string +val now : transaction datetime -- cgit v1.2.3 From 90c266db553a99056e0baac98df05f06c34de4ae Mon Sep 17 00:00:00 2001 From: Patrick Hurst Date: Thu, 5 Dec 2013 11:36:54 -0500 Subject: Day of week functions. --- lib/ur/basis.urs | 1 + lib/ur/datetime.ur | 12 ++++++++++++ lib/ur/datetime.urs | 5 +++++ src/c/urweb.c | 5 +++++ 4 files changed, 23 insertions(+) diff --git a/lib/ur/basis.urs b/lib/ur/basis.urs index 804f15b9..36d2effa 100644 --- a/lib/ur/basis.urs +++ b/lib/ur/basis.urs @@ -175,6 +175,7 @@ val datetimeDay : time -> int val datetimeHour : time -> int val datetimeMinute: time -> int val datetimeSecond : time -> int +val datetimeDayOfWeek : time -> int (** * Encryption *) diff --git a/lib/ur/datetime.ur b/lib/ur/datetime.ur index 56c29724..a633dc83 100644 --- a/lib/ur/datetime.ur +++ b/lib/ur/datetime.ur @@ -7,6 +7,18 @@ type datetime = { Second : int } +datatype day_of_week = Sunday | Monday | Tuesday | Wednesday | Thursday | + Friday | Saturday + +val show = mkShow (fn dow => case dow of + Sunday => "Sunday" + | Monday => "Monday" + | Tuesday => "Tuesday" + | Wednesday => "Wednesday" + | Thursday => "Thursday" + | Friday => "Friday" + | Saturday => "Saturday") + fun toTime dt : time = fromDatetime dt.Year dt.Month dt.Day dt.Hour dt.Minute dt.Second diff --git a/lib/ur/datetime.urs b/lib/ur/datetime.urs index 9d99b9e3..0d8e8c28 100644 --- a/lib/ur/datetime.urs +++ b/lib/ur/datetime.urs @@ -6,6 +6,11 @@ type datetime = { Year : int, Second : int } +datatype day_of_week = Sunday | Monday | Tuesday | Wednesday | Thursday | + Friday | Saturday + +val show : show day_of_week + val toTime : datetime -> time val fromTime : time -> datetime val datetimef : string -> datetime -> string diff --git a/src/c/urweb.c b/src/c/urweb.c index cb71aa15..fb6d28c6 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -3894,6 +3894,11 @@ uw_Basis_int uw_Basis_datetimeSecond(uw_context ctx, uw_Basis_time time) { return tm.tm_sec; } +uw_Basis_int uw_Basis_datetimeDayOfWeek(uw_context ctx, uw_Basis_time time) { + struct tm tm; + gmtime_r(&time.seconds, &tm); + return tm.tm_wday; +} void *uw_get_global(uw_context ctx, char *name) { -- cgit v1.2.3 From b2873bdd07801753460bd6f1b9efcc235a2f0268 Mon Sep 17 00:00:00 2001 From: Patrick Hurst Date: Sat, 7 Dec 2013 21:31:51 -0500 Subject: Add day-of-week/month <-> int conversion functions. --- lib/ur/datetime.ur | 113 +++++++++++++++++++++++++++++++++++++++++++++------- lib/ur/datetime.urs | 33 ++++++++++----- 2 files changed, 121 insertions(+), 25 deletions(-) diff --git a/lib/ur/datetime.ur b/lib/ur/datetime.ur index a633dc83..6d995d89 100644 --- a/lib/ur/datetime.ur +++ b/lib/ur/datetime.ur @@ -1,37 +1,120 @@ -type datetime = { +datatype day_of_week = Sunday | Monday | Tuesday | Wednesday | Thursday | + Friday | Saturday + +val show_day_of_week = mkShow (fn dow => case dow of + Sunday => "Sunday" + | Monday => "Monday" + | Tuesday => "Tuesday" + | Wednesday => "Wednesday" + | Thursday => "Thursday" + | Friday => "Friday" + | Saturday => "Saturday") + +fun dayOfWeekToInt dow = case dow of + Sunday => 0 + | Monday => 1 + | Tuesday => 2 + | Wednesday => 3 + | Thursday => 4 + | Friday => 5 + | Saturday => 6 + +fun intToDayOfWeek i = case i of + 0 => Sunday + | 1 => Monday + | 2 => Tuesday + | 3 => Wednesday + | 4 => Thursday + | 5 => Friday + | 6 => Saturday + | n => error Invalid day of week {[n]} + +val eq_day_of_week = mkEq (fn a b => dayOfWeekToInt a = dayOfWeekToInt b) + + +datatype month = January | February | March | April | May | June | July | + August | September | October | November | December + +val show_month = mkShow (fn m => case m of + January => "January" + | February => "February" + | March => "March" + | April => "April" + | May => "May" + | June => "June" + | July => "July" + | August => "August" + | September => "September" + | October => "October" + | November => "November" + | December => "December") + +type t = { Year : int, - Month : int, + Month : month, Day : int, Hour : int, Minute : int, Second : int } -datatype day_of_week = Sunday | Monday | Tuesday | Wednesday | Thursday | - Friday | Saturday +fun monthToInt m = case m of + January => 0 + | February => 1 + | March => 2 + | April => 3 + | May => 4 + | June => 5 + | July => 6 + | August => 7 + | September => 8 + | October => 9 + | November => 10 + | December => 11 -val show = mkShow (fn dow => case dow of - Sunday => "Sunday" - | Monday => "Monday" - | Tuesday => "Tuesday" - | Wednesday => "Wednesday" - | Thursday => "Thursday" - | Friday => "Friday" - | Saturday => "Saturday") +fun intToMonth i = case i of + 0 => January + | 1 => February + | 2 => March + | 3 => April + | 4 => May + | 5 => June + | 6 => July + | 7 => August + | 8 => September + | 9 => October + | 10 => November + | 11 => December + | n => error Invalid month number {[n]} -fun toTime dt : time = fromDatetime dt.Year dt.Month dt.Day +val eq_month = mkEq (fn a b => monthToInt a = monthToInt b) + + +fun toTime dt : time = fromDatetime dt.Year (monthToInt dt.Month) dt.Day dt.Hour dt.Minute dt.Second fun fromTime t : datetime = { Year = datetimeYear t, - Month = datetimeMonth t, + Month = intToMonth (datetimeMonth t), Day = datetimeDay t, Hour = datetimeHour t, Minute = datetimeMinute t, Second = datetimeSecond t } -fun datetimef fmt dt : string = timef fmt (toTime dt) +fun format fmt dt : string = timef fmt (toTime dt) + +fun dayOfWeek dt : day_of_week = + case datetimeDayOfWeek (toTime dt) of + 0 => Sunday + | 1 => Monday + | 2 => Tuesday + | 3 => Wednesday + | 4 => Thursday + | 5 => Friday + | 6 => Saturday + | n => error Illegal day of week {[n]} + val now : transaction datetime = n <- now; diff --git a/lib/ur/datetime.urs b/lib/ur/datetime.urs index 0d8e8c28..2fc2998b 100644 --- a/lib/ur/datetime.urs +++ b/lib/ur/datetime.urs @@ -1,17 +1,30 @@ -type datetime = { Year : int, - Month : int, - Day : int, - Hour : int, - Minute : int, - Second : int - } - datatype day_of_week = Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday -val show : show day_of_week +datatype month = January | February | March | April | May | June | July | + August | September | October | November | December + + +type datetime = { + Year : int, + Month : month, + Day : int, + Hour : int, + Minute : int, + Second : int +} + +val show_day_of_week : show day_of_week +val show_month : show month +val eq_day_of_week : eq day_of_week +val eq_month : eq month +val dayOfWeekToInt : day_of_week -> int +val intToDayOfWeek : int -> day_of_week +val monthToInt : month -> int +val intToMonth : int -> month val toTime : datetime -> time val fromTime : time -> datetime -val datetimef : string -> datetime -> string +val format : string -> datetime -> string +val dayOfWeek : datetime -> day_of_week val now : transaction datetime -- cgit v1.2.3 From 90da978a70db75e8e9f96e96a41164fceff2b425 Mon Sep 17 00:00:00 2001 From: Patrick Hurst Date: Sun, 8 Dec 2013 03:25:31 -0500 Subject: Rename datetime type to t in the .urs file. --- lib/ur/datetime.urs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/ur/datetime.urs b/lib/ur/datetime.urs index 2fc2998b..26ee795a 100644 --- a/lib/ur/datetime.urs +++ b/lib/ur/datetime.urs @@ -5,7 +5,7 @@ datatype month = January | February | March | April | May | June | July | August | September | October | November | December -type datetime = { +type t = { Year : int, Month : month, Day : int, @@ -23,8 +23,8 @@ val intToDayOfWeek : int -> day_of_week val monthToInt : month -> int val intToMonth : int -> month -val toTime : datetime -> time -val fromTime : time -> datetime -val format : string -> datetime -> string -val dayOfWeek : datetime -> day_of_week -val now : transaction datetime +val toTime : t -> time +val fromTime : time -> t +val format : string -> t -> string +val dayOfWeek : t -> day_of_week +val now : transaction t -- cgit v1.2.3 From d26ce87498bfbdcf1c510d7c0853f8ac8f07314f Mon Sep 17 00:00:00 2001 From: Patrick Hurst Date: Sun, 8 Dec 2013 13:14:58 -0500 Subject: Finish datetime -> t rename --- lib/ur/datetime.ur | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ur/datetime.ur b/lib/ur/datetime.ur index 6d995d89..dbc68da1 100644 --- a/lib/ur/datetime.ur +++ b/lib/ur/datetime.ur @@ -93,7 +93,7 @@ val eq_month = mkEq (fn a b => monthToInt a = monthToInt b) fun toTime dt : time = fromDatetime dt.Year (monthToInt dt.Month) dt.Day dt.Hour dt.Minute dt.Second -fun fromTime t : datetime = { +fun fromTime t : t = { Year = datetimeYear t, Month = intToMonth (datetimeMonth t), Day = datetimeDay t, @@ -116,6 +116,6 @@ fun dayOfWeek dt : day_of_week = | n => error Illegal day of week {[n]} -val now : transaction datetime = +val now : transaction t = n <- now; return (fromTime n) -- cgit v1.2.3 From 5f60bd5eebb24a474d55dc8edf38c8e67c590a17 Mon Sep 17 00:00:00 2001 From: Patrick Hurst Date: Mon, 9 Dec 2013 19:19:12 -0500 Subject: Add datetime functions for adding time intervals. --- lib/ur/datetime.ur | 25 ++++++++++++++++++++++++- lib/ur/datetime.urs | 8 ++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/ur/datetime.ur b/lib/ur/datetime.ur index dbc68da1..676f141f 100644 --- a/lib/ur/datetime.ur +++ b/lib/ur/datetime.ur @@ -102,6 +102,9 @@ fun fromTime t : t = { Second = datetimeSecond t } +val ord_datetime = mkOrd { Lt = fn a b => toTime a < toTime b, + Le = fn a b => toTime a <= toTime b } + fun format fmt dt : string = timef fmt (toTime dt) fun dayOfWeek dt : day_of_week = @@ -115,7 +118,27 @@ fun dayOfWeek dt : day_of_week = | 6 => Saturday | n => error Illegal day of week {[n]} - val now : transaction t = n <- now; return (fromTime n) + +(* Normalize a datetime. This will convert, e.g., January 32nd into February + 1st. *) + +fun normalize dt = fromTime (toTime dt) +fun addToField [nm :: Name] [rest ::: {Type}] [[nm] ~ rest] + (delta : int) (r : $([nm = int] ++ rest)) + : $([nm = int] ++ rest) = + (r -- nm) ++ {nm = r.nm + delta} + + +(* Functions for adding to a datetime. There is no addMonths or addYears since + it's not clear what should be done; what's 1 month after January 31, or 1 + year after February 29th? + + These can't all be defined in terms of addSeconds because of leap seconds. *) + +fun addSeconds n dt = normalize (addToField [#Second] n dt) +fun addMinutes n dt = normalize (addToField [#Minute] n dt) +fun addHours n dt = normalize (addToField [#Hour] n dt) +fun addDays n dt = normalize (addToField [#Day] n dt) diff --git a/lib/ur/datetime.urs b/lib/ur/datetime.urs index 26ee795a..972f86bf 100644 --- a/lib/ur/datetime.urs +++ b/lib/ur/datetime.urs @@ -14,6 +14,8 @@ type t = { Second : int } +val ord_datetime : ord t + val show_day_of_week : show day_of_week val show_month : show month val eq_day_of_week : eq day_of_week @@ -28,3 +30,9 @@ val fromTime : time -> t val format : string -> t -> string val dayOfWeek : t -> day_of_week val now : transaction t +val normalize : t -> t + +val addSeconds : int -> t -> t +val addMinutes : int -> t -> t +val addHours : int -> t -> t +val addDays : int -> t -> t -- cgit v1.2.3 From 1ce3acd70b3527add32015267cc916e920661dbb Mon Sep 17 00:00:00 2001 From: Patrick Hurst Date: Mon, 9 Dec 2013 20:41:24 -0500 Subject: Declare datetimeDayOfWeek in urweb_cpp.h. --- include/urweb/urweb_cpp.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/urweb/urweb_cpp.h b/include/urweb/urweb_cpp.h index be9f9d16..9105a86a 100644 --- a/include/urweb/urweb_cpp.h +++ b/include/urweb/urweb_cpp.h @@ -269,6 +269,7 @@ uw_Basis_int uw_Basis_datetimeDay(struct uw_context *, uw_Basis_time); uw_Basis_int uw_Basis_datetimeHour(struct uw_context *, uw_Basis_time); uw_Basis_int uw_Basis_datetimeMinute(struct uw_context *, uw_Basis_time); uw_Basis_int uw_Basis_datetimeSecond(struct uw_context *, uw_Basis_time); +uw_Basis_int uw_Basis_datetimeDayOfWeek(struct uw_context *, uw_Basis_time); extern const uw_Basis_time uw_Basis_minTime; void uw_register_transactional(struct uw_context *, void *data, uw_callback commit, uw_callback rollback, uw_callback_with_retry free); -- cgit v1.2.3 From 031c575db2d6e79c3e3f78b4b80d4b1d27d8cf6d Mon Sep 17 00:00:00 2001 From: Patrick Hurst Date: Sat, 25 Jan 2014 18:13:33 -0500 Subject: Trim trailing whitespace in urweb.c. --- src/c/urweb.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/c/urweb.c b/src/c/urweb.c index d7761f7a..831b2452 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -847,7 +847,7 @@ static void adjust_input(input *x, input *old_start, input *new_start, size_t le break; default: break; - } + } } size_t uw_subinputs_max = SIZE_MAX; @@ -1863,12 +1863,12 @@ uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) { uw_unit uw_Basis_urlifyChannel_w(uw_context ctx, uw_Basis_channel chn) { if (ctx->client != NULL && chn.cli == ctx->client->id) { int len; - + uw_check(ctx, INTS_MAX + 1); sprintf(ctx->page.front, "%u%n", chn.chn, &len); ctx->page.front += len; } - + return uw_unit_v; } @@ -1929,11 +1929,11 @@ uw_unit uw_Basis_urlifyBool_w(uw_context ctx, uw_Basis_bool b) { uw_unit uw_Basis_urlifySource_w(uw_context ctx, uw_Basis_source src) { int len; - + uw_check(ctx, 2 * INTS_MAX + 2); sprintf(ctx->page.front, "%d/%llu%n", src.context, src.source, &len); ctx->page.front += len; - + return uw_unit_v; } @@ -2024,7 +2024,7 @@ static uw_Basis_string uw_unurlifyString_to(int fromClient, uw_context ctx, char uw_Basis_bool uw_Basis_unurlifyBool(uw_context ctx, char **s) { char *new_s = uw_unurlify_advance(*s); uw_Basis_bool r; - + if (*s[0] == 0 || !strcmp(*s, "0") || !strcmp(*s, "off")) r = uw_Basis_False; else @@ -2085,7 +2085,7 @@ uw_unit uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) { uw_check(ctx, INTS_MAX); sprintf(ctx->page.front, "%lld%n", n, &len); ctx->page.front += len; - + return uw_unit_v; } @@ -2149,7 +2149,7 @@ uw_unit uw_Basis_jsifyInt_w(uw_context ctx, uw_Basis_int n) { uw_check(ctx, INTS_MAX); sprintf(ctx->page.front, "%lld%n", (uw_Basis_int)n, &len); ctx->page.front += len; - + return uw_unit_v; } @@ -2253,7 +2253,7 @@ uw_unit uw_Basis_htmlifySource_w(uw_context ctx, uw_Basis_source src) { uw_check(ctx, 2 * INTS_MAX + 1); sprintf(ctx->page.front, "s%d_%llu%n", src.context, src.source, &len); ctx->page.front += len; - + return uw_unit_v; } @@ -2363,7 +2363,7 @@ uw_Basis_string uw_Basis_substring(uw_context ctx, uw_Basis_string s, uw_Basis_i r[len] = 0; return r; } - + } uw_Basis_string uw_Basis_str1(uw_context ctx, uw_Basis_char ch) { @@ -2587,7 +2587,7 @@ uw_Basis_string uw_Basis_sqlifyBlob(uw_context ctx, uw_Basis_blob b) { sprintf(s2, "%02X", c); s2 += 2; } - } + } *s2++ = '\''; strcpy(s2, uw_sqlsuffixBlob); @@ -3254,7 +3254,7 @@ static char *find_sig(char *haystack) { if (!s || strlen(haystack) - (s - haystack) - (sizeof sig_intro - 1) < uw_hash_blocksize*2+1) return NULL; - + s += sizeof sig_intro - 1; for (i = 0; i < uw_hash_blocksize*2; ++i) @@ -3667,7 +3667,7 @@ uw_Basis_string uw_unnull(uw_Basis_string s) { uw_Basis_string uw_Basis_makeSigString(uw_context ctx, uw_Basis_string sig) { uw_Basis_string r = uw_malloc(ctx, 2 * uw_hash_blocksize + 1); int i; - + for (i = 0; i < uw_hash_blocksize; ++i) sprintf(&r[2*i], "%.02X", ((unsigned char *)sig)[i]); @@ -3885,7 +3885,7 @@ uw_Basis_string uw_Basis_mstrcat(uw_context ctx, ...) { va_list ap; size_t len = 1; char *s, *r, *s2; - + va_start(ap, ctx); for (s = va_arg(ap, char*); s; s = va_arg(ap, char*)) len += strlen(s); @@ -4117,7 +4117,7 @@ uw_Basis_int uw_Basis_rand(uw_context ctx) { pthread_mutex_lock(&rand_mutex); int r = RAND_bytes((unsigned char *)&ret, sizeof ret); pthread_mutex_unlock(&rand_mutex); - + if (r) return abs(ret); else -- cgit v1.2.3 From 50a8f1ffa063388b6c7c43bf3ecd8c4d92c77cdc Mon Sep 17 00:00:00 2001 From: Patrick Hurst Date: Sat, 25 Jan 2014 18:15:14 -0500 Subject: Use localtime, not gmtime, for datetime functions. --- src/c/urweb.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/c/urweb.c b/src/c/urweb.c index 831b2452..4eb542df 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -3939,43 +3939,43 @@ uw_Basis_time uw_Basis_fromDatetime(uw_context ctx, uw_Basis_int year, uw_Basis_ uw_Basis_int uw_Basis_datetimeYear(uw_context ctx, uw_Basis_time time) { struct tm tm; - gmtime_r(&time.seconds, &tm); + localtime_r(&time.seconds, &tm); return tm.tm_year + 1900; } uw_Basis_int uw_Basis_datetimeMonth(uw_context ctx, uw_Basis_time time) { struct tm tm; - gmtime_r(&time.seconds, &tm); + localtime_r(&time.seconds, &tm); return tm.tm_mon; } uw_Basis_int uw_Basis_datetimeDay(uw_context ctx, uw_Basis_time time) { struct tm tm; - gmtime_r(&time.seconds, &tm); + localtime_r(&time.seconds, &tm); return tm.tm_mday; } uw_Basis_int uw_Basis_datetimeHour(uw_context ctx, uw_Basis_time time) { struct tm tm; - gmtime_r(&time.seconds, &tm); + localtime_r(&time.seconds, &tm); return tm.tm_hour; } uw_Basis_int uw_Basis_datetimeMinute(uw_context ctx, uw_Basis_time time) { struct tm tm; - gmtime_r(&time.seconds, &tm); + localtime_r(&time.seconds, &tm); return tm.tm_min; } uw_Basis_int uw_Basis_datetimeSecond(uw_context ctx, uw_Basis_time time) { struct tm tm; - gmtime_r(&time.seconds, &tm); + localtime_r(&time.seconds, &tm); return tm.tm_sec; } uw_Basis_int uw_Basis_datetimeDayOfWeek(uw_context ctx, uw_Basis_time time) { struct tm tm; - gmtime_r(&time.seconds, &tm); + localtime_r(&time.seconds, &tm); return tm.tm_wday; } -- cgit v1.2.3 From a3c561f3daaffc39245c1aabee46f8cc22f375b8 Mon Sep 17 00:00:00 2001 From: Patrick Hurst Date: Mon, 17 Feb 2014 19:21:46 -0500 Subject: Add Datetime JavaScript support. --- lib/js/urweb.js | 47 ++++++++++++++++++++++++++++++++++++++++------- src/settings.sml | 10 ++++++++++ 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/lib/js/urweb.js b/lib/js/urweb.js index 6830945a..cb9a94af 100644 --- a/lib/js/urweb.js +++ b/lib/js/urweb.js @@ -217,13 +217,13 @@ var Dt = { var y = d.getFullYear(); var V = parseInt(Dt.formats.V(d), 10); var W = parseInt(Dt.formats.W(d), 10); - + if(W > V) { y++; } else if(W===0 && V>=52) { y--; } - + return y; }, H: ["getHours", "0"], @@ -262,7 +262,7 @@ var Dt = { { idow = Dt.formats.V(new Date("" + (d.getFullYear()-1) + "/12/31")); } - + return xPad(idow, 0); }, w: "getDay", @@ -345,7 +345,40 @@ function strftime(fmt, thisTime) var thisDate = new Date(); thisDate.setTime(Math.floor(thisTime / 1000)); return Dt.format(thisDate, fmt); -}; +}; + +function fromDatetime(year, month, date, hour, minute, second) { + console.log(arguments); + return (new Date(year, month, date, hour, minute, second)).getTime() * 1000; +}; + +function datetimeYear(t) { + return (new Date(t / 1000)).getYear() + 1900; +}; + +function datetimeMonth(t) { + return (new Date(t / 1000)).getMonth(); +}; + +function datetimeDay(t) { + return (new Date(t / 1000)).getDate(); +}; + +function datetimeHour(t) { + return (new Date(t / 1000)).getHours(); +}; + +function datetimeMinute(t) { + return (new Date(t / 1000)).getMinutes(); +}; + +function datetimeSecond(t) { + return (new Date(t / 1000)).getSeconds(); +}; + +function datetimeDayOfWeek(t) { + return (new Date(t / 1000)).getDay(); +}; // Error handling @@ -717,7 +750,7 @@ function runScripts(node) { if (node.tagName == "SCRIPT") { var savedScript = thisScript; thisScript = node; - + try { eval(thisScript.text); } catch (v) { @@ -1102,7 +1135,7 @@ function dynClass(html, s_class, s_style) { x.signal = s_class; x.sources = null; x.closures = htmlCls; - + x.recreate = function(v) { for (var ls = x.closures; ls != htmlCls; ls = ls.next) freeClosure(ls.data); @@ -1123,7 +1156,7 @@ function dynClass(html, s_class, s_style) { x.signal = s_style; x.sources = null; x.closures = htmlCls2; - + x.recreate = function(v) { for (var ls = x.closures; ls != htmlCls2; ls = ls.next) freeClosure(ls.data); diff --git a/src/settings.sml b/src/settings.sml index 020ca5a4..6282577d 100644 --- a/src/settings.sml +++ b/src/settings.sml @@ -331,6 +331,16 @@ val jsFuncsBase = basisM [("alert", "alert"), ("toMilliseconds", "toMilliseconds"), ("diffInMilliseconds", "diffInMilliseconds"), + ("fromDatetime", "fromDatetime"), + ("datetimeYear", "datetimeYear"), + ("datetimeMonth", "datetimeMonth"), + ("datetimeDay", "datetimeDay"), + ("datetimeHour", "datetimeHour"), + ("datetimeMinute", "datetimeMinute"), + ("datetimeSecond", "datetimeSecond"), + ("datetimeDayOfWeek", "datetimeDayOfWeek"), + + ("onClick", "uw_onClick"), ("onDblclick", "uw_onDblclick"), ("onKeydown", "uw_onKeydown"), -- cgit v1.2.3