Skip to content

Commit

Permalink
Time: 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
rr0gi committed Feb 14, 2024
1 parent 7cf2334 commit 2068e04
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions time.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,23 @@ let put_2d s ofs n =
Bytes.unsafe_set s ofs (unsafe_digit (n / 10));
Bytes.unsafe_set s (ofs+1) (unsafe_digit (n mod 10))

let replace_year_2023 s t =
let yyyy = 2024

let replace_yyyy s t =
let year = t.Unix.tm_year + 1900 in
if year <> 2023 then
if year <> yyyy then
begin
if year >= 2023 && year < 2030 then
if year >= yyyy && year < 2030 then
Bytes.unsafe_set s 3 (unsafe_digit (year mod 10))
else
Bytes.unsafe_blit (Bytes.unsafe_of_string @@ if year >= 1000 then string_of_int year else sprintf "%04u" year) 0 s 0 4
end

let yyyy = string_of_int yyyy

let fast_to_string =
(* "%04u-%02u-%02uT%02u:%02u:%02u%s" *)
let template = "2023-__-__T__:__:__" in
let template = yyyy ^ "-__-__T__:__:__" in
let template_z = template ^ "Z" in
let last_time = ref 0 in
let last_gmt = ref true in
Expand All @@ -47,7 +51,7 @@ let fast_to_string =
let open Unix in
let t = (if gmt then gmtime else localtime) f in
let s = Bytes.of_string (if gmt then template_z else template) in
replace_year_2023 s t;
replace_yyyy s t;
put_2d s 5 (t.tm_mon+1);
put_2d s 8 t.tm_mday;
put_2d s 11 t.tm_hour;
Expand All @@ -74,45 +78,45 @@ let gmt_string_ms = to_string ~gmt:true ~ms:true

(** YYYY-MM-DD *)
let format_date_w3 =
let template = "2023-__-__" in
let template = yyyy^"-__-__" in
fun t ->
let open Unix in
let s = Bytes.of_string template in
replace_year_2023 s t;
replace_yyyy s t;
put_2d s 5 (t.tm_mon+1);
put_2d s 8 t.tm_mday;
Bytes.unsafe_to_string s

(** YYYYMMDD *)
let format_date8 =
let template = "2023____" in
let template = yyyy^"____" in
fun t ->
let open Unix in
let s = Bytes.of_string template in
replace_year_2023 s t;
replace_yyyy s t;
put_2d s 4 (t.tm_mon+1);
put_2d s 6 t.tm_mday;
Bytes.unsafe_to_string s

(** YYYYMMDDhh *)
let format_date8h =
let template = "2023______" in
let template = yyyy^"______" in
fun t ->
let open Unix in
let s = Bytes.of_string template in
replace_year_2023 s t;
replace_yyyy s t;
put_2d s 4 (t.tm_mon+1);
put_2d s 6 t.tm_mday;
put_2d s 8 t.tm_hour;
Bytes.unsafe_to_string s

(** YYYYMMDDhhmm *)
let format_date8hm =
let template = "2023________" in
let template = yyyy^"________" in
fun t ->
let open Unix in
let s = Bytes.of_string template in
replace_year_2023 s t;
replace_yyyy s t;
put_2d s 4 (t.tm_mon+1);
put_2d s 6 t.tm_mday;
put_2d s 8 t.tm_hour;
Expand All @@ -121,11 +125,11 @@ let format_date8hm =

(** YYYYMMDDhhmmss *)
let format_date8hms =
let template = "2023__________" in
let template = yyyy^"__________" in
fun t ->
let open Unix in
let s = Bytes.of_string template in
replace_year_2023 s t;
replace_yyyy s t;
put_2d s 4 (t.tm_mon+1);
put_2d s 6 t.tm_mday;
put_2d s 8 t.tm_hour;
Expand All @@ -135,11 +139,11 @@ let format_date8hms =

(** YYYY-MM-DD hh:mm:ss *)
let format_basic =
let template = "2023-__-__ __:__:__" in
let template = yyyy^"-__-__ __:__:__" in
fun t ->
let open Unix in
let s = Bytes.of_string template in
replace_year_2023 s t;
replace_yyyy s t;
put_2d s 5 (t.tm_mon+1);
put_2d s 8 t.tm_mday;
put_2d s 11 t.tm_hour;
Expand All @@ -157,10 +161,10 @@ let format_date4 t =

(** YYYYMM *)
let format_date_yyyymm =
let template = "2023__" in
let template = yyyy^"__" in
fun t ->
let s = Bytes.of_string template in
replace_year_2023 s t;
replace_yyyy s t;
put_2d s 4 (t.tm_mon+1);
Bytes.unsafe_to_string s

Expand Down

0 comments on commit 2068e04

Please sign in to comment.