I writing a program telling time on Python

In summary, the homework statement asks for a program that tells one the current time in hours, minutes, and seconds, and time since January 1, 1970. The attempt at a solution includes importing the time module, creating an int variable now, and calculating the current time in seconds, minutes, and hours. The current time is currently 12:00pm, but it has been 3 days since the epoch.
  • #1
Eclair_de_XII
1,083
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
  • #2
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
  • #3
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.
 
  • #4
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).
 

What is Python?

Python is a high-level, interpreted programming language that is used for a variety of applications such as web development, data analysis, and scientific computing. It is known for its simple and readable syntax which makes it popular among programmers.

How do I write a program in Python?

To write a program in Python, you will need a text editor or an integrated development environment (IDE) such as PyCharm or Visual Studio Code. Open your chosen editor and start writing your code using Python's syntax. Once you have finished writing your program, save it with the ".py" extension and run it through a Python interpreter.

What is the purpose of a "telling time" program?

A "telling time" program is used to display the current time in a readable format. It can be useful for various applications such as scheduling, time tracking, and reminders. It can also serve as a learning tool for beginners to understand how to work with date and time in programming.

What are the basic steps for creating a "telling time" program in Python?

The basic steps for creating a "telling time" program in Python include importing the necessary modules, getting the current time using the datetime module, formatting the time into a readable format, and printing the time to the console. Optional steps may include adding user input or creating a graphical user interface (GUI) for the program.

Are there any resources available for learning how to create a "telling time" program in Python?

Yes, there are many online tutorials, articles, and courses available for learning how to create a "telling time" program in Python. You can also refer to official documentation and forums for help and guidance. Some popular resources include Codecademy, W3Schools, and Python.org.

Similar threads

  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
2
Views
899
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
25
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
Replies
10
Views
960
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top