6.8. ISO 8601 Dates and Times

This module defines special classes for handling ISO 8601 dates and times.

class pyslet.iso8601.Date(src=None, base=None, bce=False, century=None, decade=None, year=None, month=None, day=None, week=None, weekday=None, ordinal_day=None, absolute_day=None, xdigits=None, **kwargs)

Bases: pyslet.pep8.PEP8Compatibility, pyslet.py2.SortableMixin, pyslet.py2.UnicodeMixin

A class for representing ISO dates.

Values can represent dates with reduced precision, for example:

Date(century=20, year=13, month=12)

represents December 2013, no specific day.

There are a number of different forms of the constructor based on named parameters, the simplest is:

Date(century=19, year=69, month=7, day=20)

You can also use weekday format (decade must be provided separately when using this format):

Date(century=19, decade=6, year=9, week=29, weekday=7)

…ordinal format (where day 1 is 1st Jan):

Date(century=19, year=69, ordinal_day=201)

…absolute format (where day 1 is the notional 1st Jan 0001):

Date(absolute_day=718998)

An empty constructor is equivalent to:

Date() == Date(absolute_day=1)      # True

By default the calendar used supports dates in the range 0001-01-01 through to 9999-12-31. ISO 8601 allows this range to be extended by agreement using a fixed number of additional digits in the century specification. These dates are referred to as expanded dates and they include provision for negative, as well as larger positive, years using the astronomical convention of including a year 0.

Given that the use of expanded dates can only be done by agreement the constructor supports an additional parameter to enable you to construct a date using an agreed number of additional digits:

Date(century=19, year=69, month=7, day=20, xdigits=2)

The above instance represents the same date as the previous examples but with an expanded century representation consisting of an additional two decimal digits. Using xdigits=2 the range of allowable dates is now -999999-01-01 to +999999-12-31. Notice that ISO 8601 uses the leading +/- to indicate the use of an expanded form. If one instance is used to create another, e.g., using offset() or the base parameter described below then value of xdigits is copied from the existing instance to the new one.

It is acceptable for xdigits to be set to 0, this indicates expanded dates with no additional decimal digits but has the effect of extending the default range to -9999-01-01 to +9999-12-31.

When constructing instances for negative years you must set the bce flag on the constructor (indicating that the date is “before common era”). The values passed for century and year (and optionally decade when using weekday form) must always be positive as they would be written in the documented ISO decimal forms.

Expanded dates include the year 0 (as per ISO 8601). As a result, the common meaning of 1 BCE would be year 0, not year -1. To represent the year 753 BCE you would use:

Date(bce=True, century=7, year=52, xdigits=0)

For year 0, the bce flag can be set either way (or omitted).

The constructor includes a wildcard form of expansion using the special value -1 for xdigits. Such dates are assumed to have been represented in the minimum number of decimal digits for the century (but not less than 4) and will accept a century of any size.

All constructors, except the absolute form, allow the passing of a base date which allows the most-significant values to be omitted, (truncated forms) for example:

base = Date(century=19, year=69, month=7, day=20)
new_date = Date(day=21, base=base)  #: 21st July 1969

base always represents a date on or before the newly constructed date, so:

base = Date(century=19, year=99, month=12, day=31)
new_date = Date(day=5, base=base)

constructs a Date representing the 5th January 2000. These truncated forms cannot be used with xdigits as the century is never present so cannot be expanded. However, the value of base may be an expanded date and the result is another expanded date with the same xdigits constraint. Caution is required when dealing with negative dates:

base = Date(bce=True, century=7, year=52, month=4, day=21, xdigits=0)
new_date = Date(year=53, month=4, day=21, base=base)

results in the date -0653-04-21 and not -0753-04-21 because the year -0753 would have been before the base year -0752.

Given that Date can hold imprecise dates, there is some ambiguity over the comparisons between things such as January 1985 and Week 3 1985. Although at first sight it may be tempting to declare 1st April to be greater than March, it is harder to determine the relationship between 1st April and April itself. Especially if a complete ordering is required.

