1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Services to manipulate `Date`, `Time` and `DateTime`
17 # The services are split in 2 classes:
19 # * `Date` handles the year, month and day parts of the date.
20 # * `Time` handles the time in hours, minutes and seconds.
22 # These are united in `DateTime` for a precise time in a precise day.
25 # var now = new Time.now
26 # var midnight = new Time(0, 0, 0)
27 # assert now > midnight
29 # var nine_thirty = new Time(9, 30, 0)
30 # var eleven_twenty = new Time(11, 20, 0)
31 # assert eleven_twenty > nine_thirty
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
37 # var now_t = new DateTime.now
38 # var epoch = new DateTime(1970, 1, 1, 0, 0, 0)
39 # assert now_t > epoch
44 # A time of the day, composed of an `hour`, a `minute` and a `second` count
47 redef type OTHER: Time
49 # The hour part of this time, between 0 and 23
52 # The minute within the hour, between 0 and 59
55 # The second within the minute, between 0 and 59
58 # Get the current time of the day
60 var tm
= new Tm.localtime
66 # Get the difference between two times in second
67 fun diff_time
(other
: Time): Int do
68 return (hour
* 3600 + minute
* 60 + second
) -
69 (other
.hour
* 3600 + other
.minute
* 60 + other
.second
)
72 redef fun ==(d
) do return d
isa Time and time_eq
(d
)
74 redef fun <(d
) do return self.diff_time
(d
) < 0
76 redef fun hash
do return hour
* 1024 + minute
* 64 + second
78 private fun time_eq
(other
: Time): Bool
80 return hour
* 3600 + minute
* 60 + second
==
81 other
.hour
* 3600 + other
.minute
* 60 + other
.second
85 # A date, composed by a `year`, a `month` and a `day`
88 redef type OTHER: Date
93 # Month as an integer, `1` for January, `2` for February, etc.
101 # FIXME this value is not yet applied
104 # The date of this day
106 var tm
= new Tm.localtime
107 year
= 1900 + tm
.year
112 # `self` formatted according to ISO 8601
113 redef fun to_s
do return "{year}-{month}-{day}"
115 # Difference in days between `self` and `other`
116 fun diff_days
(other
: Date): Int
118 var y_out
= year
- other
.year
120 var m_out
= month
- other
.month
121 m_out
= m_out
* 30 # FIXME
122 return day
- other
.day
+ m_out
+ y_out
125 # Difference in months between `self` and `other`
126 fun diff_months
(other
: Date): Int
128 var y_out
= year
- other
.year
130 return month
- other
.month
+ y_out
133 # Difference in years between `self` and `other`
134 fun diff_years
(other
: Date): Int do return year
- other
.year
136 redef fun ==(d
) do return d
isa Date and self.diff_days
(d
) == 0
138 redef fun hash
do return year
+ month
* 1024 + day
* 2048
140 redef fun <(d
) do return self.diff_days
(d
) < 0
142 # Is `self` is between the years of `a` and `b`?
143 private fun is_between_years
(a
, b
: Date): Bool
145 return (a
.year
> year
and b
.year
< year
) or (b
.year
> year
and a
.year
< year
) or (a
.year
== year
or b
.year
== year
)
148 # Is `self` is between the months of `a` and `b`?
149 private fun is_between_months
(a
, b
: Date) : Bool
151 if not self.is_between_years
(a
,b
) then return false
152 return (a
.month
> month
and b
.month
< month
) or (b
.month
> month
and a
.month
< month
) or (a
.month
== month
or b
.month
== month
)
155 # Is `self` between `a` and `b`?
156 redef fun is_between
(a
, b
)
158 if not self.is_between_months
(a
, b
) then return false
159 return (a
.day
> day
and b
.day
< day
) or (b
.day
> day
and a
.day
< day
) or (a
.day
== day
or b
.day
== day
)
163 # A `Time` in a `Date`
167 redef type OTHER: DateTime
168 autoinit year
, month
, day
, hour
, minute
, second
170 # Get the current `DateTime`
177 redef fun ==(other
) do return other
isa DateTime and diff_days
(other
) == 0 and time_eq
(other
)
179 redef fun to_s
do return "{super} {hour}:{minute}:{second}{time_zone}"