#!/usr/bin/python3
################################################################################### 
# Python date functions 
# using datetime module (datetime and timedelta)
################################################################################### 
print("Content-Type: text/html \n")     #http header with newline char (required for the web)

from datetime import datetime, timedelta

print('<pre>')                          #to produce better display on the web 

now   = datetime.now()                              #create date obj for today
other = datetime(2022,12,1,15,30,43,100000)         #create date obj for another day

#------------------------------------------------------------------------------- 
# Subtracting 2 dates
#------------------------------------------------------------------------------- 
delta   = other - now                                 #delta is a timedelta object
seconds = delta.total_seconds()                       #using timedelta total_seconds()
numDays = seconds / (24*60*60)  

print("Subtracting 2 Dates")
print("The 2 dates are: ", now," and ",other)
print("Entire obj:      ", delta) 
print("Days and Hours:  ", delta.days, delta.seconds /(60*60.0) )
print("Delta in seconds:", seconds)
print("Delta in Days:   ", numDays)  
print()

#------------------------------------------------------------------------------- 
# Adding an interval to a date
#------------------------------------------------------------------------------- 
interval = 365
future   = now + timedelta(days=interval, hours=0)          #can use weeks, days, hours, minutes, seconds 

print("Adding an Interval to a Date")
print("The date and interval are:<td>",now," and ",interval,"days")
print("Future date:    ", future)
print("Future year:    ", future.year)
print("Future month:   ", future.month)
print("Future day:     ", future.day)
print("Future hh:mi:ss ", future.hour, future.minute, future.second) 
print()