Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Section 5.14 - Exercise 1 #18

Closed
AlJohri opened this issue Apr 15, 2016 · 4 comments
Closed

Section 5.14 - Exercise 1 #18

AlJohri opened this issue Apr 15, 2016 · 4 comments

Comments

@AlJohri
Copy link

AlJohri commented Apr 15, 2016

Exercise 1
The time module provides a function, also named time, that returns the current Greenwich Mean Time in “the epoch”, which is an arbitrary time used as a reference point. On UNIX systems, the epoch is 1 January 1970.

>> import time
>> time.time()
1437746094.5735958

Write a script that reads the current time and converts it to a time of day in hours, minutes, and seconds, plus the number of days since the epoch.


I think asking a student to convert the epoch into time of day might be a little too complicated for Exercise 1 of the chapter. Dates aren't the easiest subject to deal with this early into python. I was trying to use ThinkPython 2 to tutor a student today and found it to be far too advanced having just learned conditionals and floor division in the same chapter.

>>> # Part 1

>>> mydate = int(1437746094.5735958)
>>> num_days = mydate // 3600 // 24
>>> print(num_days, "days since epoch")

16640 days since epoch

>>> # Part 2

>>> midnight_on_mydate = num_days * 24 * 3600
>>>
>>> seconds_since_midnight = mydate - midnight_on_mydate
>>>
>>> hours = seconds_since_midnight // 3600
>>> minutes = (seconds_since_midnight - (hours * 3600)) // 60
>>> seconds = seconds_since_midnight - (hours * 3600 + minutes * 60)
>>>
>>> time_of_day = "%s:%s:%s" % (hours, minutes, seconds)
>>>
>>> print(time_of_day, "on", num_days, "days since epoch")

13:54:54 on 16640 days since epoch
@JCWDFCS
Copy link

JCWDFCS commented Apr 9, 2017

import time

epoch = time.time()
seconds_in_a_day = 24 * 60 * 60
seconds_in_an_hour = 60 * 60
seconds_in_a_minute = 60

days = epoch // seconds_in_a_day
hours = (epoch % seconds_in_a_day) // seconds_in_an_hour + 8
minutes = (epoch % seconds_in_a_day) % seconds_in_an_hour // seconds_in_a_minute
seconds = (epoch % seconds_in_a_day) % seconds_in_an_hour % seconds_in_a_minute
print("%s: %s: %s: %s" %(days, hours, minutes, seconds))
print("Beijing Current time is %d: %d: %d: %d" %(days, hours, minutes, seconds")) 

@AndrewReifel
Copy link

AndrewReifel commented Jan 22, 2020

Looks like the extra closing quote is not needed on final print statement.

@Janardanchorat
Copy link

import time mytime=time.time() seconds=mytime mins=mytime/60 hrs=mins/60 days=hrs/24 weeks=days/7 months=weeks/4 years=months/12 print(seconds,mins,hrs,days,weeks,months,years)

@neo-shruti-ghosh
Copy link

import time

epoch=time.time()

#606024=86400
total_sec = epoch % 86400
#60*60
hours = int(total_sec/3600)
total_minutes = int(total_sec/60)
mins = total_minutes % 60
sec = int(total_sec % 60)

days=int(epoch/86400)

print("The Current time is",hours,':',mins,':',sec)
print("Days since epoch:", days)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants