python-chrono / chrono / calendar / calendar.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# -*- coding: utf-8 -*-
#
# python-chrono - a Python module for easy and convenient date/time handling
#
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
#

from __future__ import absolute_import
from __future__ import division

from .. import error
from .. import utility

import calendar
import datetime


class Calendar(object):
    """
    Base calendar class, with common calendar functionality.
    """

    @classmethod
    def fullyear(cls, year):
        """
        Converts a short 2-digit year to a full 4-digit year.
        *year* will be interpreted to be in range 1930-2029.

        Raises :exc:`chrono.error.YearError` if *year* is invalid.
        """

        year = utility.int_year(year)

        if year != 0:
            cls.validate_year(year)

        if year > 99:
            return year

        elif year >= 30:
            return 1900 + year

        elif year >= 0:
            return 2000 + year

        else:
            return year

    @classmethod
    def julian(cls, year, month, day):
        """
        Converts a date to a julian day number.

        Raises :exc:`chrono.error.YearError`, :exc:`chrono.error.MonthError` or
        :exc:`chrono.error.DayError` if *year*, *month* or *day* is invalid.
        """

        year = utility.int_year(year)
        month = utility.int_month(month)
        day = utility.int_day(day)

        cls.validate(year, month, day)

        a = (14 - month) // 12
        y = year + 4800 - a
        m = month + (12 * a) - 3
        p = day + (((153 * m) + 2) // 5) + (365 * y)
        q = (y // 4) - (y // 100) + (y // 400) - 32045

        return p + q

    @classmethod
    def julian_to_date(cls, julian):
        """
        Converts a julian day number to a date, returns a tuple of year,
        month, and day.

        Raises :exc:`chrono.error.DayError` if *julian* is invalid.
        """

        try:
            julian = int(julian)

        except ValueError:
            raise error.DayError("Invalid julian day '{0}'".format(julian))

        j = julian + 32044
        g = j // 146097
        dg = j % 146097
        c = (dg // 36524 + 1) * 3 // 4
        dc = dg - c * 36524
        b = dc // 1461
        db = dc % 1461
        a = (db // 365 + 1) * 3 // 4
        da = db - a * 365
        y = g * 400 + c * 100 + b * 4 + a
        m = (da * 5 + 308) // 153 - 2
        d = da - (m + 4) * 153 // 5 + 122

        year = int(y - 4800 + (m + 2) // 12)
        month = int((m + 2) % 12 + 1)
        day = int(d + 1)

        return (year, month, day)

    @classmethod
    def leapyear(cls, year):
        """
        Returns **True** if *year* is a leap year, otherwise **False**.

        Raises :exc:`chrono.error.YearError` if *year* is invalid.
        """

        cls.validate_year(year)

        return calendar.isleap(utility.int_year(year))

    @classmethod
    def monthdays(cls, year, month):
        """
        Returns the number of days in *month*. *year* is needed to handle
        leap years.

        Raises :exc:`chrono.error.YearError` or :exc:`chrono.error.MonthError`
        if *year* or *month* is invalid.
        """

        year = utility.int_year(year)
        month = utility.int_month(month)

        cls.validate_year(year)
        cls.validate_month(month)

        return calendar.monthrange(
            utility.int_year(year),
            utility.int_month(month)
        )[1]

    @classmethod
    def monthname(cls, month, short=False):
        """
        Returns the name of *month*, according to the current system locale.
        If *short* is **True**, returns the abbreviated month name.

        Raises :exc:`chrono.error.MonthError` if *month* is invalid.
        """

        month = utility.int_month(month)

        cls.validate_month(month)

        d = datetime.date(2000, month, 1)

        return d.strftime(short and "%b" or "%B")

    @classmethod
    def ordinal(cls, year, month, day):
        """
        Returns the ordinal day for the given date.

        Raises :exc:`chrono.error.YearError`, :exc:`chrono.error.MonthError` or
        :exc:`chrono.error.DayError` if *year*, *month* or *day* is invalid.
        """

        cls.validate(year, month, day)

        ordinal = 0

        for m in range(1, utility.int_month(month)):
            ordinal += cls.monthdays(utility.int_year(year), m)

        ordinal += utility.int_day(day)

        return ordinal

    @classmethod
    def ordinal_to_date(cls, year, day):
        """
        Converts an ordinal day to a date, returned as a tuple of year,
        month, and day.

        Raises :exc:`chrono.error.YearError` or :exc:`chrono.error.DayError`
        if *year* or *day* is invalid.
        """

        day = utility.int_day(day)

        cls.validate_ordinal(year, day)

        dt = datetime.date(
            year=utility.int_year(year),
            month=1,
            day=1
        )

        if day > 1:
            dt += datetime.timedelta(days=day - 1)

        return (dt.year, dt.month, dt.day)

    @classmethod
    def validate(cls, year, month, day):
        """
        Validates a date: *year* must be in range 1-9999, *month* in range
        1-12, and *day* in range 1-31, depending on *year* and *month*.

        Raises :exc:`chrono.error.YearError`, :exc:`chrono.error.MonthError`
        or :exc:`chrono.error.DayError` on invalid input.
        """

        cls.validate_year(year)
        cls.validate_month(month)

        monthdays = cls.monthdays(year, month)

        if not 1 <= utility.int_day(day) <= monthdays:
            raise error.DayError(
                "Day '{0}' not in range 1-{1} for year {2}, month {3}"
                .format(day, monthdays, year, month)
            )

    @classmethod
    def validate_month(cls, month):
        """
        Validates *month*: must be in range 1-12.

        Raises :exc:`chrono.error.MonthError` on invalid input.
        """

        if not 1 <= utility.int_month(month) <= 12:
            raise error.MonthError(
                "Month '{0}' not in range 1-12".format(month)
            )

    @classmethod
    def validate_ordinal(cls, year, day):
        """
        Validates an ordinal date: *year* must be in range 1-9999, *day* must
        be in range 1-365, or 366 if *year* is a leap year.

        Raises :exc:`chrono.error.YearError` or :exc:`chrono.error.DayError` on
        invalid input.
        """

        cls.validate_year(year)

        yeardays = cls.yeardays(year)

        if not 1 <= utility.int_day(day) <= yeardays:
            raise error.DayError(
                "Ordinal day '{0}' not in range 1-{1} for year '{2}'"
                .format(day, yeardays, year)
            )

    @classmethod
    def validate_week(cls, year, week):
        """
        Validates a week: *year* must be in range 1-9999, and *week* must be
        in range 1-53, depending on *year*.

        Raises :exc:`chrono.error.YearError` or :exc:`chrono.error.WeekError`
        if *year* or *week* is invalid.
        """

        cls.validate_year(year)

        weeks = cls.weeks(year)

        if not 1 <= utility.int_week(week) <= weeks:
            raise error.WeekError(
                "Week '{0}' not in range 1-{1} for year '{2}'"
                .format(week, weeks, year)
            )

    @classmethod
    def validate_weekdate(cls, year, week, weekday):
        """
        Validates a weekdate: *year* must be in range 1-9999, *week* must be
        in range 1-53, depending on *year*, and *weekday* must be in range
        1-7.

        Raises :exc:`chrono.error.YearError`, :exc:`chrono.error.WeekError`, or
        :exc:`chrono.error.DayError` if *year*, *week*, or *weekday* is
        invalid.
        """

        cls.validate_week(year, week)
        cls.validate_weekday(weekday)

    @classmethod
    def validate_weekday(cls, day):
        """
        Validates a week day: *day* must be in range 1-7.

        Raises :exc:`chrono.error.DayError` if *day* is invalid.
        """

        if not 1 <= utility.int_day(day) <= 7:
            raise error.DayError("Weekday '{0}' not in range 1-7")

    @classmethod
    def validate_year(cls, year):
        """
        Validates *year*: must be in range 1-9999.

        Raises :exc:`chrono.error.YearError` if *year* is invalid.
        """

        if not 1 <= utility.int_year(year) <= 9999:
            raise error.YearError(
                "Year '{0}' not in range 1-9999".format(year)
            )

    @classmethod
    def week(cls, year, month, day):
        """
        Returns the week containing the given date as a tuple of year and
        week.

        Raises :exc:`chrono.error.YearError`, :exc:`chrono.error.MonthError`,
        or :exc:`chrono.error.DayError` if *year*, *month*, or *day* is
        invalid.
        """

        return cls.weekdate(year, month, day)[:2]

    @classmethod
    def week_to_date(cls, year, week):
        """
        Returns the date of the first day in the given week as a tuple of
        year, month, and day.

        Raises :exc:`chrono.error.YearError` or :exc:`chrono.error.WeekError`
        if *year* or *week* is invalid.
        """

        return cls.weekdate_to_date(year, week, 1)

    @classmethod
    def weekdate(cls, year, month, day):
        """
        Returns the weekdate for the given date as a tuple with year, week,
        and weekday.

        Raises :exc:`chrono.error.YearError`, :exc:`chrono.error.MonthError`,
        or :exc:`chrono.error.DayError` if *year*, *month* or *day* is
        invalid.

        .. note:: This is a placeholder method which just raises
           :exc:`NotImplementedError`, it is implemented in
           calendar-specific subclasses.
        """

        raise NotImplementedError(
            "This is a calendar-specific method to be handled in subclasses"
        )

    @classmethod
    def weekdate_to_date(cls, year, week, day):
        """
        Returns the date of the given weekdate as a tuple with year, month,
        and day.

        Raises :exc:`chrono.error.YearError`, :exc:`chrono.error.WeekError`,
        or :exc:`chrono.error.DayError` if *year*, *week* or *day* is invalid.

        .. note:: This is a placeholder method which just raises
           :exc:`NotImplementedError`, it is implemented in
           calendar-specific subclasses.
        """

        raise NotImplementedError(
            "This is a calendar-specific method to be handled in subclasses"
        )

    @classmethod
    def weekday(cls, year, month, day):
        """
        Returns the weekday of the given date.

        Raises :exc:`chrono.error.YearError`, :exc:`chrono.error.MonthError`,
        or :exc:`chrono.error.DayError` if *year*, *month*, or *day* is
        invalid.
        """

        return cls.weekdate(year, month, day)[2]

    @classmethod
    def weekdayname(cls, weekday, short=False):
        """
        Returns the weekday name of the given weekday. If *short*
        is **True**, returns the abbreviated weekday name.

        Raises :exc:`chrono.error.DayError` if *weekday* is invalid.

        .. note:: This is a placeholder method which just raises
           :exc:`NotImplementedError`, it is implemented in
           calendar-specific subclasses.
        """

        raise NotImplementedError(
            "This is a calendar-specific method to be handled in subclasses"
        )

    @classmethod
    def weeks(cls, year):
        """
        Returns the number of weeks in *year*.

        Raises :exc:`chrono.error.YearError` if *year* is invalid.

        .. note:: This is a placeholder method which just raises
           :exc:`NotImplementedError`, it is implemented in
           calendar-specific subclasses.
        """

        raise NotImplementedError(
            "This is a calendar-specific method to be handled in subclasses"
        )

    @classmethod
    def yeardays(cls, year):
        """
        Returns the number of days in *year*.

        Raises :exc:`chrono.error.YearError` if *year* is invalid.
        """

        return cls.leapyear(year) and 366 or 365
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.