2. Parsing and Formatting

2.1. Date and time formats

2.1.1. Date formats

2.1.1.1. Formal date format in USA

Code Listing 2.7. Formal date format in USA
4/12/61
April 12, 1961

2.1.1.2. Formal date format in Japan

Code Listing 2.8. Formal date format in Japan
20/12/31

2.1.1.3. Formal date format in Germany

Code Listing 2.9. Formal date format in Germany
12.4.1961
12.04.1961

2.1.1.4. Formal date format in Poland

12.4.1961
12.04.1961
12 IV 1961
12.IV.1961
12/4/1961
12/04/1961
12 kwietnia 1961
12 kwiecień 1961

2.1.2. Time formats

2.1.2.1. 24 and 12 hour clock

  • What AM stands for?

  • What PM stands for?

  • Is 12:00 a noon, or someone just simply forgot to put AM/PM?

17:00
5:00 PM
5:00

2.1.2.2. Noon and Midnight

12:00 am
12:00 pm
12:00
24:00
00:00
0:00

2.1.2.3. Are those times correct?

23:59:59
23:59:60
24:00
24:01
25:00
27:45
14:00-30:00
1200J

2.2. ISO 8601 Standard

2.2.1. Dates

1961-04-12

2.2.2. Date and time

  • “Z” (Zulu) means UTC

1961-04-12T06:07:00Z
  • Date and time with milliseconds:

1961-04-12T06:07:00.123Z
  • Date and time with microseconds:

1961-04-12T06:07:00.123456Z

2.2.2.1. Noon and Midnight

  • The notation “00:00” is used at the beginning of a calendar day and is the more frequently used.

  • At the end of a day use “24:00”.

  • “2007-04-05T24:00” is the same as “2007-04-06T00:00”

2.2.3. Weeks

  • Monday 29 December 2008 is written “2009-W01-1”

  • Sunday 3 January 2010 is written “2009-W53-7”

2009-W01
2009-W53-7

2.2.4. Timezone

  • “Z” (Zulu) means UTC

<time>Z
<time>±hh:mm
<time>±hhmm
<time>±hh

2.2.5. Duration

  • P[n]Y[n]M[n]DT[n]H[n]M[n]S:

    • P is the duration designator (for period) placed at the start of the duration representation.

    • Y is the year designator that follows the value for the number of years.

    • M is the month designator that follows the value for the number of months.

    • W is the week designator that follows the value for the number of weeks.

    • D is the day designator that follows the value for the number of days.

    • T is the time designator that precedes the time components of the representation.

    • H is the hour designator that follows the value for the number of hours.

    • M is the minute designator that follows the value for the number of minutes.

    • S is the second designator that follows the value for the number of seconds.

"P3Y6M4DT12H30M5S" represents a duration of "three years, six months, four days, twelve hours, thirty minutes, and five seconds".

2.3. Table of date and time parsing and formatting parameters

Note

Prawie wszystkie parametry są podobne różnych językach programowania. Od czasu do czasu występują małe zmiany, np. w JavaScript minuty to i a nie M

Tab. 2.6. Tabelka parametrów formatowania i parsowania dat i czasu

Directive

Example

Meaning

%a

Sun, Mon, …, Sat

Weekday as locale’s abbreviated name

%A

Sunday, Monday, …, Saturday (en_US)

Weekday as locale’s full name

%w

0, 1, …, 6

Weekday as a decimal number, where 0 is Sunday and 6 is Saturday

%d

01, 02, …, 31

Day of the month as a zero-padded decimal number

%b

Jan, Feb, …, Dec (en_US)

Month as locale’s abbreviated name

%B

January, February, …, December (en_US)

Month as locale’s full name

%m

01, 02, …, 12

Month as a zero-padded decimal number

%y

00, 01, …, 99

Year without century as a zero-padded decimal number

%Y

0001, 0002, …, 2013, 2014, …, 9998, 9999

Year with century as a decimal number

%H

00, 01, …, 23

Hour (24-hour clock) as a zero-padded decimal number

%I

01, 02, …, 12

Hour (12-hour clock) as a zero-padded decimal number

%p

AM, PM (en_US)

Locale’s equivalent of either AM or PM

%M

00, 01, …, 59

Minute as a zero-padded decimal number

%S

00, 01, …, 59

Second as a zero-padded decimal number

%f

000000, 000001, …, 999999

Microsecond as a decimal number, zero-padded on the left

%z

(empty), +0000, -0400, +1030

UTC offset in the form +HHMM or -HHMM (empty string if the object is naive)

%Z

(empty), UTC, EST, CST

Time zone name (empty string if the object is naive)

%j

001, 002, …, 366

Day of the year as a zero-padded decimal number

%U