The approach taken here is to disallow comparisons between dates with different precisions.

Date objects are immutable and so can be used as the keys in dictionaries provided they all share the same precision.

Some older functions did allow modification but these now raise an error with an appropriate suggested refactoring.

Instances can be converted directly to strings using the default, extended calendar format. Other formats are supported through format specific methods.

xdigits = None

the number of expanded digits in the century

bce = None

BCE flag (before common era)

century = None

the century

year = None

the year, 0..99

month = None

the month, 1..12 (for dates stored in calendar form)

week = None

the week (for dates stored in week form)

Fully specified dates are always stored in calendar form but instances can represent reduced precision dates in week format, e.g., 2016-W01. In these cases, day and month will be None and the week will be recorded instead.

day = None

the day, 1..31

get_absolute_day()

Return a notional day number

The number 1 being the 0001-01-01 which is the base day of our calendar.

get_calendar_day()

Returns a tuple of: (century, year, month, day)

get_xcalendar_day()

Returns a tuple of: (bce, century, year, month, day)

get_ordinal_day()

Returns a tuple of (century, year, ordinal_day)

get_xordinal_day()

Returns a tuple of (century,year,ordinal_day)

get_week_day()

Returns a tuple of (century, decade, year, week, weekday), note that Monday is 1 and Sunday is 7

get_xweek_day()

Returns a tuple of (bce, century, decade, year, week, weekday), note that Monday is 1 and Sunday is 7

expand(xdigits)

Constructs a new expanded instance

The purpose of this method is to create a new instance from an existing Date but with a different expansion (value of xdigits).

The resulting value must still satisfy the constraints imposed by the new xdigits value. In particular, if you pass xdigits=None the new instance will not be expanded and must be in the range 0001-01-01 to 9999-12-31.

classmethod from_struct_time(t)

Constructs a Date from a struct_time, such as might be returned from time.gmtime() and related functions.

update_struct_time(t)

update_struct_time changes the year, month, date, wday and ydat fields of t, a struct_time, to match the values in this date.

classmethod from_now()

Constructs a Date from the current local time.

offset(centuries=0, years=0, months=0, weeks=0, days=0)

Adds an offset

Constructs a Date from the given date + a given offset.

There are significant limitations on this method to avoid ambiguous outcomes such as adding 1 year to a leap day such as 2016-02-29.

A fully specified date can be offset by days or weeks, in the latter case all weeks have 7 days so this is always unambiguous.

Dates known only to week precision can only be offset by weeks.

Dates with month precision can be offset by months, years or centuries because every year has exactly the same number of months. The concept of February next year is always meaningful (unlike the meaning of 29th Feb next year or the similarly problematic week 53 next year).

Dates with year precision can be offset by years or centuries and, for completeness, dates with century precision can only be offset by centuries.

Creating an offset date from an expanded date always results in another expanded date (with the same xdigits value).

classmethod from_str(src, base=None, xdigits=None)

Parses a Date instance from a src string.

classmethod from_string_format(src, base=None, xdigits=None)

Similar to from_str() except that a tuple is returned, the first item is the resulting Date instance, the second is a string describing the format parsed. For example:

d,f=Date.from_string_format("1969-07-20")
# f is set to "YYYY-MM-DD". 
get_calendar_string(basic=False, truncation=0)

Formats this date using calendar form, for example 1969-07-20

basic
True/False, selects basic form, e.g., 19690720. Default is False. Expanded dates that use the non-conformant xdigits=-1 mode are not compatible with basic formatting.
truncation
One of the Truncation constants used to select truncated forms of the date. For example, if you specify Truncation.Year you’ll get –07-20 or –0720. Default is NoTruncation.

Calendar format only supports Century, Year and Month truncated forms.

get_ordinal_string(basic=False, truncation=0)

