X-Git-Url: http://nitlanguage.org?ds=sidebyside diff --git a/lib/standard/time.nit b/lib/standard/time.nit index e0b3867..adac6ff 100644 --- a/lib/standard/time.nit +++ b/lib/standard/time.nit @@ -14,7 +14,7 @@ # Management of time and dates module time -import string_search +import text import stream in "C Header" `{ @@ -23,12 +23,15 @@ in "C Header" `{ redef class Object # Unix time: the number of seconds elapsed since January 1, 1970 - protected fun get_time: Int is extern "kernel_Any_Any_get_time_0" + protected fun get_time: Int `{ return time(NULL); `} end redef class Sys # Wait a specific number of second and nanoseconds - fun nanosleep(sec, nanosec: Int) is extern "std_nanosleep" + fun nanosleep(sec, nanosec: Int) `{ + const struct timespec req = {sec, nanosec}; + nanosleep(&req, NULL); + `} end # Time since epoch @@ -41,20 +44,20 @@ extern class TimeT `{time_t`} new from_i(i: Int) `{ return i; `} # Update current time. - fun update `{ time(&recv); `} + fun update `{ time(&self); `} # Convert `self` to a human readable String. fun ctime: String import NativeString.to_s_with_copy `{ - return NativeString_to_s_with_copy( ctime(&recv) ); + return NativeString_to_s_with_copy( ctime(&self) ); `} # Difference in secondes from start (self if the end time) - fun difftime(start: TimeT): Float `{ return difftime(recv, start); `} + fun difftime(start: TimeT): Float `{ return difftime(self, start); `} redef fun to_s do return ctime.replace("\n", "") # Convert self to Int (expressed as seconds since epoch). - fun to_i: Int `{ return (int)recv; `} + fun to_i: Int `{ return (int)self; `} end # Time structure @@ -91,50 +94,49 @@ extern class Tm `{struct tm *`} `} # Convert `self` as a TimeT. - fun to_timet: TimeT `{ return mktime(recv); `} + fun to_timet: TimeT `{ return mktime(self); `} # Seconds after the minute. - fun sec: Int `{ return recv->tm_sec; `} + fun sec: Int `{ return self->tm_sec; `} # Minutes after the hour. - fun min: Int `{ return recv->tm_min; `} + fun min: Int `{ return self->tm_min; `} # hours since midnight. - fun hour: Int `{ return recv->tm_hour; `} + fun hour: Int `{ return self->tm_hour; `} # Day of the month. - fun mday: Int `{ return recv->tm_mday; `} + fun mday: Int `{ return self->tm_mday; `} # Months since January. - fun mon: Int `{ return recv->tm_mon; `} + fun mon: Int `{ return self->tm_mon; `} # Years since 1900. - fun year: Int `{ return recv->tm_year; `} + fun year: Int `{ return self->tm_year; `} # Days since Sunday. - fun wday: Int `{ return recv->tm_wday; `} + fun wday: Int `{ return self->tm_wday; `} # Days since January 1st. - fun yday: Int `{ return recv->tm_yday; `} + fun yday: Int `{ return self->tm_yday; `} # Is `self` in Daylight Saving Time. - fun is_dst: Bool `{ return recv->tm_isdst; `} + fun is_dst: Bool `{ return self->tm_isdst; `} # Convert `self` to a human readable String. fun asctime: String import NativeString.to_s_with_copy `{ - return NativeString_to_s_with_copy( asctime(recv) ); + return NativeString_to_s_with_copy( asctime(self) ); `} # Convert `self` to a human readable String corresponding to `format`. # TODO document allowed format. fun strftime(format: String): String import String.to_cstring, NativeString.to_s `{ char* buf, *c_format; - size_t res; buf = (char*)malloc(100); c_format = String_to_cstring(format); - res = strftime(buf, 100, c_format, recv); + strftime(buf, 100, c_format, self); String s = NativeString_to_s_with_copy(buf); free(buf); return s;