Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 154 lines (117 sloc) 4.481 kb
fccc685 Initial open-source release
MLstate authored
1 (*
2 Copyright © 2011 MLstate
3
6fac5ce @Aqua-Ye [cleanup] ocamllib: typo on Opa
Aqua-Ye authored
4 This file is part of Opa.
fccc685 Initial open-source release
MLstate authored
5
6fac5ce @Aqua-Ye [cleanup] ocamllib: typo on Opa
Aqua-Ye authored
6 Opa is free software: you can redistribute it and/or modify it under the
fccc685 Initial open-source release
MLstate authored
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
6fac5ce @Aqua-Ye [cleanup] ocamllib: typo on Opa
Aqua-Ye authored
10 Opa is distributed in the hope that it will be useful, but WITHOUT ANY
fccc685 Initial open-source release
MLstate authored
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
6fac5ce @Aqua-Ye [cleanup] ocamllib: typo on Opa
Aqua-Ye authored
16 along with Opa. If not, see <http://www.gnu.org/licenses/>.
fccc685 Initial open-source release
MLstate authored
17 *)
18
865ee95 [enhance] 32 bits port: Time now compliant, so are cookies
François-Régis Sinot authored
19 #<Ifstatic:OCAML_WORD_SIZE 64>
20
21 type t = int
fccc685 Initial open-source release
MLstate authored
22 type tm = Unix.tm
23
865ee95 [enhance] 32 bits port: Time now compliant, so are cookies
François-Régis Sinot authored
24 let infinity = max_int
25
fccc685 Initial open-source release
MLstate authored
26 let of_unix_time t = int_of_float (t *. 1000.)
27 let to_unix_time t = float_of_int t /. 1000.
28 let fmi = float_of_int max_int
29 let of_unix_time_inf t = let t1000 = t *. 1000.0 in if t1000 >= fmi then max_int else int_of_float t1000
30
865ee95 [enhance] 32 bits port: Time now compliant, so are cookies
François-Régis Sinot authored
31 let adjust_mktime res ms =
32 if res < 0 then res - ms else res + ms
33
34 let zero = 0
35 let is_positive t = t > 0
36 let add t1 t2 = if t1 = infinity || t2 = infinity then infinity else t1 + t2
37 let difference t1 t2 = if t1 = infinity then zero else if t2 = infinity then infinity else t2 - t1
38
39 let milliseconds v = v
40 let in_milliseconds t = t
41 let in_seconds t = float_of_int t /. 1000.
42 let round_to_sec t = t - (t mod 1000)
43
44 let gmt_msec time = (abs time) mod 1000
45 let local_msec time = (abs time) mod 1000
46
47 #<Else>
48
49 type t = float
50 type tm = Unix.tm
51
52 let infinity = infinity
53
54 let of_unix_time t = t
55 let to_unix_time t = t
56 let of_unix_time_inf t = t
57
58 let adjust_mktime res ms =
59 (if res < 0. then (-.) else (+.)) res (float_of_int ms /. 1000.)
60
61 let zero = 0.
62 let is_positive t = t > 0.
63 let add t1 t2 = t1 +. t2
64 let difference t1 t2 = t2 -. t1
65
66 let milliseconds v = float_of_int v /. 1000.
67 let in_milliseconds t = int_of_float (t *. 1000.)
68 let in_seconds t = t
69 let round_to_sec t = floor t
70
71 let gmt_msec time = int_of_float (snd (modf (abs_float time)) *. 1000.)
72 let local_msec time = gmt_msec time
73
74 #<End>
75
fccc685 Initial open-source release
MLstate authored
76 let now () = of_unix_time (Unix.gettimeofday ())
77
78 let process_utime () = of_unix_time (Unix.times ()).Unix.tms_utime
79 let process_stime () = of_unix_time (Unix.times ()).Unix.tms_stime
80 let process_cutime () = of_unix_time (Unix.times ()).Unix.tms_cutime
81 let process_cstime () = of_unix_time (Unix.times ()).Unix.tms_cstime
82
83 let sleep = Unix.sleep
84
85 let gmtime t = Unix.gmtime (to_unix_time t)
86 let localtime t = Unix.localtime (to_unix_time t)
87
88 let gmt_sec time = (gmtime time).Unix.tm_sec
89 let gmt_min time = (gmtime time).Unix.tm_min
90 let gmt_hour time = (gmtime time).Unix.tm_hour
91 let gmt_mday time = (gmtime time).Unix.tm_mday
92 let gmt_mon time = (gmtime time).Unix.tm_mon
93 let gmt_year time = (gmtime time).Unix.tm_year + 1900
94 let gmt_wday time = (gmtime time).Unix.tm_wday
95 let gmt_yday time = (gmtime time).Unix.tm_yday
96 let gmt_isdst time = (gmtime time).Unix.tm_isdst
97
98 let local_sec time = (localtime time).Unix.tm_sec
99 let local_min time = (localtime time).Unix.tm_min
100 let local_hour time = (localtime time).Unix.tm_hour
101 let local_mday time = (localtime time).Unix.tm_mday
102 let local_mon time = (localtime time).Unix.tm_mon
103 let local_year time = (localtime time).Unix.tm_year + 1900
104 let local_wday time = (localtime time).Unix.tm_wday
105 let local_yday time = (localtime time).Unix.tm_yday
106 let local_isdst time = (localtime time).Unix.tm_isdst
107
11a55a7 [enhance] stdlib: Add %z (timezone) handling in Date code.
Alok Menghrajani authored
108 let local_timezone_offset () =
109 let t = Unix.time() in
110 let gmt = Unix.gmtime(t) in
111 let local = Unix.localtime(t) in
112 let (gmt_s, _) = Unix.mktime(gmt) in
113 let (local_s, _) = Unix.mktime(local) in
114 int_of_float((gmt_s -. local_s) /. 60.0);;
115
fccc685 Initial open-source release
MLstate authored
116 let mktime ~year ~month ~day ~h ~min ~sec ~ms =
117 let res =
118 of_unix_time (
119 fst (
120 Unix.mktime {
121 Unix.tm_sec = sec ;
122 Unix.tm_min = min ;
123 Unix.tm_hour = h ;
124 Unix.tm_mday = day ;
125 Unix.tm_mon = month ;
126 Unix.tm_year = year - 1900 ;
127 Unix.tm_wday = 0 ;
128 Unix.tm_yday = 0 ;
129 Unix.tm_isdst = false
130 }
131 )
132 )
133 in
865ee95 [enhance] 32 bits port: Time now compliant, so are cookies
François-Régis Sinot authored
134 adjust_mktime res ms
fccc685 Initial open-source release
MLstate authored
135
136 let bound = Chrono.bound
137
138
139 let is_infinite t = t = infinity
140
141 let is_after t1 t2 = t1 > t2
142 let is_before t1 t2 = t1 < t2
143
144 let seconds v = milliseconds (v * 1000)
145 let seconds_float = of_unix_time
146 let minutes v = seconds (v * 60)
147 let hours v = minutes (v * 60)
148 let days v = hours (v * 24)
149
150 let max = Pervasives.max
151 let min = Pervasives.min
152
153 let get_accurate_time = Unix.gettimeofday
Something went wrong with that request. Please try again.