Formats this date using ordinal form, for example 1969-201

basic
True/False, selects basic form, e.g., 1969201. Default is False
truncation
One of the Truncation constants used to select truncated forms of the date. For example, if you specify Truncation.Year you’ll get -201. Default is NoTruncation.

Note that ordinal format only supports century and year truncated forms.

get_week_string(basic=False, truncation=0)

Formats this date using week form, for example 1969-W29-7

basic
True/False, selects basic form, e.g., 1969W297. Default is False
truncation
One of the Truncation constants used to select truncated forms of the date. For example, if you specify Truncation.Year you’ll get -W297. Default is NoTruncation.

Note that week format only supports century, decade, year and week truncated forms.

classmethod from_julian(year, month, day, xdigits=None)

Constructs a Date from a year, month and day expressed in the Julian calendar.

If the year is 0 or negative you must provide a value for xdigits in order to construct an expanded date.

get_julian_day()

Returns a tuple of: (year,month,day) representing the equivalent date in the Julian calendar.

static split_year(year)

Static method that splits an integer year into a 3-tuple

Returns:

(bce, century, year)

Can be used as a convenience when constructing new instances:

int_year = -752
bce, century, year = Date.split_year(int_year)
d = Date(bce=bce, century=century, year=year, month=4, day=21,
         xdigits=2)
str(d) == "-000752-04-21"     # True
leap_year()

leap_year returns True if this date is (in) a leap year and False otherwise.

Note that leap years fall on all years that divide by 4 except those that divide by 100 but including those that divide by 400.

complete()

Returns True if this date has a complete representation, i.e., does not use one of the reduced precision forms.

get_precision()

Returns one of the Precision constants representing the precision of this date.

class pyslet.iso8601.Time(src=None, hour=None, minute=None, second=None, total_seconds=None, zdirection=None, zhour=None, zminute=None, **kwargs)

Bases: pyslet.pep8.PEP8Compatibility, pyslet.py2.UnicodeMixin, pyslet.py2.SortableMixin

A class for representing ISO times

Values can represent times with reduced precision, for example:

Time(hour=20)

represents 8pm without a specific minute/seconds value.

There are a number of different forms of the constructor based on named parameters, the simplest is:

Time(hour=20, minute=17, second=40)

Indicate UTC (Zulu time) by providing a zone direction of 0:

Time(hour=20, minute=17, second=40, zdirection=0)

To indicate a UTC offset provide additional values for hours (and optionally minutes) with 1 or -1 for zdirection to indicate the direction of the shift. 1 indicates a more Easterly timezone, -1 indicates a more Westerly zone:

Time(hour=15, minute=17, second=40, zdirection=-1, zhour=5,
     zminute=0)

A UTC offset of 0 hours and minutes results in a value that compares as equal to the corresponding Zulu time but is formatted using an explicit offset by str() rather than using the canonical “Z” form.

You may also specify a total number of seconds past midnight (no zone):

Time(total_seconds=73060)

If total_seconds overflows an error is raised. To create a time from an arbitrary number of seconds and catch overflow use offset instead:

Time(total_seconds=159460)
# raises DateTimeError

t, overflow = Time().offset(seconds=159460)
# sets t to 20:40:17 and overflow=1

Time supports two representations of midnight: 00:00:00 and 24:00:00 in keeping with the ISO specification. These are considered equivalent by comparisons!

Truncated forms can be created directly from the base time, see extend() for more information.

Comparisons are dealt with in a similar way to Date in that times must have the same precision to be comparable. Although this behaviour is consistent it might seem strange at first as it rules out comparing 09:00:15 with 09:00 but, in effect, 09:00 is actually all times in the range 09:00:00-09:00:59.999….

Zones further complicate this method but the rule is very simple, we only ever compare times from the same zone (or if both have unspecified zones). There is one subtlety to this implementation. Times stored with a redundant +00:00 or -00:00 are treated the same as those with a zero zone direction (Zulu time).

