chrono.DateTime - Main class for datetime handling

class chrono.DateTime(datetime=None, **kwargs)

A class for date/time handling. For general usage, see the description of chrono.Date in the Usage section, which works in much the same way as chrono.DateTime.

Valid values for datetime can be:

  • string: parses date/time from a string, see chrono.parser.ISOParser for valid formats
  • True: sets the date/time to the current date
  • integer: assumes input is a UNIX timestamp, sets date/time accordingly
  • chrono.DateTime: sets date/time from another DateTime object
  • datetime.datetime: sets date/time from a datetime.datetime object
  • time.struct_time: sets date/time from a time.struct_time object
  • None: creates a date/time with empty attributes
  • False: creates a date/time with empty attributes

The class can also be initialized using the keyword arguments year, month, day, hour, minute, and second:

Date(year=2000, month=10, day=16, hour=16, minute=27, second=43)

If both datetime and keywords are specified, datetime takes precedence.

assert_set()

Makes sure the object has a full date set, ie the attributes chrono.DateTime.year, chrono.DateTime.month, chrono.DateTime.day, chrono.DateTime.hour, chrono.DateTime.minute, and chrono.DateTime.second are not None

Raises chrono.error.NoDateTimeError on missing attributes.

clear()
Clears the date/time, by setting chrono.DateTime.year, chrono.DateTime.month, chrono.DateTime.day, chrono.DateTime.hour, chrono.DateTime.minute, and chrono.DateTime.second to None.
format(template)

Formats the date using template, replacing variables as supported by chrono.formatter.Formatter.

Raises chrono.error.NoDateTimeError on missing date data.

get()

Returns the datetime as a tuple of year, month, day, hour, minute, and second.

Raises chrono.error.NoDateTimeError on missing datetime data.

get_datetime()

Returns a datetime.datetime instance based on the date/time.

Raises chrono.error.NoDateTimeError on missing date data.

get_string()

Returns a string representation (yyyy-mm-dd hh:mm:ss) of the date/time.

Raises chrono.error.NoDateTimeError on missing date/time data.

get_struct_time()

Returns a time.struct_time representation of the date (expected as input to many Python functions).

Raises chrono.error.NoDateTimeError on missing date data.

get_unix()

Returns a UNIX timestamp representation of the date.

Raises chrono.error.NoDateTimeError on missing date data.

is_set()
Returns True if a date is set, ie if the attributes chrono.DateTime.year, chrono.DateTime.month, chrono.DateTime.day, chrono.DateTime.hour, chrono.DateTime.minute, and chrono.DateTime.second are not None, otherwise returns False.
leapyear()

Returns True if the date is in a leap year, otherwise False.

Raises chrono.error.NoDateTimeError on missing date data.

monthdays()

Returns the number of days in the set month.

Raises chrono.error.NoDateTimeError on missing date data.

ordinal()

Returns the ordinal day (day number in the year) of the set date.

Raises chrono.error.NoDateTimeError on missing date data.

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

Sets the date.

Raises an appropriate subclass of chrono.error.DateTimeError for invalid values.

set_datetime(datetime)
Sets the date from a datetime.datetime object.
set_now()
Sets the datetime to the current date and time.
set_string(string)

Sets the datetime from a string.

Raises chrono.error.ParseError for invalid input format, TypeError for invalid input type, and an appropriate chrono.error.DateTimeError subclass for invalid date values.

set_struct_time(struct_time)
Sets the datetime from a time.struct_time (as returned by various Python functions).
set_unix(timestamp)
Sets the date from an integer UNIX timestamp.
week()

Returns the week of the set date as a tuple with year and week number.

Raises chrono.error.NoDateTimeError on missing date data.

weekdate()

Returns the week date of the set date as a tuple with year, week, and weekday.

Raises chrono.error.NoDateTimeError on missing date data.

weekday()

Returns the week day of the set date, or None if no date is set.

Raises chrono.error.NoDateTimeError on missing date data.

weeks()

Returns the number of weeks in the set year.

Raises chrono.error.NoDateTimeError on missing date data.

yeardays()

Returns the number of days in the year.

Raises chrono.error.NoDateTimeError on missing date data.

Previous topic

chrono.Date - Main class for date handling

Next topic

chrono.Time - Main class for time handling