3. Time Deltas

3.1. Time shifts

Code Listing 3.7. Shifting datetimes
from datetime import datetime


gagarin = datetime(1961, 4, 12, 6, 7)
armstrong = datetime(1969, 7, 21, 14, 56, 15)

between_dates = armstrong - gagarin    # datetime.timedelta(3022, 31755)

between_dates                          # 3022 days, 8:49:15
between_dates.seconds                  # 31755
between_dates.days                     # 3022
between_dates.total_seconds()          # 261132555.0 (days * seconds per day + seconds)

3.2. timedelta

3.2.1. Simple timedelta shifts

Code Listing 3.8. Simple timedelta shifts
from datetime import datetime, timedelta, date


sputnik = date(1957, 10, 4)
gagarin = datetime(1961, 4, 12)
armstrong = datetime(1969, 7, 21, 14, 56, 15)


gagarin - timedelta(minutes=15)
gagarin + timedelta(minutes=10)

armstrong - timedelta(hours=21)
armstrong + timedelta(hours=5)

sputnik + timedelta(days=5)
sputnik - timedelta(days=3)

gagarin + timedelta(weeks=2)
gagarin - timedelta(weeks=3)

armstrong - timedelta(days=2, hours=21)

3.2.2. Complex timedelta shifts

3.2.3. timedelta month shifts

Code Listing 3.9. Substract month from datetime
from datetime import datetime, timedelta, date


# Average days a month in solar calendar
MONTH = timedelta(days=30.436875)

month_before = date(1961, 4, 12) - MONTH
# datetime.date(1961, 3, 13)

3.3. Time diff

Code Listing 3.10. Diff between datetimes
from datetime import datetime


SECOND = 1
MINUTE = 60 * SECOND
HOUR = 60 * MINUTE
DAY = 24 * HOUR
MONTH = 30.436875 * DAY    # Average days a month in solar calendar
YEAR = 365.2425 * DAY      # Solar calendar


gagarin = datetime(1961, 4, 12, 6, 7)
armstrong = datetime(1969, 7, 21, 14, 56, 15)

duration = armstrong - gagarin
# datetime.timedelta(3022, 31755)

years, seconds = divmod(duration.total_seconds(), YEAR)
months, seconds = divmod(seconds, MONTH)
days, seconds = divmod(seconds, DAY)
hours, seconds = divmod(duration.seconds, HOUR)
minutes, seconds = divmod(seconds, MINUTE)

difference = {
    'years': int(years),
    'months': int(months),
    'days': int(days),
    'hours': int(hours),
    'minutes': int(minutes),
    'seconds': int(seconds),
}

print(difference)
# {'years': 8, 'months': 3, 'days': 9, 'hours': 8, 'minutes': 49, 'seconds': 15}

3.4. Assignments

3.4.1. Date manipulation

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

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

  3. Odejmij obie daty od siebie

  4. Oblicz ile lat i miesięcy minęło między wydarzeniami

  5. Od obecnej chwili (UTC) odejmij ten sam czas, który Ci wyszedł w poprzednim punkcie

  6. Wyświetl samą datę (bez czasu)

  7. Ile miałeś wtedy lat?

  8. Przyjmij:

    • rok = 365.2425 dni
    • miesiac = 30.436875 dni
About:
  • Filename: datetime_deltas.py
  • Lines of code to write: 15 lines
  • Estimated time of completion: 20 min
Zadanie z gwiazdką:
 
  • Co to za daty, które podał użytkownik?
  • Uwzględnij strefy czasowe.
  • Co robiłeś przez ten czas?
Hint:
  • Wpisz “local time” jako zwykły tekst do strptime

  • datetime.now(tz=timezone.utc)

  • datetime(1961, 04, 12, 6, 7).replace(tz=timezone.utc)

  • Standard ISO:

    • ‘1961-04-12’
    • ‘1961-04-12T06:07:00’
    • ‘1961-04-12T06:07:00.123456’