X-Git-Url: http://git.annexia.org/?p=whenjobs.git;a=blobdiff_plain;f=lib%2Fwhenutils.ml;h=643da7def8a2e3e7b06188e0c5cd6c7dd6c3d162;hp=8cab30e4a5fbbb24f4001c1ae6be79c39032183f;hb=ad0b6d412312967a6604a763368ce1bcd977bc75;hpb=973fae9b3be949857be0612dd71280156121f82e diff --git a/lib/whenutils.ml b/lib/whenutils.ml index 8cab30e..643da7d 100644 --- a/lib/whenutils.ml +++ b/lib/whenutils.ml @@ -19,7 +19,10 @@ open Camlp4.PreCast open Ast +open CalendarLib + open Big_int +open Unix open Printf module StringMap = struct @@ -129,8 +132,8 @@ let rec expr_of_ast _loc ast = expr_of_iexpr _loc (iexpr_of_ast _loc ast) and iexpr_of_ast _loc = function - | ExId (_, IdLid (_, "true")) -> IExpr_bool true - | ExId (_, IdLid (_, "false")) -> IExpr_bool false + | ExId (_, IdUid (_, "True")) -> IExpr_bool true + | ExId (_, IdUid (_, "False")) -> IExpr_bool false | ExStr (_, str) -> IExpr_str str | ExInt (_, i) -> IExpr_int (big_int_of_string i) (* XXX too large? *) | ExFlo (_, f) -> IExpr_float (float_of_string f) @@ -358,3 +361,48 @@ let job_evaluate job variables = job_prev_variables = variables } in let job = { job with job_private = jobp } in true, job + +let next_periodexpr = + (* Round up 'a' to the next multiple of 'i'. *) + let round_up_float a i = + let r = mod_float a i in + if r = 0. then a +. i else a +. (i -. r) + and round_up a i = + let r = a mod i in + if r = 0 then a + i else a + (i - r) + in + + fun t -> function + | Every_seconds i -> + let i = float_of_int i in + round_up_float t i + + | Every_years i -> + let tm = gmtime t in + + (* Round 'tm' up to the first day of the next year. *) + let year = round_up tm.tm_year i in + let tm = { tm with tm_sec = 0; tm_min = 0; tm_hour = 0; + tm_mday = 1; tm_mon = 0; tm_year = year } in + fst (mktime tm) + + | Every_days i -> + let t = Date.from_unixfloat t in + let t0 = Date.make 1970 1 1 in + + (* Number of whole days since Unix Epoch. *) + let nb_days = Date.Period.safe_nb_days (Date.sub t t0) in + + let nb_days = round_up nb_days i in + let t' = Date.add t0 (Date.Period.day nb_days) in + Date.to_unixfloat t' + + | Every_months i -> + (* Calculate number of whole months since Unix Epoch. *) + let tm = gmtime t in + let months = 12 * (tm.tm_year - 70) + tm.tm_mon in + + let months = round_up months i in + let t0 = Date.make 1970 1 1 in + let t' = Date.add t0 (Date.Period.month months) in + Date.to_unixfloat t'