X-Git-Url: http://git.annexia.org/?p=whenjobs.git;a=blobdiff_plain;f=lib%2Fwhenutils.ml;h=362f4f69bdbe1afb1777bdfc687bb7b91737db8c;hp=8cab30e4a5fbbb24f4001c1ae6be79c39032183f;hb=0f58f891d531defd1fa923dd2da93678c9c6f35b;hpb=973fae9b3be949857be0612dd71280156121f82e;ds=sidebyside diff --git a/lib/whenutils.ml b/lib/whenutils.ml index 8cab30e..362f4f6 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 @@ -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'