Time objects are immutable and so can be used as the keys in dictionaries provided they all share the same precision.

Some older functions did allow modification but these have been deprecated. Use python -Wd to force warnings from these unsafe methods.

Instances can be converted directly to strings or unicode strings using the default, extended format. Other formats are supported through format specific methods.

hour = None

the hour, 0..24

minute = None

the minute, 0..59

second = None

the seconds, 0..60 (to include leap seconds)

zoffset = None

the difference in minutes to UTC

get_total_seconds()

Note that leap seconds are handled as if they were invisible, e.g., 23:00:60 returns the same total seconds as 23:00:59.

get_time()

Returns a tuple of (hour,minute,second).

Times with reduced precision will return None for second and or minute.

get_zone()

Returns a tuple of:

(zdirection, zoffset)

zdirection is defined as per Time’s constructor, zoffset is a non-negative integer minute offset or None, if the zone is unspecified for this Time.

get_zone_offset()

Returns a single integer representing the zone offset (in minutes) or None if this time does not have a time zone offset.

get_zone3()

Returns a tuple of:

(zdirection, zhour, zminute)

These values are defined as per Time’s constructor.

get_canonical_zone()

Returns a tuple of:

(zdirection, zhour, zminute)

These values are defined as per Time’s constructor but zero offsets always return zdirection=0. If present, the zone is always returned with complete (minute) precision.

get_time_and_zone()

Returns a tuple of (hour,minute,second,zone direction,zone offset) as defined in get_time and get_zone.

extend(hour=None, minute=None, second=None)

Constructs a Time instance from an existing time, extended a (possibly) truncated hour/minute/second value.

The time zone is always copied if present. The result is a tuple of (<Time instance>,overflow) where overflow 0 or 1 indicating whether or not the time overflowed. For example:

# set base to 20:17:40Z
base=Time(hour=20,minute=17,second=40,zdirection=0)
t,overflow=base.extend(minute=37)
# t is 20:37:40Z, overflow is 0
t,overflow=base.extend(minute=7)
# t is 21:07:40Z, overflow is 0
t,overflow=base.extend(hour=19,minute=7)
# t is 19:07:40Z, overflow is 1
offset(hours=0, minutes=0, seconds=0)

Constructs a Time instance from an existing time and an offset number of hours, minutes and or seconds.

The time zone is always copied (if present). The result is a tuple of (<Time instance>,overflow) where overflow is the number of days by which the time overflowed. For example:

# set base to 20:17:40Z
base = Time(hour=20, minute=17, second=40, zdirection=0)
t, overflow = base.offset(minutes=37)
# t is 20:54:40Z, overflow is 0
t, overflow = base.offset(hours=4, minutes=37)
# t is 00:54:40Z, overflow is 1

Reduced precision times can still be offset but only by matching arguments. In other words, if the time has minute precision then you may not pass a non-zero value for seconds, etc. A similar constraint applies to the passing of floating point arguments. You may pass a fractional offset for seconds if the time has second precision but the minute and hour offsets must be to integer precision. Similarly, you may pass a fractional offset for minutes if the time has minute precision, etc.

with_zone(zdirection, zhour=None, zminute=None, **kwargs)

Replace time zone information

Constructs a new Time instance from an existing time but with the time zone specified. The time zone of the existing time is ignored. Pass zdirection=None to strip the zone information completely.

shift_zone(zdirection, zhour=None, zminute=None, **kwargs)

Constructs a Time instance from an existing time but shifted so that it is in the time zone specified. The return value is a tuple of:

(<Time instance>, overflow)

overflow is one of -1, 0 or 1 indicating if the time over- or under-flowed as a result of the time zone shift.

classmethod from_struct_time(t)

Constructs a zone-less Time from a struct_time, such as might be returned from time.gmtime() and related functions.

update_struct_time(t)

update_struct_time changes the hour, minute, second and isdst fields of t, a struct_time, to match the values in this time.

