1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2008 Floréal Morandat <morandat@lirmm.fr>
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
13 # Management of time and dates
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"
28 # Wait a specific number of second and nanoseconds
29 fun nanosleep
(sec
, nanosec
: Int) is extern "std_nanosleep"
33 extern class TimeT `{time_t`}
35 # Returns Time since epoch from now.
36 new `{ return time(NULL); `}
38 # Returns Time since epoch from `i` (expressed in seconds).
39 new from_i
(i
: Int) `{ return i; `}
41 # Update current time.
42 fun update `{ time(&recv); `}
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) );
49 # Difference in secondes from start (self if the end time)
50 fun difftime
(start
: TimeT): Float `{ return difftime(recv, start); `}
52 redef fun to_s do return ctime.replace("\n", "")
54 # Convert self to Int (expressed as seconds since epoch).
55 fun to_i: Int `{ return (int)recv; `}
59 extern class Tm `{struct tm *`}
61 # Create a new Time structure expressed in Coordinated Universal Time (UTC).
64 time_t t
= time
(NULL);
69 # Create a new Time structure expressed in UTC from `t
`.
70 new gmtime_from_timet(t: TimeT) `{
76 # Create a new Time structure expressed in the local timezone.
79 time_t t
= time
(NULL);
84 # Create a new Time structure expressed in the local timezone from `t
`.
85 new localtime_from_timet(t: TimeT) `{
91 # Convert `self` as a TimeT.
92 fun to_timet: TimeT `{ return mktime(recv); `}
94 # Seconds after the minute.
95 fun sec
: Int `{ return recv->tm_sec; `}
97 # Minutes after the hour.
98 fun min: Int `{ return recv->tm_min; `}
100 # hours since midnight.
101 fun hour
: Int `{ return recv->tm_hour; `}
104 fun mday: Int `{ return recv->tm_mday; `}
106 # Months since January.
107 fun mon
: Int `{ return recv->tm_mon; `}
110 fun year: Int `{ return recv->tm_year; `}
113 fun wday
: Int `{ return recv->tm_wday; `}
115 # Days since January 1st.
116 fun yday: Int `{ return recv->tm_yday; `}
118 # Is `self` in Daylight Saving Time.
119 fun is_dst
: Bool `{ return recv->tm_isdst; `}
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
) );
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
;
132 buf
= (char
*)malloc
(100);
133 c_format
= String_to_cstring(format
);
135 res
= strftime
(buf
, 100, c_format
, recv
);
136 return NativeString_to_s(buf
);
139 redef fun to_s do return asctime.replace("\n", "")