#!/usr/bin/python3 -u

"""
 *    Author : David Billsbrough <billsbrough@gmail.com>
 *   Created : Thursday, November 22, 2007 at 16:30:35 PM (EST)
 *   License : GNU General Public License -- version 2
 *   Version : $Revision: 0.82 $
 *  Warranty : None
 *   Purpose : Calculate the difference in days between two dates  (python 3)
 *
 *  $Id: daystogo.py,v 0.82 2024/05/27 22:22:19 kc4zvw Exp kc4zvw $
"""

import os, string, sys, time

def getHomeDir():
	myHOME = os.environ["HOME"]
	#print(f"My $HOME directory is {myHOME}.\n")
	return myHOME

def formattedDate(d):
	return time.strftime("%A, %B %d, %Y at %H:%M:%S %Z", time.localtime(d))

def processLine(line):
	aline = line.rstrip()
	print(f"Line: '{aline}'")

	(eventDate, eventName) = aline.split("~")
	(year, month, day) = (eventDate.split("/"))
	dateYMD = year + " " + month + " " + day

	#print(dateYMD)
	#dateTarget = time.mktime(time.strptime(dateYMD, "%Y %m %d"))

	dateTarget = time.mktime(time.strptime(eventDate, "%Y/%m/%d"))
	dayCount = int(((dateTarget - Today) / 86400) + 1)

	if (dayCount <= -2):
		diff = abs(dayCount)
		print(f"It was {diff} days ago since {eventName}.")
	elif (dayCount == -1):
		print(f"Yesterday was {eventName}.")
	elif (dayCount == 0):
		print(f"Today is {eventName}.")
	elif (dayCount == 1):
		print(f"Tomorrow is {eventName}.")
	else:
		print(f"There are {dayCount} days until {eventName}.")


### ================ Main program begins here ================ ###

Today = time.time()
dateStr = formattedDate(Today)

print()
print("Days To Go Calculator (Python version)")
print()
print(f"Today is {dateStr} (local).")
print()

filename = ".calendar"
home = getHomeDir()
calendarFile = os.sep.join((home, filename))

try:
	input = open(calendarFile, 'r')
except IOError:
	print(f"Couldn't open {calendarFile} for reading dates.")
	sys.exit()

for line in input.readlines():
	processLine(line)

input.close()

print()
print("End of report")

# End of script
