lib: extend time.nit with C's time_t and struct tm
authorAlexis Laferrière <alexis.laf@xymus.net>
Sat, 3 Aug 2013 12:37:03 +0000 (08:37 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 6 Aug 2013 13:40:19 +0000 (09:40 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/standard/time.nit

index 951d0e8..f391e51 100644 (file)
 # Management of time and dates
 package time
 
-import kernel
+import string_search
+
+in "C Header" `{
+       #include <time.h>
+`}
 
 redef class Object
        # Unix time: the number of seconds elapsed since January 1, 1970
@@ -24,3 +28,76 @@ redef class Sys
        # Wait a specific number of second and nanoseconds
        fun nanosleep(sec, nanosec: Int) is extern "std_nanosleep"
 end
+
+# Time since epoch
+extern class TimeT `{time_t`}
+       new `{ return time(NULL); `}
+       new from_i(i: Int) `{ return i; `}
+
+       fun update `{ time(&recv); `}
+
+       fun ctime: String import String::copy_from_native `{
+               return new_String_copy_from_native( ctime(&recv) );
+       `}
+
+       # Difference in secondes from start (self if the end time)
+       fun difftime(start: TimeT): Float `{ return difftime(recv, start); `}
+
+       redef fun to_s do return ctime.replace("\n", "")
+       fun to_i: Int `{ return (int)recv; `}
+end
+
+# Time structure
+extern class Tm `{struct tm *`}
+       new gmtime `{
+               struct tm *tm;
+               time_t t = time(NULL);
+               tm = gmtime(&t);
+               return tm;
+       `}
+       new gmtime_from_timet(t: TimeT) `{
+               struct tm *tm;
+               tm = gmtime(&t);
+               return tm;
+       `}
+
+       new localtime `{
+               struct tm *tm;
+               time_t t = time(NULL);
+               tm = localtime(&t);
+               return tm;
+       `}
+       new localtime_from_timet(t: TimeT) `{
+               struct tm *tm;
+               tm = localtime(&t);
+               return tm;
+       `}
+
+       fun to_timet: TimeT `{ return mktime(recv); `}
+
+       fun sec: Int `{ return recv->tm_sec; `}
+       fun min: Int `{ return recv->tm_min; `}
+       fun hour: Int `{ return recv->tm_hour; `}
+       fun mday: Int `{ return recv->tm_mday; `}
+       fun mon: Int `{ return recv->tm_mon; `}
+       fun year: Int `{ return recv->tm_year; `}
+       fun wday: Int `{ return recv->tm_wday; `}
+       fun yday: Int `{ return recv->tm_yday; `}
+       fun is_dst: Bool `{ return recv->tm_isdst; `}
+
+       fun asctime: String import String::copy_from_native `{
+               return new_String_copy_from_native( asctime(recv) );
+       `}
+       fun strftime(format: String): String import String::to_cstring, String::from_cstring `{
+               char* buf, *c_format;
+               size_t res;
+
+               buf = (char*)malloc(100);
+               c_format = String_to_cstring(format);
+
+               res = strftime(buf, 100, c_format, recv);
+               return new_String_from_cstring(buf);
+       `}
+
+       redef fun to_s do return asctime.replace("\n", "")
+end