From 872491f60e85807d989fe2ccb94e77b098089726 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Sat, 29 Jun 2019 14:04:27 +0100 Subject: [PATCH] First finished implementation of XMP dates --- cpdf.ml | 43 +++++++++++++++++++++++++++++++++---------- cpdfstrftime.ml | 6 ++++-- cpdfstrftime.mli | 2 +- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/cpdf.ml b/cpdf.ml index a8655a2..4b62539 100644 --- a/cpdf.ml +++ b/cpdf.ml @@ -3426,7 +3426,27 @@ let default_date () = offset_hours = 0; offset_minutes = 0} -let make_xmp_date_from_components d = "" +(* XMP date format is YYYY-MM-DDThh:mm:ssTZD *) +let make_xmp_date_from_components d = + let tzd = + (if d.ut_relationship >=0 then "+" else "-") ^ + Printf.sprintf "%2i" d.offset_hours ^ + ":" ^ + Printf.sprintf "%2i" d.offset_minutes + in + Cpdfstrftime.strftime + ~time:{Unix.tm_sec = d.second; + Unix.tm_min = d.minute; + Unix.tm_hour = d.hour; + Unix.tm_mday = d.day; + Unix.tm_mon = d.month - 1; + Unix.tm_year = d.year - 1900; + Unix.tm_wday = 0; + Unix.tm_yday = 0; + Unix.tm_isdst = false} + "%Y-%m-%dT%H:%M:%S" + ^ + tzd let xmp_date date = let d = default_date () in @@ -3435,31 +3455,34 @@ let xmp_date date = 'D'::':'::r -> begin match r with y1::y2::y3::y4::r -> - d.year <- 0; + d.year <- int_of_string (implode [y1; y2; y3; y4]); begin match r with m1::m2::r -> - d.month <- 0; + d.month <- int_of_string (implode [y1; y2]); begin match r with d1::d2::r -> - d.day <- 0; + d.day <- int_of_string (implode [d1; d2]); begin match r with h1::h2::r -> - d.hour <- 0; + d.hour <- int_of_string (implode [h1; h2]); begin match r with m1::m2::r -> - d.minute <- 0; + d.minute <- int_of_string (implode [m1; m2]); begin match r with s1::s2::r -> - d.second <- 0; + d.second <- int_of_string (implode [s1; s2]); begin match r with o::r -> - d.ut_relationship <- 0; + d.ut_relationship <- + if o = '+' then 1 else + if o = '-' then -1 else + 0; begin match r with h1::h2::'\''::r -> - d.offset_hours <- 0; + d.offset_hours <- int_of_string (implode [h1; h2]); begin match r with m1::m2::_ -> - d.offset_minutes <- 0; + d.offset_minutes <- int_of_string (implode [m1; m2]); raise Exit | _ -> raise Exit end diff --git a/cpdfstrftime.ml b/cpdfstrftime.ml index 976b04f..5996505 100644 --- a/cpdfstrftime.ml +++ b/cpdfstrftime.ml @@ -82,8 +82,10 @@ let strftime_pairs = "%p", strf_p; "%S", strf_S; "%T", strf_T; "%u", strf_u; "%w", strf_w; "%Y", strf_Y; "%%", strf_percent] -let strftime text = - let time = Unix.localtime (Unix.gettimeofday ()) in +let strftime ?time text = + let time = + match time with None -> Unix.localtime (Unix.gettimeofday ()) | Some t -> t + in let text = ref text in iter (fun (search, replace_fun) -> diff --git a/cpdfstrftime.mli b/cpdfstrftime.mli index 602a714..aa6c86e 100644 --- a/cpdfstrftime.mli +++ b/cpdfstrftime.mli @@ -1,4 +1,4 @@ (** C-style strftime *) (** This supports the following format specifiers: %a %A %b %B %s %e %H %I %j %m %M %p %S %T %u %w %Y %% *) -val strftime : string -> string +val strftime : ?time:Unix.tm -> string -> string