@@ -456,13 +456,76 @@ from itertools import product, combinations, combinations_with_replacement, perm
456
456
457
457
Datetime
458
458
--------
459
+ * ** Module Datetime provides Date (` '<D>' ` ), Time (` '<T>' ` ) and Datetime (` '<DT>' ` ) classes.**
460
+ * ** Time and Datetime can be 'aware' (` '<a>' ` ), meaning they have defined timezone, or 'naive' (` '<n>' ` ), meaning they don't.
461
+
459
462
``` python
460
- from datetime import datetime
461
- now = datetime.now()
462
- now.month # 3
463
- now.strftime(' %Y%m%d ' ) # '20180315'
464
- now.strftime(' %Y%m%d %H%M%S' ) # '20180315002834'
465
- < datetime> = datetime.strptime(' 2015-05-12 00:39' , ' %Y-%m-%d %H:%M' )
463
+ $ pip3 install pytz
464
+ from datetime import date, time, datetime, timedelta
465
+ import pytz
466
+ ```
467
+
468
+ ### Constructors
469
+ ``` python
470
+ < D> = date(year, month, day)
471
+ < T> = time(hour = 0 , minute = 0 , second = 0 , microsecond = 0 , tzinfo = None , fold = 0 )
472
+ < DT > = datetime(year, month, day, hour = 0 , minute = 0 , second = 0 , microsecond = 0 , tzinfo = None , fold = 0 )
473
+ < TD > = timedelta(days = 0 , seconds = 0 , microseconds = 0 , milliseconds = 0 , minutes = 0 , hours = 0 , weeks = 0 )
474
+ ```
475
+ * ** ` 'fold=1' ` means second pass in case of time jumping back for one hour.**
476
+ * ** Use ` '<D/DT>.weekday()' ` to get day of the week (Mon == 0).
477
+
478
+ ### Now
479
+ ``` python
480
+ < D> = date.today() # Current local date.
481
+ < DTn> = datetime.today() # Naive datetime from current local time.
482
+ < DTn> = datetime.utcnow() # Naive datetime from current UTC time.
483
+ < DTa> = datetime.now(< tz> ) # Aware datetime from current <tz> time.
484
+ ```
485
+
486
+ ### Encode
487
+ ``` python
488
+ < D/ T/ DT > = D/ T/ DT .fromisoformat(< str > ) # From 'YYYY-MM-DD', 'HH:MM:SS.ffffff[+<offset>]' or both.
489
+ < DT > = DT .strptime(< str > , ' <format>' ) # Datetime from string according to 'format'.
490
+ < D/ DTn> = D/ DT .fromordinal(< int > ) # Date or datetime from days since Christ.
491
+ < D/ DTn> = D/ DT .fromtimestamp(< real> ) # Date or datetime from seconds since Epoch in local time.
492
+ < DTn> = DT .utcfromtimestamp(< real> ) # Naive datetime from seconds since Epoch in UTC time.
493
+ < DTa> = DT .fromtimestamp(< real> , < tz> ) # Aware datetime from seconds since Epoch in <tz> time.
494
+ ```
495
+ * ** On Unix systems Epoch is ` '1970-01-01 00:00 UTC' ` , ` '1970-01-01 01:00 CET' ` , ...**
496
+
497
+ ### Decode
498
+ ``` python
499
+ < str > = < D/ T/ DT > .isoformat() # 'YYYY-MM-DD', 'HH:MM:SS.ffffff[+<offset>]' or both.
500
+ < str > = < D/ T/ DT > .strftime(' <format>' ) # Returns customized string representation.
501
+ < int > = < D/ DT > .toordinal() # Returns days since Christ ignoring time and timezone.
502
+ < float > = < DT > .timestamp() # Returns seconds since Epoch in local time or <tz> if set.
503
+ ```
504
+
505
+ ### Format
506
+ ``` python
507
+ >> > dt = datetime.strptime(' 2015-05-14 23:39:00' , ' %Y-%m-%d %H:%M:%S' )
508
+ ```
509
+
510
+ #### Rest of the options:
511
+ * ** ` 'y' ` - Year, 2 digits**
512
+ * ** ` 'b' ` - Month, abbreviated name**
513
+ * ** ` 'B' ` - Month, full name**
514
+ * ** ` 'a' ` - Weekday, abbreviated name**
515
+ * ** ` 'A' ` - Weekday, full name**
516
+ * ** ` 'I' ` - Hours, 2 digits, 12 hours**
517
+ * ** ` 'p' ` - AM/PM**
518
+ * ** ` 'f' ` - Microseconds, 6 digits**
519
+ * ** ` 'z' ` - Timezone offset, ± and 4 digits**
520
+ * ** ` 'Z' ` - Timezone name**
521
+
522
+ ### Timezone
523
+ ``` python
524
+ < tz> = pytz.timezone(' <Continent>/<City>' ) # Use 'pytz.utc' for UTC.
525
+ < DTa> = < DT > .astimezone(< tz> ) # Converts datetime to passed timezone.
526
+ < Ta/ DTa> = < T/ DT > .replace(tzinfo = < timezone> ) # Changes timezone without conversion.
527
+ < timedelta> = < T/ DT > .utcoffset() # Returns timezone's current offset from UTC.
528
+ < timedelta> = < T/ DT > .dst() # Returns daylight saving time offset.
466
529
```
467
530
468
531
0 commit comments