Calculating the minutes since midnight [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
Have run into more of a maths problem than code. So far have programmed to retrieve the local time, print the local time as "Current Time: " and then programmed the variables to print "Number of minutes since midnight: ".
looks like this
import time
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print("Current Time: ",current_time)
hour = time.strftime("%H")
minute = time.strftime("%M")
print ("Number of minutes since midnight: ",int(hour)*60+int(minute))
so my output is
Current Time: 22:16:15
Number of minutes since midnight: 1336
Except a quick google search tells me it's closer to 2,777 minutes since midnight.
This is my first program, so if you're wondering why I want to know how many minutes since midnight it is at any given time without JFGI, I just do. It's been a fun problem to solve so far, and I would hate to leave it unfinished because I don't know the maths I need to know yet.
Thanks in advance!

I think your code is actually working as intended!
One quick recommendation for an improvement though:
When you call
t = time.localtime()
You're storing the current time in the variable t, but then when you later call
hour = time.strftime("%H")
minute = time.strftime("%M")
you are actually asking the program to fetch the system clock time again twice, rather than using the already stored time value you have in t.
You could instead do the following and access the hour and time values from the t object directly.
import time
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print("Current Time: ", current_time)
print ("Number of minutes since midnight: ",int(t.tm_hour)*60+int(t.tm_min))
I hope you're enjoying your first steps into programming!

I believe whatever you googled was incorrect, I tried this out and also got 1336.
current_time = "22:16:15"
hrs, mins, _ = current_time.split(":")
since_midnight = int(hrs) * 60 + int(mins)
The total number of minutes in a day is 1440, so it clearly cant be more than that.

Related

How to get the Date and Time of Current Location in Python [duplicate]

This question already has answers here:
How do I get the current time?
(54 answers)
Closed 3 years ago.
I want to get the Date and Time of The Current Location. If somebody runs the code in a different country thought it will show the date and and time for them, and not for me. If you know any commands for this please put the down bellow. I prefer If I don't need a library so that people who get the code don't need to download the library.
I think you can not get the date or time of a location.
This code gets your device's date and time:
from datetime import datetime
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
current_date = now.strftime("%D")
print("Current Time: ", current_time)
print("Current Date: ", current_date)
A standard Python module called datetime can probably help you with retrieving the current system date and time from where the program is run
Since it's a standard module, there would be no need to install it rather just import it in the program itself.
Example :
import datetime
x = datetime.datetime.now()
print(x)
Would give you the output as :
2019-07-20 00:52:27.158746
The above output contains the current year, month, day, hour, minute, second, and microsecond.

Receiving inaccurate time from python time module? [duplicate]

This question already has answers here:
Python: strftime, gmtime not respecting timezone
(2 answers)
Closed 3 years ago.
I am a beginner in programming and I was messing around with the time module in python, as I recently got a raspberry pi and want to start doing some projects. I imported the time module, and just made a loop to print out the time every second. The code runs correctly, but the time given is not accurate to my location. Currently, it is the 14th and a Friday, around 9 pm, but it is returning the 15th a Saturday, with 0 hours and 10 minutes. Does anyone know how I can obtain the correct time?
I tried a couple of the different functions to receive the current time like .localtime() and .gmtime() but they're all the same.
import time
while(True):
thisTime = time.gmtime()
print(time.asctime(thisTime))
time.sleep(1)
Go ahead and check out this post, I think this is your solution:
Python get current time in right timezone
So assuming your computer's time is configured correctly, you could use this:
from datetime import datetime
naive_dt = datetime.now()
It's unclear what else you've tried, although the following should work:
localtime = time.localtime()
print(time.asctime(localtime))
↳ https://docs.python.org/3/library/time.html#time.localtime

Python and module - date

If I don't have access to the Python time module, how would I determine the number of days that have passed since I was born? That is, within the code, how many days old I am?
The code I am trying to understand better is this:
import time
start = raw_input(“Enter the time stamp in seconds: “)
start = float(start)
end = time.time()
elapsed = end - start
st_elapsed = time.gmtime(elapsed)
print "\n"
hours_mins_secs = time.strftime("%H:%M:%S", st_elapsed)
print "Elapsed time in HH:MM:SS ->", hours_mins_secs, "\n"
Now, I looked to the site https://docs.python.org/2/library/time.html
but I didn't find the alternative related to time, without using module time.
My goal is understand better this code.
This sounds like a homework question. You should give us what you've done so far and we will help you. SO users are not your personal coders.
(No criticism intended)

Getting microseconds past the hour

I am working on a quick program to generate DIS (Distributed Interactive Simulation) packets to stress test a gateway we have. I'm all set and rearing to go, except for one small issue. I'm having trouble pulling the current microseconds past the top of the hour correctly.
Currently I'm doing it like this:
now = dt.now()
minutes = int(now.strftime("%M"))
seconds = int(now.strftime("%S")) + minutes*60
microseconds = int(now.strftime("%f"))+seconds*(10**6)
However when I run this multiple times in a row, I'll get results all over the place, with numbers that cannot physically be right. Can someone sanity check my process??
Thanks very much
You can eliminate all that formatting and just do the following:
now = dt.now()
microseconds_past_the_hour = now.microsecond + 1000000*(now.minute*60 + now.second)
Keep in mind that running this multiple times in a row will continually produce different results, as the current time keeps advancing.

I don't know what to do next [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am making a program to tell you the day of the week when told the date.
This is what I have got so far but I don't know how to continue.
print('Welcome to the Daygram, the program that tells you the day on any date.')
print('Enter the date in the following format: DD MM YYYY')
date_input = input(What is the date? (DD MM YYYY)')
date_array = date_input.split(' ')
date = date_array[0]
month = date_array[1]
year = date_array[2]
If anyone could help me so I could figure out what I need to do next.
I'll help you with some parts, but most of this code should be a learning experience for you.
import datetime
# prompt here for the date
# put it into a datetime.datetime object named "day"
# this is the part of the code you need to type
day_array = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
day_of_week = day_array[day.weekday()]
The datetime module should be your go-to when working with dates. It gives you a comprehensive object with a list of methods relating to time and date, which is EXACTLY what you're looking for. You initialize a datetime object using datetime.datetime(YEAR,MONTH,DAY) (where you fill in the year, month, and day). You can also set a time, but it's not necessary in your use case.
The datetime object has a method called weekday that returns a number (0 through 6) that represents which day of the week that day represents. I built an array day_array that maps each day to the index it represents (e.g., a Monday will return 0, so day_array[0] == "Monday" and etc).
At this point, it's child's play. The hard part is going to be to prompt the user for a day and convert that into a datetime object. That I'll leave for you.

Categories