cpdf-source/cpdfstrftime.ml

94 lines
2.4 KiB
OCaml
Raw Normal View History

2013-08-20 16:32:57 +02:00
(* C-Style strftime *)
open Pdfutil
2013-11-11 17:43:03 +01:00
let strf_A t =
match t.Cpdfunix.tm_wday with
2013-08-20 16:32:57 +02:00
| 0 -> "Sunday" | 1 -> "Monday" | 2 -> "Tuesday"
| 3 -> "Wednesday" | 4 -> "Thursday" | 5 -> "Friday"
| 6 -> "Saturday"
2013-11-11 17:43:03 +01:00
| _ -> "strf_AFail"
2013-08-20 16:32:57 +02:00
let strf_a t =
String.sub (strf_A t) 0 3
2013-11-11 17:43:03 +01:00
let strf_B t =
match t.Cpdfunix.tm_mon with
2013-08-20 16:32:57 +02:00
| 0 -> "January" | 1 -> "February" | 2 -> "March" | 3 -> "April"
| 4 -> "May" | 5 -> "June" | 6 -> "July" | 7 -> "August"
| 8 -> "September" | 9 -> "October" | 10 -> "November"
2013-11-11 17:43:03 +01:00
| 11 -> "December" | _ -> "strf_Bfail"
2013-08-20 16:32:57 +02:00
let strf_b t =
String.sub (strf_B t) 0 3
2013-11-11 17:43:03 +01:00
let strf_d t =
let s = string_of_int t.Cpdfunix.tm_mday in
if String.length s = 1 then "0" ^ s else s
2013-08-20 16:32:57 +02:00
2013-11-11 17:43:03 +01:00
let strf_e t =
let s = string_of_int t.Cpdfunix.tm_mday in
if String.length s = 1 then " " ^ s else s
2013-08-20 16:32:57 +02:00
2013-11-11 17:43:03 +01:00
let strf_H t =
let s = string_of_int t.Cpdfunix.tm_hour in
if String.length s = 1 then "0" ^ s else s
2013-08-20 16:32:57 +02:00
2013-11-11 17:43:03 +01:00
let strf_I t =
let s = string_of_int (t.Cpdfunix.tm_hour mod 12) in
if String.length s = 1 then "0" ^ s else s
2013-08-20 16:32:57 +02:00
2013-11-11 17:43:03 +01:00
let strf_j t =
let s = string_of_int t.Cpdfunix.tm_yday in
2013-08-20 16:32:57 +02:00
match String.length s with
| 1 -> "00" ^ s
| 2 -> "0" ^ s
2013-11-11 17:43:03 +01:00
| _ -> s
2013-08-20 16:32:57 +02:00
2013-11-11 17:43:03 +01:00
let strf_m t =
let s = string_of_int (t.Cpdfunix.tm_mon + 1) in
if String.length s = 1 then "0" ^ s else s
2013-08-20 16:32:57 +02:00
2013-11-11 17:43:03 +01:00
let strf_M t =
let s = string_of_int t.Cpdfunix.tm_min in
if String.length s = 1 then "0" ^ s else s
2013-08-20 16:32:57 +02:00
2013-11-11 17:43:03 +01:00
let strf_p t =
if t.Cpdfunix.tm_hour >= 12 then "p.m" else "a.m"
2013-08-20 16:32:57 +02:00
2013-11-11 17:43:03 +01:00
let strf_S t =
let s = string_of_int t.Cpdfunix.tm_sec in
if String.length s = 1 then "0" ^ s else s
2013-08-20 16:32:57 +02:00
let strf_T t =
strf_H t ^ ":" ^ strf_M t ^ ":" ^ strf_S t
2013-11-11 17:43:03 +01:00
let strf_u t =
match t.Cpdfunix.tm_wday with
2013-08-20 16:32:57 +02:00
| 0 -> "7"
2013-11-11 17:43:03 +01:00
| n -> string_of_int (n + 1)
2013-08-20 16:32:57 +02:00
2013-11-11 17:43:03 +01:00
let strf_w t =
string_of_int t.Cpdfunix.tm_wday
2013-08-20 16:32:57 +02:00
2013-11-11 17:43:03 +01:00
let strf_Y t =
string_of_int (t.Cpdfunix.tm_year + 1900)
2013-08-20 16:32:57 +02:00
let strf_percent _ = "%"
let strftime_pairs =
["%a", strf_a; "%A", strf_A; "%b", strf_b; "%B", strf_B;
"%d", strf_d; "%e", strf_e; "%H", strf_H;
"%I", strf_I; "%j", strf_j; "%m", strf_m; "%M", strf_M;
"%p", strf_p; "%S", strf_S; "%T", strf_T; "%u", strf_u;
"%w", strf_w; "%Y", strf_Y; "%%", strf_percent]
2013-11-11 17:43:03 +01:00
let strftime text =
let time = Cpdfunix.localtime (Cpdfunix.gettimeofday ()) in
2013-08-20 16:32:57 +02:00
let text = ref text in
iter
(fun (search, replace_fun) ->
text := string_replace_all search (replace_fun time) !text)
strftime_pairs;
2013-11-11 17:43:03 +01:00
!text
2013-08-20 16:32:57 +02:00