0

The time module provides a function, also named time ,that return 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 a day in hours, minutes, and seconds ,plus the number of days since the epoch.

I don't see how this exercise connect to the chapter 5.Conditionals and Recursion and how to write code to make this happen?

Thinks for answering my question. So, as your advice, i wrote a section of code like this:

import time

secs = time.time()

def time():
    mins = secs / 60
    hours = mins / 60
    days = hours/24
    print 'The minues:',mins,'The hours:' ,hours, 'The days:',days

print 'The seconds:', secs, time()

It output the result like this: The seconds:1481077157.6 The minues:24684619.2933 The hours:411410.321554 The days:17142.0967314 none, My question is where is "none" come from?

chenyibo416
  • 31
  • 1
  • 5
  • 2
    Regardless of whether or not it is relevant to the current chapter, can you show what yuo have tried so far? – voodoo-burger Dec 06 '16 at 15:00
  • According to https://docs.python.org/3/library/time.html#time.time , `time.time()` returns the number of seconds since EPOCH. The exercise expects you to convert this into a date. – pradyunsg Dec 06 '16 at 15:14
  • You can write this algorithm in a recursive manner, no problem. It's weird and pointless, but maybe for the pure sake of an exercise it has some value. So maybe there is no other point than learning the syntax, and some basic algorithmic thinking. IMHO there is no practical reason to do it this way, but probably the author should be the one who answers this. Looking at the book I guess it expects you to do it like the `countdown` shown in `5.8`. – luk32 Dec 06 '16 at 15:15
  • I am quite sure that integer division and "modulo" have been covered lately... – Jasper Dec 06 '16 at 15:15
  • Should help - http://stackoverflow.com/a/31687530/2308683 – OneCricketeer Dec 06 '16 at 15:16
  • @cricket_007 I doubt it. The point seems to be to use the recursion, not to "get job done". – luk32 Dec 06 '16 at 15:18

4 Answers4

0
import time

def current_time():
    current=time.time()
    t_sec = current % 86400
    c_hours = int(t_sec/3600)
    t_minutes = int(t_sec/60)
    c_mins = t_minutes % 60
    c_sec = int(t_sec % 60)

    days=int(current/86400)

    print("The Current time is",c_hours,':',c_mins,':',c_sec)
    print('Days since epoch:', days)
  • it would be nice if you could add a comment explaining your answer. – Simas Joneliunas Jan 25 '20 at 04:06
  • Welcome to Stack Overflow! Please try to provide a nice description of how your solution works. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) Thanks – Rajesh Pandya Jan 25 '20 at 04:25
0
>import time
>epoch=time.time()

>#60*60*24=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)
neoflies
  • 3
  • 2
  • 1
    Please add some details about your answer, like why does it work, what was the mistake done by the person who asked the question, and how is that corrected. Code without any discussion or details says almost nothing to the person that has no idea what was wrong initially. Please complete your answer! Thanks! – EnriqueBet May 22 '20 at 15:59
  • Check this out once – neoflies May 22 '20 at 16:09
0

EDIT in response to: "My question is where is "none" come from?"

In your print function at the end, you call 'time()', but you do not return anything, thus it prints 'None'. If you want 'None' gone, try this:

import time

secs = time.time()

def time():
  mins = secs / 60
  hours = mins / 60
  days = hours/24
  print ('The minues:',mins,'The hours:' ,hours, 'The days:',days)

time()
print ('The seconds:', secs)

Though the point should probably be, that if you want to use a recursive function, you should return something that you then use to calculate with.


Let's have a look at the exercise description:

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

How I understand the question, is that the answer should be formatted something like this:

Today is 18 hours, 12 minutes, 11 seconds and 18404 days since epoch. 

To get this answer, you could use a function using 'modulus operator', which is a part of paragraph 5.1. You could then subtract the variable containing 'today' with the number of days, then the hours, minutes and seconds. This is somewhat a recursive process, which could help your understanding for subsequent exercises.

estherwn
  • 156
  • 6
0
import time

#the epoch time
epoch = int(time.time())

#calculate number of days since epoch
days = epoch / (60 * 60 * 24)
hour = days % int(days) * 24
min  = hour % int(hour) * 60
sec  = min % int(min) * 60

print(f"Days since epoch: {int(days)}\nCurrent Time: {int(hour)}:{int(min)}:{int(sec)}")
David Buck
  • 3,752
  • 35
  • 31
  • 35
Mustafa Kemal
  • 762
  • 1
  • 5
  • 11