I writing a program telling time on Python

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 2K views
Eclair_de_XII
Messages
1,085
Reaction score
92

Homework Statement


"Using the time module, write a program that tells one the current time in hours, minutes, and seconds, and time since January the first of 1970."

Homework Equations


import time
% = mod division
int = returns integer value of number

The Attempt at a Solution


Python:
import time

now = int(time.time())

second = 1
minute = 60*second
hour = 60*minute
day = 24*hour
year=365.25*day

d = now%year
h = d%day # error here?
m = h%hour
s = m%minute

day_count = int(d/24/3600)
hour_count = int(h/3600)
min_count = int(m/60)
sec_count = int(s)
days_since_epoch = int(now/3600/24)

print("The current time is ",hour_count," hours, ",min_count," minutes, and ",sec_count," seconds.","It has been ",
      days_since_epoch," days since the epoch.")
<Moderator's note: code tags added. Please use them.>

I have the minutes and seconds down correctly, but when I execute this program, it gives me the wrong hour. I know it hasn't anything to do with Python thinking I'm in some other timezone, because when I tried to calculate the time directly, it gave me the correct hour. So I'm thinking I messed up on the bolded text <Moderator's note: comment added instead> above. I don't quite know how to fix this...

*Oops, I noticed that I should have erased that "finding" in the title of this topic...
<Moderator's note: title fixed.>
 
Last edited by a moderator:
on Phys.org
Eclair_de_XII said:

Homework Statement


"Using the time module, write a program that tells one the current time in hours, minutes, and seconds, and time since January the first of 1970."

Homework Equations


import time
% = mod division
int = returns integer value of number

The Attempt at a Solution


Python:
import time

now = int(time.time())

second = 1
minute = 60*second
hour = 60*minute
day = 24*hour
year=365.25*day

d = now%year
h = d%day # error here?
m = h%hour
s = m%minute

day_count = int(d/24/3600)
hour_count = int(h/3600)
min_count = int(m/60)
sec_count = int(s)
days_since_epoch = int(now/3600/24)

print("The current time is ",hour_count," hours, ",min_count," minutes, and ",sec_count," seconds.","It has been ",
      days_since_epoch," days since the epoch.")
<Moderator's note: code tags added. Please use them.>

I have the minutes and seconds down correctly, but when I execute this program, it gives me the wrong hour. I know it hasn't anything to do with Python thinking I'm in some other timezone, because when I tried to calculate the time directly, it gave me the correct hour. So I'm thinking I messed up on the bolded text <Moderator's note: comment added instead> above. I don't quite know how to fix this...

*Oops, I noticed that I should have erased that "finding" in the title of this topic...
<Moderator's note: title fixed.>

I'm not sure that that's what the exercise is asking for. Python already has functions for extracting hour, minute, and second, so you might be expected to use those.

But if you're going down the approach that you're taking, it's not correct.

In figuring out the date, you can't just use that a year is 365.25 days. A new calendar year never starts in the middle of a day. So each year is always an integer number of days. Sometimes a year is 365 days, and sometimes it is 366 days. It's 365 days except on a "leap year", which is any year divisible by 4, except for years ending with 100, 200, 300, 500, 600, 700, 900. The leap years since 1970 are: 1972, 1976, 1980, 1984, 1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, and the next one is 2020.

But actually, you don't need to know the year in order to figure out the hours, minutes and seconds. Your variable "now" is the seconds past 1970. So
"now % day" gives the seconds since midnight. You have to divide that by "hour" (and take the integer part) to get the number of hours past midnight.
 
  • Like
Likes   Reactions: scottdave, anorlunda and BvU
I like the
Code:
now % day
as an alternate approach.
But as someone who's been working with Python for less than a year, I find that usually if there a task that I'm trying to do, there's a decent chance that a function or package exists to do it (or most of it) for me.
Personally, I think it helps to know how to do it both ways. Perhaps after trudging through making your own function, then finding out one already may help you remember it better?

I wonder how many coders actually know how to code a sort algorithm (or more than one)?
Just some thoughts.
 
stevendaryl said:
I'm not sure that that's what the exercise is asking for.

It's a practice in the use of the modulus operator. I think I also sort of figured out what my original problem was. With my original setup, I was calculating the number of seconds since 1970 mod the number of seconds in 365.25 days. When I took the modulus of d = now%year w.r.t. day, I ended up with a remainder that is 0.25*3 days more than if I had took the modulus of the seconds since 1970 w.r.t. the seconds in 365 days (since three years have passed since the last leap year).