adac6ff06ed06050119c98aa7e67aa7c9847e824
[nit.git] / lib / core / time.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2008 Floréal Morandat <morandat@lirmm.fr>
4 # Copyright 2014 Alexandre Terrasa <alexandre@moz-code.org>
5 #
6 # This file is free software, which comes along with NIT. This software is
7 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
9 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
10 # is kept unaltered, and a notification of the changes is added.
11 # You are allowed to redistribute it and sell it, alone or is a part of
12 # another product.
13
14 # Management of time and dates
15 module time
16
17 import text
18 import stream
19
20 in "C Header" `{
21 #include <time.h>
22 `}
23
24 redef class Object
25 # Unix time: the number of seconds elapsed since January 1, 1970
26 protected fun get_time: Int `{ return time(NULL); `}
27 end
28
29 redef class Sys
30 # Wait a specific number of second and nanoseconds
31 fun nanosleep(sec, nanosec: Int) `{
32 const struct timespec req = {sec, nanosec};
33 nanosleep(&req, NULL);
34 `}
35 end
36
37 # Time since epoch
38 extern class TimeT `{time_t`}
39
40 # Returns Time since epoch from now.
41 new `{ return time(NULL); `}
42
43 # Returns Time since epoch from `i` (expressed in seconds).
44 new from_i(i: Int) `{ return i; `}
45
46 # Update current time.
47 fun update `{ time(&self); `}
48
49 # Convert `self` to a human readable String.
50 fun ctime: String import NativeString.to_s_with_copy `{
51 return NativeString_to_s_with_copy( ctime(&self) );
52 `}
53
54 # Difference in secondes from start (self if the end time)
55 fun difftime(start: TimeT): Float `{ return difftime(self, start); `}
56
57 redef fun to_s do return ctime.replace("\n", "")
58
59 # Convert self to Int (expressed as seconds since epoch).
60 fun to_i: Int `{ return (int)self; `}
61 end
62
63 # Time structure
64 extern class Tm `{struct tm *`}
65
66 # Create a new Time structure expressed in Coordinated Universal Time (UTC).
67 new gmtime `{
68 struct tm *tm;
69 time_t t = time(NULL);
70 tm = gmtime(&t);
71 return tm;
72 `}
73
74 # Create a new Time structure expressed in UTC from `t`.
75 new gmtime_from_timet(t: TimeT) `{
76 struct tm *tm;
77 tm = gmtime(&t);
78 return tm;
79 `}
80
81 # Create a new Time structure expressed in the local timezone.
82 new localtime `{
83 struct tm *tm;
84 time_t t = time(NULL);
85 tm = localtime(&t);
86 return tm;
87 `}
88
89 # Create a new Time structure expressed in the local timezone from `t`.
90 new localtime_from_timet(t: TimeT) `{
91 struct tm *tm;
92 tm = localtime(&t);
93 return tm;
94 `}
95
96 # Convert `self` as a TimeT.
97 fun to_timet: TimeT `{ return mktime(self); `}
98
99 # Seconds after the minute.
100 fun sec: Int `{ return self->tm_sec; `}
101
102 # Minutes after the hour.
103 fun min: Int `{ return self->tm_min; `}
104
105 # hours since midnight.
106 fun hour: Int `{ return self->tm_hour; `}
107
108 # Day of the month.
109 fun mday: Int `{ return self->tm_mday; `}
110
111 # Months since January.
112 fun mon: Int `{ return self->tm_mon; `}
113
114 # Years since 1900.
115 fun year: Int `{ return self->tm_year; `}
116
117 # Days since Sunday.
118 fun wday: Int `{ return self->tm_wday; `}
119
120 # Days since January 1st.
121 fun yday: Int `{ return self->tm_yday; `}
122
123 # Is `self` in Daylight Saving Time.
124 fun is_dst: Bool `{ return self->tm_isdst; `}
125
126 # Convert `self` to a human readable String.
127 fun asctime: String import NativeString.to_s_with_copy `{
128 return NativeString_to_s_with_copy( asctime(self) );
129 `}
130
131 # Convert `self` to a human readable String corresponding to `format`.
132 # TODO document allowed format.
133 fun strftime(format: String): String import String.to_cstring, NativeString.to_s `{
134 char* buf, *c_format;
135
136 buf = (char*)malloc(100);
137 c_format = String_to_cstring(format);
138
139 strftime(buf, 100, c_format, self);
140 String s = NativeString_to_s_with_copy(buf);
141 free(buf);
142 return s;
143 `}
144
145 redef fun to_s do return asctime.replace("\n", "")
146 end
147
148 # Date using the international format defined by ISO 8601.
149 #
150 # Usage:
151 #
152 # # By default ISODate at today.
153 # var date = new ISODate
154 # var tm = new Tm.localtime
155 # assert date.year == tm.year + 1900
156 # assert date.month == tm.mon + 1
157 # assert date.day == tm.mday
158 #
159 # # ISODate can be initialized from a String.
160 # date = new ISODate.from_string("1970-01-01T00:00:00Z")
161 # assert date.year == 1970
162 # assert date.month == 1
163 # assert date.day == 1
164 #
165 # # ISODate can be printed as String following the ISO format.
166 # assert date.to_s == "1970-01-01T00:00:00Z"
167 #
168 # Note that only the `YYYY-MM-DDTHH:MM:SSZ` is supported for now.
169 #
170 # See <http://www.w3.org/QA/Tips/iso-date>
171 class ISODate
172 super Comparable
173
174 # UTC years as Int.
175 var year: Int is noinit
176
177 # UTC months as Int (`1` for January).
178 var month: Int is noinit
179
180 # UTC days as Int (starting at `1`).
181 var day: Int is noinit
182
183 # UTC hours as Int.
184 var hours: Int is noinit
185
186 # UTC minutes as Int.
187 var minutes: Int is noinit
188
189 # UTC seconds as Int.
190 var seconds: Int is noinit
191
192 # UTC timezone marker.
193 #
194 # Note that I don't know what will happen if you change this value...
195 var timezone = "Z"
196
197 init do
198 var t = new Tm.localtime
199 year = 1900 + t.year
200 month = t.mon + 1
201 day = t.mday
202 hours = t.hour
203 minutes = t.min
204 seconds = t.sec
205 end
206
207 # Init `self` from a ISODate formatted string.
208 init from_string(str: String) do
209 year = str.substring(0, 4).to_i
210 month = str.substring(5, 2).to_i
211 day = str.substring(8, 2).to_i
212 hours = str.substring(11, 2).to_i
213 minutes = str.substring(14, 2).to_i
214 seconds = str.substring(17, 2).to_i
215 timezone = str.substring(19, str.length)
216 end
217
218 redef fun to_s do
219 var buff = new FlatBuffer
220 buff.append year.to_s
221 buff.add '-'
222 if month < 10 then buff.add '0'
223 buff.append month.to_s
224 buff.add '-'
225 if day < 10 then buff.add '0'
226 buff.append day.to_s
227 buff.add 'T'
228 if hours < 10 then buff.add '0'
229 buff.append hours.to_s
230 buff.add ':'
231 if minutes < 10 then buff.add '0'
232 buff.append minutes.to_s
233 buff.add ':'
234 if seconds < 10 then buff.add '0'
235 buff.append seconds.to_s
236 buff.append timezone
237 return buff.write_to_string
238 end
239
240 redef type OTHER: ISODate
241
242 # TODO handle timezones
243 redef fun <(o) do return to_s < o.to_s
244 end