ich bin neu hier im Forum, wollte aber trotzdem mal mein kleines Programm zeigen und ggf. Verbesserungsvorschlaege einnehmen. Also ohne weitere Verzoegerung, hier mein Code: (Dokumentation auf Englisch)
Code: Alles auswählen
year_start = day_start = day_target = month_target = month_start = year_target = 0
full_year_flag = False
#inputs
while day_start < 1 or day_start > 31:
day_start = input("insert beginning day ")
while month_start > 12 or month_start < 1:
month_start = input("insert beginning month ")
while year_start <= 0:
year_start = input("insert beginning year ")
while day_target < 1 or day_target > 31:
day_target = input("insert target day ")
while month_target > 12 or month_target < 1:
month_target = input("insert target month ")
while year_target <= year_start: #backward time count doesn't make sense, obviously.
#Same year counts won't work with the algorithm.
year_target = input("insert target year ") #problem occurs when you try to count
#time less than a year.
#year, month converter algorithm
if month_target > month_start:
years_distance = year_target - year_start # +(month_target-month_start)/12.0+(<something with days>), so the
# years won't round up
months_distance = 12 * years_distance + month_target - month_start #but then this has to change (would get
#simpler in fact)
elif month_target < month_start:
years_distance = (year_target - year_start) - 1
months_distance = (years_distance * 12) + (12 + (month_target - month_start))
elif month_target == month_start:
if day_target > day_start:
years_distance = year_target - year_start
elif day_target < day_start:
years_distance = (year_target - year_start) - 1
if day_target >= day_start:
months_distance = (12 * years_distance) + 1
elif day_target < day_start:
months_distance = 12 * years_distance
elif day_target == day_start:
year_distance = year_target - year_start
months_distance = years_distance * 12
full_year_flag = True #full_year_flag exists to 1) add more precission in this
#case 2) test purposes 3) convince about the corectness
#of the algorithm
#days converter algorithm (can get the +5 days accuracy with a month list implemented)
if day_target > day_start:
days_distance = (months_distance * 30.42) + (day_target - day_start)
elif day_target == day_start:
days_distance = months_distance * 30.42
elif day_target < day_start:
days_distance = (months_distance * 30.42) + (30.42 + (day_target - day_start))
#because days have greater precission than rounded years or months, I convert
#them backwards to years and months
months_distance_unrounded = days_distance / 30.42
years_distance_unrounded = days_distance / 365.25
#simple secs/hours/mins, can get more accurate, but for it to have any
#meaning, the day algorithm has to improve
hours_distance = days_distance * 24
mins_distance = days_distance * 24 * 60
secs_distance = days_distance * 24 * 60 * 60
if full_year_flag: #for testing, put the second print tag inside the
#if-check, insert two dates with same month and
#day and compare the printed results
print "excactly ",years_distance, "years", years_distance * 12, "in months, ", years_distance * 365.25, "in days, ", years_distance * 365.35 * 24, "in hours, ",years_distance * 365.25 * 24 * 60, "in minutes and ", years_distance * 365.25 * 24 * 3600, " in seconds"
print "from begining to target date so much time has passed: ",years_distance, " in years, ", months_distance, "in months, ", days_distance, "in days, ", hours_distance, "in hours, ",mins_distance, "in minutes and ", secs_distance, " in seconds"
print "Approximations: Years and months obviously rounded. Days, hours, secs and mins: up to 5 days (worst case)"
print "In unrounded years, this time would be ", years_distance_unrounded, "and in unrounded months ", months_distance_unrounded