00, 01, …, 53

Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0

%W

00, 01, …, 53

Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0

%c

Tue Aug 16 21:30:00 1988 (en_US)

Locale’s appropriate date and time representation

%x

08/16/1988 (en_US); 16.08.1988 (de_DE)

Locale’s appropriate date representation

%X

21:30:00

Locale’s appropriate time representation

%%

%

A literal % character

%G

0001, 0002, …, 2013, 2014, …, 9998, 9999

ISO 8601 year with century representing the year that contains the greater part of the ISO week (%V)

%u

1, 2, …, 7

ISO 8601 weekday as a decimal number where 1 is Monday

%V

01, 02, …, 53

ISO 8601 week as a decimal number with Monday as the first day of the week. Week 01 is the week containing Jan 4

2.4. Date formatting

2.4.1. f-string formatting

Code Listing 2.10. Datetime formatting as string with f'...'
from datetime import datetime

gagarin = datetime(1961, 4, 12, 6, 7)

print(f'Gagarin launched on {gagarin:%Y-%m-%d}')
# Gagarin launched on 1961-04-12
Code Listing 2.11. Datetime formatting as string with f'...'
from datetime import datetime

gagarin = datetime(1961, 4, 12, 6, 7)

print(f'Gagarin launched on {gagarin:%Y-%m-%d %H:%M}')
# Gagarin launched on 1961-04-12 06:07
Code Listing 2.12. Datetime formatting as string with f'...'
from datetime import datetime

gagarin = datetime(1961, 4, 12, 6, 7)
format = '%Y-%m-%d %H:%M'

print(f'Gagarin launched on {gagarin:{format}}')
# Gagarin launched on 1961-04-12  06:07

2.4.2. Format to string

Code Listing 2.13. Datetime formatting as string with .strftime()
from datetime import datetime

gagarin = datetime(1961, 4, 12, 6, 7)
formatted = gagarin.strftime('%Y-%m-%d %H:%M')

print(f'Gagarin launched on {formatted}')
# Gagarin launched on 1961-04-12 06:07

2.4.3. ISO Format

Code Listing 2.14. Datetime formatting to ISO format
from datetime import datetime

dt = datetime(1969, 7, 21, 14, 56, 15)

dt.isoformat()
# 1969-07-21T14:56:15
Code Listing 2.15. Date formatting to ISO format
from datetime import date

d = date(1969, 7, 21)

d.isoformat()
# 1969-07-21

2.5. Parsing dates

  • Parsing - analyze (a sentence) into its parts and describe their syntactic roles.

Code Listing 2.16. Datetime parsing from string
from datetime import datetime

sputnik = '4 October 1957, 19:28:34 [UTC]'

out = datetime.strptime(sputnik, '%d %B %Y, %H:%M:%S [%Z]')
# datetime.datetime(1957, 10, 4, 19, 28, 34)

print(out)
# 1957-10-04 19:28:34

2.6. Assignments

2.6.1. From ISO date format

  • Filename: datetime_from_iso.py

  • Lines of code to write: 5 lines

  • Estimated time of completion: 5 min

  • Input data: Code Listing 2.17.

  1. Przedstaw datę jako obiekt datetime

Code Listing 2.17. Convert str from ISO date format to datetime objects
1969-07-21T14:56:15.123Z

2.6.2. To ISO date format

  • Filename: datetime_to_iso.py

  • Lines of code to write: 5 lines

  • Estimated time of completion: 5 min

  1. Datę:

    datetime.datetime(1961, 4, 12, 6, 7, 0, 123456)
    
  2. Przedstaw daty jako obiekt datetime

  3. Wyświetl w formacie ISO datę i czas, tj.:

    "Rok-Miesiąc-DzieńTGodzina:Minuta:Sekunda.UłamkiSekundZ"
    1961-04-12T06:07:00.123456
    
  4. Wyświetl w formacie ISO samą datę, tj. bez czasu:

    "Rok-Miesiąc-Dzień"
    1961-04-12
    

2.6.3. US date and time format

  • Filename: datetime_from_us.py

  • Lines of code to write: 5 lines

  • Estimated time of completion: 5 min

  1. Rozczytaj datę z formatu amerykańskiego długiego (skopiuj z cudzysłowami):

    gagarin = '"April 12, 1961 06:07:00 AM local time"'
    
  2. Wyświetl datę w formacie amerykańskim krótkim:

    "Miesiąc/Dzień/Rok Godzina:Minuta AM/PM"
    04/12/61 06:07 AM
    
Hint
  • Wpisz “local time” jako zwykły tekst w parametrze fmt funkcji .strptime()

  • Wpisz znaki cudzysłowia " jako zwykły tekst w parametrze fmt funkcji .strptime()