2. Parsing and Formatting

2.1. Date and time formats

2.1.1. Date formats

  • Which format is a formal standard in USA?
  • Which format is a formal standard in Germany?
  • Which format is a formal standard in Poland?
4/12/61
April 12, 1961

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

  • Which time is a midnight?
  • Which time is a noon?
12:00 am
12:00 pm
12:00
23:59
24:00
0:00
00:00

2.1.3. ISO 8601 Standard

  • Dates:

    1961-04-12
    
  • Date and time

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

    1961-04-12T06:07:00.123456
    

2.2. 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.8. 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.3. f-string formatting

Code Listing 2.2. 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

2.4. Format to string

Code Listing 2.3. 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.5. Parsing dates

  • Parsing - analyze (a sentence) into its parts and describe their syntactic roles.
Code Listing 2.4. 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

  1. Dane są dwie następujące daty w formacie jak poniżej:

    gagarin = '1961-04-12T06:07:00.123456'
    armstrong = '1969-07-21T14:56:15.123456'
    
  2. Przedstaw daty jako obiekt datetime

  3. Wyświetl pierwszą datę w formacie Amerykańkim “Miesiąc Dzień, Rok Godzina:Minuta AM/PM”

  4. Wyświetl drugą datę w formacie Niemieckim “Dzień.Miesiąc.Rok”

About:
  • Filename: datetime_from_iso.py
  • Lines of code to write: 5 lines
  • Estimated time of completion: 10 min

2.6.2. To ISO date format

  1. Dane są dwie następujące daty w formacie jak poniżej:

    date1 = 'April 12, 1961 2:07 local time'
    date2 = '"07/21/69 2:56:15 AM UTC"'
    
  2. Przedstaw daty jako obiekt datetime

  3. Wyświetl date1 w formacie ISO, tj. “Rok-Miesiąc-DzieńTGodzina:Minuta:Sekunda.UłamkiSekund”

  4. Wyświetl date2 w formacie ISO samą datę, tj. bez czasu

About:
  • Filename: datetime_to_iso.py
  • Lines of code to write: 5 lines
  • Estimated time of completion: 15 min
Hint:
  • Wpisz “local time” jako zwykły tekst do strptime
  • Znaki znaki cudzysłowia jako znaczki do strptime