package devkit
Development kit - general purpose library
Install
Dune Dependency
Authors
Maintainers
Sources
devkit-1.3.tbz
sha256=dae965685dceed47ad8e9844f12fe707dafdf2c3bdd46d0431d5b4d1e7754b23
sha512=b94ade804d751db87434042bbaa821fa8e82e233820a76806f910e2da040094b137e88a3579911a1626930912622b064c776ddbcb6991fb7111021ebf6553fdc
doc/src/devkit.core/time.ml.html
Source file time.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
(** Time *) open Printf open ExtLib open Prelude (** unix timestamp *) type t = float (* duration in seconds *) type duration = float let compare = Float.compare let get = Unix.gettimeofday let now = Unix.gettimeofday let unsafe_digit n = Char.unsafe_chr (Char.code '0' + n) 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_2019 s year = if year <> 2019 then begin if year >= 2010 && year < 2020 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 fast_to_string = (* "%04u-%02u-%02uT%02u:%02u:%02u%s" *) let template = "2019-__-__T__:__:__" in let template_z = template ^ "Z" in let last_time = ref 0 in let last_gmt = ref true in let last_s = ref "1970-01-01T00:00:00Z" in fun ~gmt f -> let seconds = int_of_float f in if gmt = !last_gmt && seconds = !last_time then !last_s else 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_2019 s (t.tm_year + 1900); put_2d s 5 (t.tm_mon+1); put_2d s 8 t.tm_mday; put_2d s 11 t.tm_hour; put_2d s 14 t.tm_min; put_2d s 17 t.tm_sec; last_time := seconds; last_gmt := gmt; let s = Bytes.unsafe_to_string s in last_s := s; s let to_string ?(gmt=false) ?(ms=false) f = match ms with | false -> fast_to_string ~gmt f | true -> let t = (if gmt then Unix.gmtime else Unix.localtime) f in let sec = sprintf "%07.4f" (mod_float f 60.) in sprintf "%04u-%02u-%02uT%02u:%02u:%s%s" (1900 + t.Unix.tm_year) (t.Unix.tm_mon+1) t.Unix.tm_mday t.Unix.tm_hour t.Unix.tm_min sec (if gmt then "Z" else "") (** @see <http://www.w3.org/TR/NOTE-datetime> W3C Datetime *) let gmt_string = to_string ~gmt:true ~ms:false let gmt_string_ms = to_string ~gmt:true ~ms:true (** YYYY-MM-DD *) let format_date_w3 = let template = "2019-__-__" in fun t -> let open Unix in let s = Bytes.of_string template in replace_year_2019 s (t.tm_year + 1900); 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 = "2019____" in fun t -> let open Unix in let s = Bytes.of_string template in replace_year_2019 s (t.tm_year + 1900); 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 = "2019______" in fun t -> let open Unix in let s = Bytes.of_string template in replace_year_2019 s (t.tm_year + 1900); 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 = "2019________" in fun t -> let open Unix in let s = Bytes.of_string template in replace_year_2019 s (t.tm_year + 1900); put_2d s 4 (t.tm_mon+1); put_2d s 6 t.tm_mday; put_2d s 8 t.tm_hour; put_2d s 10 t.tm_min; Bytes.unsafe_to_string s (** YYYYMMDDhhmmss *) let format_date8hms = let template = "2019__________" in fun t -> let open Unix in let s = Bytes.of_string template in replace_year_2019 s (t.tm_year + 1900); put_2d s 4 (t.tm_mon+1); put_2d s 6 t.tm_mday; put_2d s 8 t.tm_hour; put_2d s 10 t.tm_min; put_2d s 12 t.tm_sec; Bytes.unsafe_to_string s (** MMDD *) let format_date4 t = let open Unix in let s = Bytes.create 4 in put_2d s 0 (t.tm_mon+1); put_2d s 2 t.tm_mday; Bytes.unsafe_to_string s let date_w3_gmt_string = format_date_w3 $ Unix.gmtime let date_w3_string = format_date_w3 $ Unix.localtime let date8_gmt_string = format_date8 $ Unix.gmtime let date8_string = format_date8 $ Unix.localtime let date8h_gmt_string = format_date8h $ Unix.gmtime let date8h_string = format_date8h $ Unix.localtime let date8hm_gmt_string = format_date8hm $ Unix.gmtime let date8hm_string = format_date8hm $ Unix.localtime let date8hms_gmt_string = format_date8hms $ Unix.gmtime let date8hms_string = format_date8hms $ Unix.localtime let date4_gmt_string = format_date4 $ Unix.gmtime let date4_string = format_date4 $ Unix.localtime (** unix timestamp to RFC-2822 date Example: Tue, 15 Nov 1994 12:45:26 GMT *) let to_rfc2822 secs = let module U = Unix in let t = U.gmtime secs in let wdays = [| "Sun"; "Mon"; "Tue"; "Wed"; "Thu"; "Fri"; "Sat" |] in let mons = [|"Jan"; "Feb"; "Mar"; "Apr"; "May"; "Jun"; "Jul"; "Aug"; "Sep"; "Oct"; "Nov"; "Dec"|] in let wday = wdays.(t.U.tm_wday mod 7) in let mon = mons.(t.U.tm_mon mod 12) in sprintf "%s, %02u %s %04u %02u:%02u:%02u GMT" wday t.U.tm_mday mon (1900 + t.U.tm_year) t.U.tm_hour t.U.tm_min t.U.tm_sec (** @param cut - only show this number of most significant components *) let show_duration ?cut t = let factors = [60; 60; 24; 365 ] in let names = ["sec"; "min"; "hour"; "day"; "year";] in let rec loop t acc = function | [] -> List.rev (t :: acc) | n::tl -> loop (t/n) (t mod n :: acc) tl in if t < 1. then sprintf "%.4f secs" t else if t < 10. then sprintf "%.2f secs" t else loop (int_of_float t) [] factors |> List.combine names |> List.rev |> List.dropwhile (fun (_,x) -> x = 0) |> (match cut with Some n -> List.take n | None -> id) |> List.map (fun (n,x) -> sprintf "%u %s%s" x n (if x <> 1 then "s" else "")) |> String.concat " " let duration_str = show_duration (* 1m10s *) let show_compact_duration ?(full=false) ?cut t = let factors = [60; 60; 24; ] in let names = ["s"; "m"; "h"; "d"; ] in let rec loop t acc = function | [] -> List.rev (t::acc) | n::tl -> loop (t/n) (t mod n :: acc) tl in if t < 0.000_01 then sprintf "%.0fns" (t *. 1_000_000_000.) else if t < 0.01 then sprintf "%.2gms" (t *. 1_000.) else if t < 1. then sprintf "%.0fms" (t *. 1_000.) else if t < 10. then sprintf "%.2gs" t else loop (int_of_float t) [] factors |> List.combine names |> (if full then id else List.dropwhile (fun (_,x) -> x = 0)) |> List.rev |> List.dropwhile (fun (_,x) -> x = 0) |> (match cut with Some n -> List.take n | None -> id) |> List.mapi (fun i (n,x) -> sprintf (if i = 0 then "%u%s" else "%02u%s") x n) |> String.concat "" let compact_duration = show_compact_duration (** parse compact_duration representation (except for fractional seconds) *) let of_compact_duration s = match s with | "" -> invalid_arg "of_compact_duration empty input" | _ -> let s = match s.[0] with | '0'..'9' -> s | _ -> "1"^s in Devkit_ragel.parse_compact_duration s let minutes x = float @@ 60 * x let hours x = minutes @@ 60 * x let days x = hours @@ 24 * x let seconds x = float x (** convert integer number of milliseconds to Time.t *) let msec x = float x /. 1000. (** convert integer number of nanoseconds to Time.t *) let nsec x = float x /. 1_000_000_000. let int x = int_of_float x let to_sec = int let to_ms x = int_of_float @@ 1000. *. x let ago t = now () -. t let ago_str = show_duration $ ago
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>