I writing a program telling time on Python

Click For Summary

Discussion Overview

The discussion revolves around a programming task involving the Python time module, specifically aimed at writing a program that calculates the current time in hours, minutes, and seconds, as well as the time elapsed since January 1, 1970. Participants explore various approaches to solving the problem and discuss potential errors in the code.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares their code and notes that while minutes and seconds are calculated correctly, the hour calculation appears incorrect, suspecting an error in their modulus operation.
  • Another participant suggests that Python has built-in functions for extracting time components, implying that the exercise may expect the use of these functions instead of manual calculations.
  • A participant critiques the approach of assuming a year has 365.25 days, explaining that leap years complicate this calculation and that years can have either 365 or 366 days.
  • One participant proposes using the expression "now % day" to find the seconds since midnight, suggesting that this could simplify the calculation of hours.
  • Another participant reflects on the value of understanding both manual calculations and built-in functions, emphasizing the learning experience that comes from coding tasks.
  • A participant acknowledges their original misunderstanding regarding the modulus operation and how it relates to leap years, clarifying their thought process on the calculations involved.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to the problem, with some advocating for the use of built-in functions and others defending the manual calculation method. The discussion does not reach a consensus on the most effective solution.

Contextual Notes

There are unresolved assumptions regarding the handling of leap years and the implications of using 365.25 days in calculations. Additionally, the discussion highlights the potential for confusion in the modulus operation as it relates to time calculations.

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   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).
 

Similar threads

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