Skip to content

Instantly share code, notes, and snippets.

@PythonCoderAS
Created September 1, 2020 00:32
Show Gist options
  • Select an option

  • Save PythonCoderAS/3fe7d952ce877073bdc404d468478f2d to your computer and use it in GitHub Desktop.

Select an option

Save PythonCoderAS/3fe7d952ce877073bdc404d468478f2d to your computer and use it in GitHub Desktop.
"""Gets a formatted date-time string. Handles leap years but not leap seconds."""
def get_time_components(timestamp: int):
year = 1970
month = "January"
day = 1
day_of_week = "Thursday"
hour = 0
hour_12 = 12
minute = 0
second = 0
am_or_pm = "am"
for i in range(timestamp):
second = second + 1
if second == 60:
second = 0
minute = minute + 1
if minute == 60:
minute = 0
hour = hour + 1
hour_12 = hour_12 + 1
if hour_12 == 13:
hour_12 = 1
if hour > 12:
am_or_pm = "pm"
if hour == 24:
hour = 0
am_or_pm = "am"
day = day + 1
if day_of_week == "Monday":
day_of_week = "Tuesday"
else:
if day_of_week == "Tuesday":
day_of_week = "Wednesday"
else:
if day_of_week == "Wednesday":
day_of_week = "Thursday"
else:
if day_of_week == "Thursday":
day_of_week = "Friday"
else:
if day_of_week == "Friday":
day_of_week = "Saturday"
else:
if day_of_week == "Saturday":
day_of_week = "Sunday"
else:
if day_of_week == "Sunday":
day_of_week = "Monday"
if day == 28:
if month == "February":
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
pass
else:
day = 0
month = "March"
else:
day = 0
month = "March"
if day == 29:
if month == "February":
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
day = 0
month = "March"
else:
day = 0
month = "March"
if day == 30:
if month == "April":
day = 0
month = "May"
else:
if month == "June":
day = 0
month = "July"
else:
if month == "September":
day = 0
month = "October"
else:
if month == "November":
day = 0
month = "December"
if day == 31:
if month == "January":
day = 0
month = "February"
else:
if month == "March":
day = 0
month = "April"
else:
if month == "May":
day = 0
month = "June"
else:
if month == "July":
day = 0
month = "August"
else:
if month == "August":
day = 0
month = "September"
else:
if month == "October":
day = 0
month = "November"
else:
if month == "December":
day = 0
year = year + 1
month = "January"
if day == 0:
day = 1
day_str = str(day)
year_str = str(year)
hour_str = str(hour)
hour_12_str = str(hour_12)
minute_str = str(minute)
second_str = str(second)
if len(day_str) == 1:
day_str = "0" + day_str
if len(hour_str) == 1:
hour_str = "0" + hour_str
if hour_str == "00":
hour_str = "12"
if len(hour_12_str) == 1:
hour_12_str = "0" + hour_12_str
if len(minute_str) == 1:
minute_str = "0" + minute_str
if len(second_str) == 1:
second_str = "0" + second_str
return day_str, day_of_week, month, year_str, hour_str, hour_12_str, minute_str, second_str, am_or_pm
def str_to_time(timestamp: int, str_format: str):
d, A, B, Y, H, h, M, S, p = get_time_components(timestamp)
final_str = ""
for i in range(len(str_format)):
character = str_format[i]
if character == "d":
final_str = final_str + d
else:
if character == "A":
final_str = final_str + A
else:
if character == "B":
final_str = final_str + B
else:
if character == "Y":
final_str = final_str + Y
else:
if character == "H":
final_str = final_str + H
else:
if character == "h":
final_str = final_str + h
else:
if character == "M":
final_str = final_str + M
else:
if character == "S":
final_str = final_str + S
else:
if character == "p":
final_str = final_str + p
else:
final_str = final_str + character
return final_str
def main():
val1 = str_to_time(1599335154, 'h:M:S p')
print("Statement 1 returned:", val1)
val2 = str_to_time(683475320, 'A d B Y')
print("Statement 2 returned:", val2)
def test():
import datetime
start = datetime.datetime.utcnow()
print("Starting at:", start.strftime("%D %T UTC"))
main()
end = datetime.datetime.utcnow()
print("Ending at:", end.strftime("%D %T UTC"))
print("Duration:", end - start)
if __name__ == '__main__':
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment