Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update datetime usage docs #6077

Merged
merged 1 commit into from Jun 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
104 changes: 46 additions & 58 deletions docs/usage/types/datetime.md
Expand Up @@ -2,85 +2,50 @@
description: Support for datetime types.
---

*Pydantic* supports the following [datetime](https://docs.python.org/library/datetime.html#available-types)
Pydantic supports the following [datetime](https://docs.python.org/library/datetime.html#available-types)
types:

`datetime.date`
: see [Datetime Types](#datetime-types) below for more detail on parsing and validation
* `datetime.date`
* `datetime.time`
* `datetime.datetime`
* `datetime.timedelta`

`datetime.time`
: see [Datetime Types](#datetime-types) below for more detail on parsing and validation
## Validation of datetime types

`datetime.datetime`
: see [Datetime Types](#datetime-types) below for more detail on parsing and validation

`datetime.timedelta`
: see [Datetime Types](#datetime-types) below for more detail on parsing and validation

`PastDate`
: like `date`, but the date should be in the past

`FutureDate`
: like `date`, but the date should be in the future

## Datetime types

* `datetime` fields can be:

* `datetime`, existing `datetime` object
* `int` or `float`, assumed as Unix time, i.e. seconds (if >= `-2e10` and <= `2e10`) or milliseconds (if < `-2e10`or > `2e10`) since 1 January 1970
* `str`, following formats work:
* `datetime` fields will accept values of type:

* `datetime`; an existing `datetime` object
* `int` or `float`; assumed as Unix time, i.e. seconds (if >= `-2e10` and <= `2e10`) or milliseconds
(if < `-2e10`or > `2e10`) since 1 January 1970
* `str`; the following formats are accepted:
* `YYYY-MM-DD[T]HH:MM[:SS[.ffffff]][Z or [±]HH[:]MM]`
* `int` or `float` as a string (assumed as Unix time)

* `date` fields can be:
* `date` fields will accept values of type:

* `date`, existing `date` object
* `int` or `float`, see `datetime`
* `str`, following formats work:
* `date`; an existing `date` object
* `int` or `float`; handled the same as described for `datetime` above
* `str`; the following formats are accepted:

* `YYYY-MM-DD`
* `int` or `float`, see `datetime`
* `int` or `float` as a string (assumed as Unix time)

* `time` fields can be:
* `time` fields will accept values of type:

* `time`, existing `time` object
* `str`, following formats work:
* `time`; an existing `time` object
* `str`; the following formats are accepted:

* `HH:MM[:SS[.ffffff]][Z or [±]HH[:]MM]`

* `timedelta` fields can be:
* `timedelta` fields will accept values of type:

* `timedelta`, existing `timedelta` object
* `int` or `float`, assumed as seconds
* `str`, following formats work:
* `timedelta`; an existing `timedelta` object
* `int` or `float`; assumed to be seconds
* `str`; the following formats are accepted:

* `[-][DD ][HH:MM]SS[.ffffff]`
* `[±]P[DD]DT[HH]H[MM]M[SS]S` ([ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format for timedelta)


## Pydantic date types

`PastDate`
: like `date`, but the date should be in the past

`FutureDate`
: like `date`, but the date should be in the future

`AwareDatetime`
: like `datetime`, but requires the value to have timezone info

`NaiveDatetime`
: like `datetime`, but requires the value to lack timezone info

`PastDatetime`
: like `datetime`, but the datetime should be in the past

`FutureDatetime`
: like `datetime`, but the datetime should be in the future


```py
from datetime import date, datetime, time, timedelta

Expand All @@ -106,3 +71,26 @@ print(m.model_dump())
{'d': datetime.date(2023, 3, 24), 'dt': datetime.datetime(2032, 4, 23, 10, 20, 30, 400000, tzinfo=TzInfo(+02:30)), 't': datetime.time(4, 8, 16), 'td': datetime.timedelta(days=3, seconds=45005)}
"""
```

## Pydantic date types

The following types can be imported from `pydantic`, and augment the types described above
with additional validation constraints:

`PastDate`
: like `date`, with the constraint that the value must be in the past

`FutureDate`
: like `date`, with the constraint that the value must be in the future

`PastDatetime`
: like `PastDate`, but for `datetime`

`FutureDatetime`
: like `FutureDate`, but for `datetime`

`AwareDatetime`
: like `datetime`, with the constraint that the value must have timezone info

`NaiveDatetime`
: like `datetime`, with the constraint that the value must lack timezone info