calculator: intro android UI
[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 new `{ return time(NULL); `}
35 new from_i(i: Int) `{ return i; `}
36
37 fun update `{ time(&recv); `}
38
39 fun ctime: String import NativeString.to_s_with_copy `{
40 return NativeString_to_s_with_copy( ctime(&recv) );
41 `}
42
43 # Difference in secondes from start (self if the end time)
44 fun difftime(start: TimeT): Float `{ return difftime(recv, start); `}
45
46 redef fun to_s do return ctime.replace("\n", "")
47 fun to_i: Int `{ return (int)recv; `}
48 end
49
50 # Time structure
51 extern class Tm `{struct tm *`}
52 new gmtime `{
53 struct tm *tm;
54 time_t t = time(NULL);
55 tm = gmtime(&t);
56 return tm;
57 `}
58 new gmtime_from_timet(t: TimeT) `{
59 struct tm *tm;
60 tm = gmtime(&t);
61 return tm;
62 `}
63
64 new localtime `{
65 struct tm *tm;
66 time_t t = time(NULL);
67 tm = localtime(&t);
68 return tm;
69 `}
70 new localtime_from_timet(t: TimeT) `{
71 struct tm *tm;
72 tm = localtime(&t);
73 return tm;
74 `}
75
76 fun to_timet: TimeT `{ return mktime(recv); `}
77
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; `}
87
88 fun asctime: String import NativeString.to_s_with_copy `{
89 return NativeString_to_s_with_copy( asctime(recv) );
90 `}
91 fun strftime(format: String): String import String.to_cstring, NativeString.to_s `{
92 char* buf, *c_format;
93 size_t res;
94
95 buf = (char*)malloc(100);
96 c_format = String_to_cstring(format);
97
98 res = strftime(buf, 100, c_format, recv);
99 return NativeString_to_s(buf);
100 `}
101
102 redef fun to_s do return asctime.replace("\n", "")
103 end