Source code for timelinelib.calendar.gregorian.gregorian

# -*- coding: utf-8 -*-
# Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  Rickard Lindberg, Roger Lindberg
#
# This file is part of Timeline.
#
# Timeline is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Timeline is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Timeline.  If not, see <http://www.gnu.org/licenses/>.

"""
Contains the GregorianDateTime class and static functions related
to the class.

:doc:`Tests are found here <unit_calendar_gregorian_gregorian>`.
"""

from timelinelib.calendar.gregorian.time import GregorianDelta
from timelinelib.calendar.gregorian.time import GregorianTime
from timelinelib.calendar.gregorian.gregorian2julian import gregorian_ymd_to_julian_day
from timelinelib.calendar.gregorian.julian2gregorian import julian_day_to_gregorian_ymd


[docs]class GregorianDateTime: """ """
[docs] def __init__(self, year, month, day, hour, minute, second): """ """ if not is_valid(year, month, day): raise ValueError("Invalid gregorian date %s-%s-%s" % (year, month, day)) self.year = year self.month = month self.day = day self.hour = hour self.minute = minute self.second = second
[docs] def __eq__(self, other): """ """ return (isinstance(other, self.__class__) and self.to_tuple() == other.to_tuple())
[docs] def __ne__(self, other): """ """ return not (self == other)
[docs] @classmethod def from_ymd(cls, year, month, day): """ """ return cls(year, month, day, 0, 0, 0)
[docs] @classmethod def from_time(cls, time): """ """ (year, month, day) = julian_day_to_gregorian_ymd(time.julian_day) (hour, minute, second) = time.get_time_of_day() return cls(year, month, day, hour, minute, second)
@property def week_number(self): """ """ def monday_week_1(year): jan_4 = GregorianDateTime.from_ymd(year, 1, 4).to_time() return jan_4 - GregorianDelta.from_days(jan_4.day_of_week) def days_between(end, start): return end.julian_day - start.julian_day def days_since_monday_week_1(time): year = GregorianDateTime.from_time(time).year diff = days_between(end=time, start=monday_week_1(year + 1)) if diff >= 0: return diff diff = days_between(end=time, start=monday_week_1(year)) if diff >= 0: return diff diff = days_between(end=time, start=monday_week_1(year - 1)) if diff >= 0: return diff raise ValueError("should not end up here") return days_since_monday_week_1(self.to_time()) // 7 + 1
[docs] def is_bc(self): """ """ return self.year <= 0
[docs] def replace(self, year=None, month=None): """ """ if year is None: year = self.year if month is None: month = self.month return self.__class__( year, month, self.day, self.hour, self.minute, self.second )
[docs] def days_in_month(self): """ """ return days_in_month(self.year, self.month)
[docs] def to_tuple(self): """ """ return self.year, self.month, self.day, self.hour, self.minute, self.second
[docs] def to_date_tuple(self): """ """ return self.year, self.month, self.day
[docs] def to_time_tuple(self): """ """ return self.hour, self.minute, self.second
[docs] def to_time(self): """ """ days = gregorian_ymd_to_julian_day(self.year, self.month, self.day) seconds = self.hour * 60 * 60 + self.minute * 60 + self.second return GregorianTime(days, seconds)
[docs] def is_first_day_in_year(self): """ """ return (self.month == 1 and self.day == 1 and self.hour == 0 and self.minute == 0 and self.second == 0)
[docs] def is_first_of_month(self): """ """ return (self.day == 1 and self.hour == 0 and self.minute == 0 and self.second == 0)
[docs] def __repr__(self): """ """ return "GregorianDateTime<%d-%02d-%02d %02d:%02d:%02d>" % self.to_tuple()
[docs]def days_in_month(year, month): """ """ if month in [4, 6, 9, 11]: return 30 if month in [1, 3, 5, 7, 8, 10, 12]: return 31 if is_leap_year(year): return 29 return 28
[docs]def is_leap_year(year): """ """ return year % 4 == 0 and (year % 400 == 0 or not year % 100 == 0)
[docs]def is_valid_time(hour, minute, second): """ """ return 0 <= hour < 24 and 0 <= minute < 60 and 0 <= second < 60
[docs]def is_valid(year, month, day): """ """ return 1 <= month <= 12 and 1 <= day <= days_in_month(year, month)