isdst is always set to -1

classmethod from_now()

Constructs a Time from the current local time.

classmethod from_str(src, base=None)

Constructs a Time instance from a string representation, truncated forms are returned as the earliest time on or after base and may have overflowed. See from_string_format() for more.

with_zone_string(zone_str)

Constructs a Time instance from an existing time but with the time zone parsed from zone_str. The time zone of the existing time is ignored.

with_zone_string_format(zone_str)

Constructs a Time instance from an existing time but with the time zone parsed from zone_str. The time zone of the existing time is ignored.

Returns a tuple of: (<Time instance>,format)

classmethod from_string_format(src, base=None)

Constructs a Time instance from a string representation, truncated forms are returned as the earliest time on or after base.

Returns a tuple of (<Time instance>,overflow,format) where overflow is 0 or 1 indicating whether or not a truncated form overflowed and format is a string representation of the format parsed, e.g., “hhmmss”.

get_string(basic=False, truncation=0, ndp=0, zone_precision=7, dp=', ', **kwargs)

Formats this time, including zone, for example 20:17:40

basic
True/False, selects basic form, e.g., 201740. Default is False
truncation
One of the Truncation constants used to select truncated forms of the time. For example, if you specify Truncation.Hour you’ll get -17:40 or -1740. Default is NoTruncation.
ndp
Specifies the number of decimal places to display for the least significant component, the default is 0.
dp
The character to use as the decimal point, the default is the comma, as per the ISO standard.
zone_precision
One of Precision.Hour or Precision.Complete to control the precision of the zone offset.

Note that time formats only support Minute and Second truncated forms.

get_zone_string(basic=False, zone_precision=7, **kwargs)

Formats this time’s zone, for example -05:00.

basic
True/False, selects basic form, e.g., -0500. Default is False
zone_precision
One of Precision.Hour or Precision.Complete to control the precision of the zone offset.

Times constructed with a zdirection value of 0 are always rendered using “Z” for Zulu time (the name is taken from the phonetic alphabet). To force use of the offset format you must construct the time with a non-zero value for zdirection.

complete()

Returns True if this date has a complete representation, i.e., does not use one of the reduced precision forms.

(Whether or not a time is complete refers only to the precision of the time value, it says nothing about the presence or absence of a time zone offset.)

get_precision()

Returns one of the Precision constants representing the precision of this time.

with_precision(precision, truncate=False)

Constructs a Time instance from an existing time but with the precision specified by precision.

precision is one of the Precision constants, only hour, minute and complete precision are valid.

truncate is True/False indicating whether or not the time value should be truncated so that all values are integers. For example:

t = Time(hour=20, minute=17, second=40)
tm = t.with_precision(Precision.Minute, False)
print tm.get_string(ndp=3)
#   20:17,667
tm=t.with_precision(Precision.Minute, True)
print tm.get_string(ndp=3)
#   20:17,000   
class pyslet.iso8601.TimePoint(src=None, date=None, time=None)

Bases: pyslet.pep8.PEP8Compatibility, pyslet.py2.UnicodeMixin, pyslet.py2.SortableMixin

A class for representing ISO timepoints

TimePoints are constructed from a date and a time (which may or may not have a time zone), for example:

TimePoint(date=Date(century=19,year=69,month=7,day=20),
          time=Time(hour=20,minute=17,second=40,zdirection=0))

If the date is missing then the date origin is used, Date() or 0001-01-01. Similarly, if the time is missing then the time origin is used, Time() or 00:00:00

Times may be given with reduced precision but the date must be complete. In other words, there is no such thing as a timepoint with, month precision, use Date instead. Expanded dates may be used.

When comparing TimePoint instances we deal with partially specified TimePoints in the same way as Time. However, unlike the comparison of Time instances, we reduce all TimePoints with time-zones to a common zone before doing a comparison. As a result, TimePoints which are equal but are expressed in different time zones will still compare equal.

