README: document nit_env.sh
[nit.git] / lib / date.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Services to manipulate `Date`, `Time` and `DateTime`
16 #
17 # The services are split in 2 classes:
18 #
19 # * `Date` handles the year, month and day parts of the date.
20 # * `Time` handles the time in hours, minutes and seconds.
21 #
22 # These are united in `DateTime` for a precise time in a precise day.
23 #
24 # ~~~
25 # var now = new Time.now
26 # var midnight = new Time(0, 0, 0)
27 # assert now > midnight
28 #
29 # var nine_thirty = new Time(9, 30, 0)
30 # var eleven_twenty = new Time(11, 20, 0)
31 # assert eleven_twenty > nine_thirty
32 #
33 # var pi_day = new Date(2015, 03, 14)
34 # var may_the_fourth = new Date(2015, 5, 4)
35 # assert pi_day < may_the_fourth
36 #
37 # var now_t = new DateTime.now
38 # var epoch = new DateTime(1970, 1, 1, 0, 0, 0)
39 # assert now_t > epoch
40 # ~~~
41 #
42 module date
43
44 # A time of the day, composed of an `hour`, a `minute` and a `second` count
45 class Time
46 super Comparable
47 redef type OTHER: Time
48
49 # The hour part of this time, between 0 and 23
50 var hour: Int
51
52 # The minute within the hour, between 0 and 59
53 var minute: Int
54
55 # The second within the minute, between 0 and 59
56 var second: Int
57
58 # Get the current time of the day
59 init now do
60 var tm = new Tm.localtime
61 init(tm.hour, tm.min, tm.sec)
62 end
63
64 # Get the difference between two times in second
65 fun diff_time(other: Time): Int do
66 return (hour * 3600 + minute * 60 + second) -
67 (other.hour * 3600 + other.minute * 60 + other.second)
68 end
69
70 redef fun ==(d) do return d isa Time and time_eq(d)
71
72 redef fun <(d) do return self.diff_time(d) < 0
73
74 redef fun hash do return hour * 1024 + minute * 64 + second
75
76 private fun time_eq(other: Time): Bool
77 do
78 return hour * 3600 + minute * 60 + second ==
79 other.hour * 3600 + other.minute * 60 + other.second
80 end
81 end
82
83 # A date, composed by a `year`, a `month` and a `day`
84 class Date
85 super Comparable
86 redef type OTHER: Date
87
88 # Year, ex: 1989
89 var year: Int
90
91 # Month as an integer, `1` for January, `2` for February, etc.
92 var month: Int
93
94 # Day of the month
95 var day: Int
96
97 # UTC time zone
98 #
99 # FIXME this value is not yet applied
100 var time_zone = "Z"
101
102 # The date of this day
103 init today do
104 var tm = new Tm.localtime
105 init(1900 + tm.year, tm.mon + 1, tm.mday)
106 end
107
108 # `self` formatted according to ISO 8601
109 redef fun to_s do return "{year}-{month}-{day}"
110
111 # Difference in days between `self` and `other`
112 fun diff_days(other: Date): Int
113 do
114 var y_out = year - other.year
115 y_out = y_out * 365
116 var m_out = month - other.month
117 m_out = m_out * 30 # FIXME
118 return day - other.day + m_out + y_out
119 end
120
121 # Difference in months between `self` and `other`
122 fun diff_months(other: Date): Int
123 do
124 var y_out = year - other.year
125 y_out = y_out * 12
126 return month - other.month + y_out
127 end
128
129 # Difference in years between `self` and `other`
130 fun diff_years(other: Date): Int do return year - other.year
131
132 redef fun ==(d) do return d isa Date and self.diff_days(d) == 0
133
134 redef fun hash do return year + month * 1024 + day * 2048
135
136 redef fun <(d) do return self.diff_days(d) < 0
137
138 # Is `self` is between the years of `a` and `b`?
139 private fun is_between_years(a, b: Date): Bool
140 do
141 return (a.year > year and b.year < year) or (b.year > year and a.year < year) or (a.year == year or b.year == year)
142 end
143
144 # Is `self` is between the months of `a` and `b`?
145 private fun is_between_months(a, b: Date) : Bool
146 do
147 if not self.is_between_years(a,b) then return false
148 return (a.month > month and b.month < month) or (b.month > month and a.month < month) or (a.month == month or b.month == month)
149 end
150
151 # Is `self` between `a` and `b`?
152 redef fun is_between(a, b)
153 do
154 if not self.is_between_months(a, b) then return false
155 return (a.day > day and b.day < day) or (b.day > day and a.day < day) or (a.day == day or b.day == day)
156 end
157 end
158
159 # A `Time` in a `Date`
160 class DateTime
161 super Date
162 super Time
163 redef type OTHER: DateTime
164 autoinit year, month, day, hour, minute, second
165
166 # Get the current `DateTime`
167 init now
168 do
169 super
170 today
171 end
172
173 redef fun ==(other) do return other isa DateTime and diff_days(other) == 0 and time_eq(other)
174
175 redef fun to_s do return "{super} {hour}:{minute}:{second}{time_zone}"
176 end