date :: Date :: defaultinit
# A date, composed by a `year`, a `month` and a `day`
class Date
super Comparable
redef type OTHER: Date
# Year, ex: 1989
var year: Int
# Month as an integer, `1` for January, `2` for February, etc.
var month: Int
# Day of the month
var day: Int
# UTC time zone
#
# FIXME this value is not yet applied
var time_zone = "Z"
# The date of this day
init today do
var tm = new Tm.localtime
init(1900 + tm.year, tm.mon + 1, tm.mday)
end
# `self` formatted according to ISO 8601
redef fun to_s do return "{year}-{month}-{day}"
# Difference in days between `self` and `other`
fun diff_days(other: Date): Int
do
var y_out = year - other.year
y_out = y_out * 365
var m_out = month - other.month
m_out = m_out * 30 # FIXME
return day - other.day + m_out + y_out
end
# Difference in months between `self` and `other`
fun diff_months(other: Date): Int
do
var y_out = year - other.year
y_out = y_out * 12
return month - other.month + y_out
end
# Difference in years between `self` and `other`
fun diff_years(other: Date): Int do return year - other.year
redef fun ==(d) do return d isa Date and self.diff_days(d) == 0
redef fun hash do return year + month * 1024 + day * 2048
redef fun <(d) do return self.diff_days(d) < 0
# Is `self` is between the years of `a` and `b`?
private fun is_between_years(a, b: Date): Bool
do
return (a.year > year and b.year < year) or (b.year > year and a.year < year) or (a.year == year or b.year == year)
end
# Is `self` is between the months of `a` and `b`?
private fun is_between_months(a, b: Date) : Bool
do
if not self.is_between_years(a,b) then return false
return (a.month > month and b.month < month) or (b.month > month and a.month < month) or (a.month == month or b.month == month)
end
# Is `self` between `a` and `b`?
redef fun is_between(a, b)
do
if not self.is_between_months(a, b) then return false
return (a.day > day and b.day < day) or (b.day > day and a.day < day) or (a.day == day or b.day == day)
end
end
lib/date/date.nit:83,1--157,3