cpdf-source/cpdfstrftime.ml

94 lines
2.3 KiB
OCaml
Raw Permalink 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 =
2013-11-22 15:42:18 +01:00
match t.Unix.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 =
2013-11-22 15:42:18 +01:00
match t.Unix.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 =
2013-11-22 15:42:18 +01:00
let s = string_of_int t.Unix.tm_mday in
2013-11-11 17:43:03 +01:00
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 =
2013-11-22 15:42:18 +01:00
let s = string_of_int t.Unix.tm_mday in
2013-11-11 17:43:03 +01:00
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 =
2013-11-22 15:42:18 +01:00
let s = string_of_int t.Unix.tm_hour in
2013-11-11 17:43:03 +01:00
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 =
2013-11-22 15:42:18 +01:00
let s = string_of_int (t.Unix.tm_hour mod 12) in
2013-11-11 17:43:03 +01:00
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 =
2013-11-22 15:42:18 +01:00
let s = string_of_int t.Unix.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 =
2013-11-22 15:42:18 +01:00
let s = string_of_int (t.Unix.tm_mon + 1) in
2013-11-11 17:43:03 +01:00
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 =
2013-11-22 15:42:18 +01:00
let s = string_of_int t.Unix.tm_min in
2013-11-11 17:43:03 +01:00
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 =
2013-11-22 15:42:18 +01:00
if t.Unix.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 =
2013-11-22 15:42:18 +01:00
let s = string_of_int t.Unix.tm_sec in
2013-11-11 17:43:03 +01:00
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 =
2013-11-22 15:42:18 +01:00
match t.Unix.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 =
2013-11-22 15:42:18 +01:00
string_of_int t.Unix.tm_wday
2013-08-20 16:32:57 +02:00
2013-11-11 17:43:03 +01:00
let strf_Y t =
2013-11-22 15:42:18 +01:00
string_of_int (t.Unix.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 =
2013-11-22 15:42:18 +01:00
let time = Unix.localtime (Unix.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