Instances can be converted directly to strings or unicode strings using the default, extended calendar format. Other formats are supported through format specific methods.

get_calendar_time_point()

Returns a tuple representing the calendar time point

The result is:

(century, year, month, day, hour, minute, second)

This method cannot be used for expanded dates, use get_xcalendar_time_point() instead when dealing with dates outside of the normal ISO 8601 range.

get_xcalendar_time_point()

Returns a tuple representing an expanded calendar time point

The result is:

(bce, century, year, month, day, hour, minute, second)
get_ordinal_time_point()

Returns a tuple representing the ordinal time point

The result is:

(century, year, ordinal_day, hour, minute, second)

This method cannot be used for expanded dates, use get_xordinal_time_point() instead when dealing with dates outside of the normal ISO 8601 range.

get_xordinal_time_point()

Returns a tuple representing an expanded ordinal time point

The result is:

(bce, century, year, ordinal_day, hour, minute, second)
get_week_day_time_point()

Returns a tuple representing the week-day time point

The result is:

(century, decade, year, week, weekday, hour, minute,
 second)

This method cannot be used for expanded dates, use get_xweek_day_time_point() instead when dealing with dates outside of the normal ISO 8601 range.

get_xweek_day_time_point()

Returns a tuple representing an expanded week-day time point

The result is:

(bce, century, decade, year, week, weekday, hour, minute,
 second)
get_zone()

Returns a tuple representing the zone

The result is (zdirection, zoffset)

See Time.get_zone() for details.

expand(xdigits)

Constructs a new expanded instance

The purpose of this method is to create a new instance from an existing TimePoint but with a different date expansion (value of xdigits).

This is equivalent to:

TimePoint(date=self.date.expand(xdigits), time=self.time)
with_zone(zdirection, zhour=None, zminute=None, **kwargs)

Constructs a TimePoint instance from an existing TimePoint but with the time zone specified. The time zone of the existing TimePoint is ignored.

shift_zone(zdirection, zhour=None, zminute=None, **kwargs)

Shifts time zone

Constructs a TimePoint instance from an existing TimePoint but shifted so that it is in the time zone specified.

update_struct_time(t)

Outputs the TimePoint in struct_time format

Changes the year, month, date, hour, minute and second fields of t, t must be a mutable list arranged in the same order as struct_time.

classmethod from_struct_time(t)

Constructs an instance from a struct_time

In other words, constructs an instance from the object returned from time.gmtime() and related functions.

classmethod from_str(src, base=None, tdesignators='T', xdigits=None)

Constructs a TimePoint from a string representation. Truncated forms are parsed with reference to base.

classmethod from_string_format(src, base=None, tdesignators='T', xdigits=None, **kwargs)

Creates an instance from a string

Similar to from_str() except that a tuple is returned, the first item is the resulting TimePoint instance, the second is a string describing the format parsed. For example:

tp, f = TimePoint.from_string_format("1969-07-20T20:40:17")
# f is set to "YYYY-MM-DDTmm:hh:ss".
get_calendar_string(basic=False, truncation=0, ndp=0, zone_precision=7, dp=', ', tdesignator='T', **kwargs)

Formats this TimePoint using calendar form

For example ‘1969-07-20T20:17:40’

basic
True/False, selects basic form, e.g., 19690720T201740. Default is False
truncation
One of the Truncation constants used to select truncated forms of the date. For example, if you specify Truncation.Year you’ll get –07-20T20:17:40 or –0720T201740. Default is NoTruncation. Calendar format only supports Century, Year and Month truncated forms, the time component cannot be truncated.
ndp, dp and zone_precision
As specified in Time.get_string()

If the instance is an expanded time point with xdigits=-1 then basic format is not allowed.

get_ordinal_string(basic=0, truncation=0, ndp=0, zone_precision=7, dp=', ', tdesignator='T', **kwargs)

Formats this TimePoint using ordinal form

