Merge: Android UI, notifications, toasts and calculator
[nit.git] / lib / standard / time.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2008 Floréal Morandat <morandat@lirmm.fr>
4 #
5 # This file is free software, which comes along with NIT. This software is
6 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 # is kept unaltered, and a notification of the changes is added.
10 # You are allowed to redistribute it and sell it, alone or is a part of
11 # another product.
12
13 # Management of time and dates
14 module time
15
16 import string_search
17
18 in "C Header" `{
19 #include <time.h>
20 `}
21
22 redef class Object
23 # Unix time: the number of seconds elapsed since January 1, 1970
24 protected fun get_time: Int is extern "kernel_Any_Any_get_time_0"
25 end
26
27 redef class Sys
28 # Wait a specific number of second and nanoseconds
29 fun nanosleep(sec, nanosec: Int) is extern "std_nanosleep"
30 end
31
32 # Time since epoch
33 extern class TimeT `{time_t`}
34
35 # Returns Time since epoch from now.
36 new `{ return time(NULL); `}
37
38 # Returns Time since epoch from `i` (expressed in seconds).
39 new from_i(i: Int) `{ return i; `}
40
41 # Update current time.
42 fun update `{ time(&recv); `}
43
44 # Convert `self` to a human readable String.
45 fun ctime: String import NativeString.to_s_with_copy `{
46 return NativeString_to_s_with_copy( ctime(&recv) );
47 `}
48
49 # Difference in secondes from start (self if the end time)
50 fun difftime(start: TimeT): Float `{ return difftime(recv, start); `}
51
52 redef fun to_s do return ctime.replace("\n", "")
53
54 # Convert self to Int (expressed as seconds since epoch).
55 fun to_i: Int `{ return (int)recv; `}
56 end
57
58 # Time structure
59 extern class Tm `{struct tm *`}
60
61 # Create a new Time structure expressed in Coordinated Universal Time (UTC).
62 new gmtime `{
63 struct tm *tm;
64 time_t t = time(NULL);
65 tm = gmtime(&t);
66 return tm;
67 `}
68
69 # Create a new Time structure expressed in UTC from `t`.
70 new gmtime_from_timet(t: TimeT) `{
71 struct tm *tm;
72 tm = gmtime(&t);
73 return tm;
74 `}
75
76 # Create a new Time structure expressed in the local timezone.
77 new localtime `{
78 struct tm *tm;
79 time_t t = time(NULL);
80 tm = localtime(&t);
81 return tm;
82 `}
83
84 # Create a new Time structure expressed in the local timezone from `t`.
85 new localtime_from_timet(t: TimeT) `{
86 struct tm *tm;
87 tm = localtime(&t);
88 return tm;
89 `}
90
91 # Convert `self` as a TimeT.
92 fun to_timet: TimeT `{ return mktime(recv); `}
93
94 # Seconds after the minute.
95 fun sec: Int `{ return recv->tm_sec; `}
96
97 # Minutes after the hour.
98 fun min: Int `{ return recv->tm_min; `}
99
100 # hours since midnight.
101 fun hour: Int `{ return recv->tm_hour; `}
102
103 # Day of the month.
104 fun mday: Int `{ return recv->tm_mday; `}
105
106 # Months since January.
107 fun mon: Int `{ return recv->tm_mon; `}
108
109 # Years since 1900.
110 fun year: Int `{ return recv->tm_year; `}
111
112 # Days since Sunday.
113 fun wday: Int `{ return recv->tm_wday; `}
114
115 # Days since January 1st.
116 fun yday: Int `{ return recv->tm_yday; `}
117
118 # Is `self` in Daylight Saving Time.
119 fun is_dst: Bool `{ return recv->tm_isdst; `}
120
121 # Convert `self` to a human readable String.
122 fun asctime: String import NativeString.to_s_with_copy `{
123 return NativeString_to_s_with_copy( asctime(recv) );
124 `}
125
126 # Convert `self` to a human readable String corresponding to `format`.
127 # TODO document allowed format.
128 fun strftime(format: String): String import String.to_cstring, NativeString.to_s `{
129 char* buf, *c_format;
130 size_t res;
131
132 buf = (char*)malloc(100);
133 c_format = String_to_cstring(format);
134
135 res = strftime(buf, 100, c_format, recv);
136 return NativeString_to_s(buf);
137 `}
138
139 redef fun to_s do return asctime.replace("\n", "")
140 end