Pyjo.Date - HTTP date

from Pyjo.Date import Date

# Parse
date = Date('Sun, 06 Nov 1994 08:49:37 GMT')
print(date.epoch)

# Or import the module, as in Google Python Style Guide

import Pyjo.Date

import time

# Parse
date = Pyjo.Date.new('Sun, 06 Nov 1994 08:49:37 GMT')
print(date.epoch)

# Build
date = Pyjo.Date.new(time.time() + 60)
print(date)

Pyjo.Date implements HTTP date and time functions, based on RFC 7230, RFC 7231 and RFC 3339.

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojo::Date of Mojolicious 9.49.

Classes

class Pyjo.Date.Pyjo_Date(date: str | float | datetime | None = None)[source]

Pyjo.Date inherits all methods from Pyjo.Base and Pyjo.String.Mixin and implements the following new ones.

__bool__() → bool[source]
boolean = bool(date)

Always true.

__str__() → str[source]
string = str(date)

Alias for to_str().

epoch: int | float | None
epoch = date.epoch
date.epoch = 784111777

Epoch seconds, defaults to the current time.

parse(date: str | float | datetime) → Self[source]
date = date.parse('Sun Nov  6 08:49:37 1994')
date = date.parse(datetime.now(UTC))

Parse date. A datetime.datetime gives its timestamp, so a naive one is local time, as for datetime.datetime.timestamp().

# Epoch
print(Pyjo.Date.new('784111777').epoch)
print(Pyjo.Date.new('784111777.21').epoch)

# RFC 822/1123
print(Pyjo.Date.new('Sun, 06 Nov 1994 08:49:37 GMT').epoch)

# RFC 850/1036
print(Pyjo.Date.new('Sunday, 06-Nov-94 08:49:37 GMT').epoch)

# Ansi C asctime()
print(Pyjo.Date.new('Sun Nov  6 08:49:37 1994').epoch)

# RFC 3339
print(Pyjo.Date.new('1994-11-06T08:49:37Z').epoch)
print(Pyjo.Date.new('1994-11-06T08:49:37').epoch)
print(Pyjo.Date.new('1994-11-06T08:49:37.21Z').epoch)
print(Pyjo.Date.new('1994-11-06T08:49:37+01:00').epoch)
print(Pyjo.Date.new('1994-11-06T08:49:37-01:00').epoch)
to_datetime() → datetime[source]
dt = date.to_datetime()

Turn date into a datetime.datetime in UTC, for the standard library.

# datetime.datetime(1994, 11, 6, 8, 49, 37, tzinfo=datetime.timezone.utc)
Pyjo.Date.new(784111777).to_datetime()
to_rfc3339() → str[source]
string = date.to_rfc3339()

Render RFC 3339 date and time.

# "1994-11-06T08:49:37Z"
Pyjo.Date.new(784111777).to_rfc3339()

# "1994-11-06T08:49:37.21Z"
Pyjo.Date.new(784111777.21).to_rfc3339()
to_str() → str[source]
string = date.to_str()

Render date suitable for HTTP messages.

# "Sun, 06 Nov 1994 08:49:37 GMT"
Pyjo.Date.new(784111777).to_str()