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`}
34 new `{ return time(NULL); `}
35 new from_i
(i
: Int) `{ return i; `}
37 fun update `{ time(&recv); `}
39 fun ctime
: String import NativeString.to_s_with_copy
`{
40 return NativeString_to_s_with_copy( ctime(&recv) );
43 # Difference in secondes from start (self if the end time)
44 fun difftime
(start
: TimeT): Float `{ return difftime(recv, start); `}
46 redef fun to_s do return ctime.replace("\n", "")
47 fun to_i: Int `{ return (int)recv; `}
51 extern class Tm `{struct tm *`}
54 time_t t
= time
(NULL);
58 new gmtime_from_timet(t: TimeT) `{
66 time_t t
= time
(NULL);
70 new localtime_from_timet(t: TimeT) `{
76 fun to_timet: TimeT `{ return mktime(recv); `}
78 fun sec
: Int `{ return recv->tm_sec; `}
79 fun min: Int `{ return recv->tm_min; `}
80 fun hour
: Int `{ return recv->tm_hour; `}
81 fun mday: Int `{ return recv->tm_mday; `}
82 fun mon
: Int `{ return recv->tm_mon; `}
83 fun year: Int `{ return recv->tm_year; `}
84 fun wday
: Int `{ return recv->tm_wday; `}
85 fun yday: Int `{ return recv->tm_yday; `}
86 fun is_dst
: Bool `{ return recv->tm_isdst; `}
88 fun asctime: String import NativeString.to_s_with_copy `{
89 return NativeString_to_s_with_copy( asctime
(recv
) );
91 fun strftime(format: String): String import String.to_cstring, NativeString.to_s `{
95 buf
= (char
*)malloc
(100);
96 c_format
= String_to_cstring(format
);
98 res
= strftime
(buf
, 100, c_format
, recv
);
99 return NativeString_to_s(buf
);
102 redef fun to_s do return asctime.replace("\n", "")