Python I writing a program telling time on Python

Click For Summary
The discussion revolves around a programming assignment that involves using Python's time module to calculate the current time in hours, minutes, and seconds, as well as the time elapsed since January 1, 1970. The initial code attempts to calculate these values using modulus operations but encounters an error in determining the correct hour. The user realizes that the method of calculating days based on a year being 365.25 days is flawed, as it does not account for leap years accurately. Instead, a more effective approach is suggested, which involves using the total seconds since the epoch directly to derive the current time. It is noted that Python has built-in functions that can simplify this task, highlighting the importance of leveraging existing libraries when programming. The discussion emphasizes the learning process of understanding both manual calculations and utilizing built-in functions for efficiency.
Eclair_de_XII
Messages
1,082
Reaction score
91

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:
Technology news 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 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).
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
14
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 97 ·
4
Replies
97
Views
9K
  • · Replies 5 ·
Replies
5
Views
1K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K