diff --git a/README.md b/README.md index c8f6539..1b11e6e 100644 --- a/README.md +++ b/README.md @@ -456,13 +456,76 @@ from itertools import product, combinations, combinations_with_replacement, perm Datetime -------- +* **Module Datetime provides Date (`''`), Time (`''`) and Datetime (`'
'`) classes.** +* **Time and Datetime can be 'aware' (`''`), meaning they have defined timezone, or 'naive' (`''`), meaning they don't. + ```python -from datetime import datetime -now = datetime.now() -now.month # 3 -now.strftime('%Y%m%d') # '20180315' -now.strftime('%Y%m%d%H%M%S') # '20180315002834' - = datetime.strptime('2015-05-12 00:39', '%Y-%m-%d %H:%M') +$ pip3 install pytz +from datetime import date, time, datetime, timedelta +import pytz +``` + +### Constructors +```python + = date(year, month, day) + = time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, fold=0) +
= datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, fold=0) + = timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) +``` +* **`'fold=1'` means second pass in case of time jumping back for one hour.** +* **Use `'.weekday()'` to get day of the week (Mon == 0). + +### Now +```python + = date.today() # Current local date. + = datetime.today() # Naive datetime from current local time. + = datetime.utcnow() # Naive datetime from current UTC time. + = datetime.now() # Aware datetime from current time. +``` + +### Encode +```python + = D/T/DT.fromisoformat() # From 'YYYY-MM-DD', 'HH:MM:SS.ffffff[+]' or both. +
= DT.strptime(, '') # Datetime from string according to 'format'. + = D/DT.fromordinal() # Date or datetime from days since Christ. + = D/DT.fromtimestamp() # Date or datetime from seconds since Epoch in local time. + = DT.utcfromtimestamp() # Naive datetime from seconds since Epoch in UTC time. + = DT.fromtimestamp(, ) # Aware datetime from seconds since Epoch in time. +``` +* **On Unix systems Epoch is `'1970-01-01 00:00 UTC'`, `'1970-01-01 01:00 CET'`, ...** + +### Decode +```python + = .isoformat() # 'YYYY-MM-DD', 'HH:MM:SS.ffffff[+]' or both. + = .strftime('') # Returns customized string representation. + = .toordinal() # Returns days since Christ ignoring time and timezone. + =
.timestamp() # Returns seconds since Epoch in local time or if set. +``` + +### Format +```python +>>> dt = datetime.strptime('2015-05-14 23:39:00', '%Y-%m-%d %H:%M:%S') +``` + +#### Rest of the options: +* **`'y'` - Year, 2 digits** +* **`'b'` - Month, abbreviated name** +* **`'B'` - Month, full name** +* **`'a'` - Weekday, abbreviated name** +* **`'A'` - Weekday, full name** +* **`'I'` - Hours, 2 digits, 12 hours** +* **`'p'` - AM/PM** +* **`'f'` - Microseconds, 6 digits** +* **`'z'` - Timezone offset, ± and 4 digits** +* **`'Z'` - Timezone name** + +### Timezone +```python + = pytz.timezone('/') # Use 'pytz.utc' for UTC. + =
.astimezone() # Converts datetime to passed timezone. + = .replace(tzinfo=) # Changes timezone without conversion. + = .utcoffset() # Returns timezone's current offset from UTC. + = .dst() # Returns daylight saving time offset. ```