For example ‘1969-201T20:17:40’

basic
True/False, selects basic form, e.g., 1969201T201740. Default is False
truncation
One of the Truncation constants used to select truncated forms of the date. For example, if you specify Truncation.Year you’ll get -201T20-17-40. Default is NoTruncation. Note that ordinal format only supports century and year truncated forms, the time component cannot be truncated.
ndp, dp and zone_precision
As specified in Time.get_string()

If the instance is an expanded time point with xdigits=-1 then basic format is not allowed.

get_week_string(basic=0, truncation=0, ndp=0, zone_precision=7, dp=', ', tdesignator='T', **kwargs)

Formats this TimePoint using week form

For example ‘1969-W29-7T20:17:40’

basic
True/False, selects basic form, e.g., 1969W297T201740. Default is False
truncation
One of the Truncation constants used to select truncated forms of the date. For example, if you specify Truncation.Year you’ll get -W297T20-17-40. Default is NoTruncation. Note that week format only supports century, decade, year and week truncated forms, the time component cannot be truncated.
ndp, dp and zone_precision
As specified in Time.get_string()

If the instance is an expanded time point with xdigits=-1 then basic format is not allowed.

classmethod from_unix_time(unix_time)

Constructs a TimePoint from unix_time, the number of seconds since the time origin. The resulting time is in UTC.

This method uses python’s gmtime(0) to obtain the time origin, it isn’t necessarily the Unix base time of 1970-01-01.

get_unixtime()

Returns a unix time value representing this time point.

classmethod from_now()

Constructs a TimePoint from the current local date and time.

classmethod from_now_utc()

Constructs a TimePoint from the current UTC date and time.

complete()

Test for complete precision

Returns True if this TimePoint has a complete representation, i.e., does not use one of the reduced precision forms

(Whether or not a TimePoint is complete refers only to the precision of the time value, it says nothing about the presence or absence of a time zone offset.)

get_precision()

Returns one of the Precision constants representing the precision of this TimePoint.

with_precision(precision, truncate)

Return new instance with precision

Constructs a TimePoint instance from an existing TimePoint but with the precision specified by precision. For more details see Time.with_precision()

class pyslet.iso8601.Duration(value=None)

Bases: pyslet.py2.UnicodeMixin, pyslet.pep8.PEP8Compatibility

A class for representing ISO durations

6.8.1. Supporting Constants

class pyslet.iso8601.Truncation

Defines constants to use when formatting to truncated forms.

No = 0

constant for no truncation

Century = 1

constant for truncating to century

Decade = 2

constant for truncating to decade

Year = 3

constant for truncating to year

Month = 4

constant for truncating to month

Week = 5

constant for truncating to week

Hour = 6

constant for truncating to hour

Minute = 7

constant for truncating to minute

pyslet.iso8601.NoTruncation = 0

a synonym for Truncation.No

class pyslet.iso8601.Precision

Defines constants for representing reduced precision.

Century = 1

constant for century precision

Year = 2

constant for year precision

Month = 3

constant for month precision

Week = 4

constant for week precision

Hour = 5

constant for hour precision

Minute = 6

constant for minute precision

Complete = 7

constant for complete representations

6.8.2. Utility Functions

pyslet.iso8601.leap_year(year)

leap_year returns True if year is a leap year and False otherwise.

Note that leap years famously fall on all years that divide by 4 except those that divide by 100 but including those that divide by 400.

pyslet.iso8601.day_of_week(year, month, day)

day_of_week returns the day of week 1-7

1 being Monday for the given year, month and day

pyslet.iso8601.week_count(year)

Week count returns the number of calendar weeks in a year.

Most years have 52 weeks of course, but if the year begins on a Thursday or a leap year begins on a Wednesday then it has 53.

pyslet.iso8601.get_local_zone()

Returns the number of minutes ahead of UTC we are

This is calculated by comparing the return result of the time module’s gmtime and localtime methods.