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
Related
This question already has answers here:
How do I get the current time?
(54 answers)
Closed 2 years ago.
How do you get the current time in python? I have seen people get the date and time, but I only want the time as a string.
For example, if the current time was 8:30, the command should output:
"8:30".
Also, how do you find what day of the week it is>
For example, if it is Tuesday hen the output should be:
2.
from datetime import datetime
print(datetime.now().strftime("%H:%M"))
Will return this:
13:29
To get seconds too:
print(datetime.now().strftime("%H:%M:%S"))
You can use
import datetime
my_time = datetime.datetime.now().time()
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.
I'm trying to follow these answers and get the elapsed seconds since Jan 1st 1970 for each element in my array (my_times). I'm then using these values to find the time intervals between each consecutive time. Either approach I take seems to give the wrong answer for at least one pair of times.
Mark Byers answer
To get the seconds since Jan 1st 1970, he suggested to try:
time.mktime(my_time.timetuple())
However this does not seem to work for times "2017-11-05 01:46:00+00" and "2017-11-05 01:47:00+00".
When I run the below code, it says the values are separated by 3660.0 seconds instead of 60.0 seconds
from datetime import datetime
import time
my_time1 = datetime.strptime("2017-11-05 01:46:00+00", "%Y-%m-%d %H:%M:%S+%f")
my_time2 = datetime.strptime("2017-11-05 01:47:00+00", "%Y-%m-%d %H:%M:%S+%f")
time.mktime(my_time2.timetuple()) - time.mktime(my_time1.timetuple())
Andrzej Pronobis' answer
To get the seconds since Jan 1st 1970, he suggested to try:
my_time.timestamp()
This fixed the two earlier times however it no longer works for times "2017-11-05 01:59:00+00" and "2017-11-05 02:00:00+00". The same issue appears, I get 3660.0 seconds instead of 60.0 seconds
from datetime import datetime
my_time1 = datetime.strptime("2017-11-05 01:59:00+00", "%Y-%m-%d %H:%M:%S+%f")
my_time2 = datetime.strptime("2017-11-05 02:00:00+00", "%Y-%m-%d %H:%M:%S+%f")
my_time2.timestamp() - my_time1.timestamp()
I'd like to know if I'm doing anything wrong? Also is there a better way to find all consecutive time intervals when the datetime is given as a String?
Edit:
Thank you John, that fixed the problem. Oddly, changing the format from +%f to %z still ran into the same issue.
What did work was running sudo ln -sf /usr/share/zoneinfo/UTC /etc/localtime (changes my computer's time to UTC) and then evaluating all the times
This is a case of "garbage in, garbage out." Here:
datetime.strptime("2017-11-05 01:59:00+00", "%Y-%m-%d %H:%M:%S+%f")
You probably think that +00 on the end means "UTC time", but the %f format specifier means "fractional seconds."
In any case, you're apparently running on a system where the time zone is set to one with daylight saving time part of the year. 2 AM happens twice on the DST changeover date in November, so your code is working as written (it's ambiguous, basically).
Put another way: your issue is not that you're computing time deltas incorrectly. Your issue is that you are loading the times from strings incorrectly (or ambiguously).
This question already has answers here:
Python script to do something at the same time every day [duplicate]
(4 answers)
Closed 5 years ago.
How can i check the time in Python and run a command every day at the same time?
if datetime.time() == 17.30:
bot.sendMessage(ChatID, "Message")
I found a lot in the Internet, but i didn't found something
which really helped me. I like to check the hour and the minutes.
Later it should be implimented into a TelegramBot, which should send a Message to a Group every day. So Crontab would not be so nice also a timer.
Thanks for helping
This is probably better handled with a cron job, but here's the code
import datetime.datetime
now_time = datetime.now()
If now_time.hour == 17 and now_time.minute == 30:
#do something
I'm beginning to learn python and I've tried to create a script to run a certain command at a certain time. This is what I have so far:
import datetime
day_of_week = datetime.date.today().weekday() # 0 is Monday, 6 is Sunday
time = datetime.datetime.now().time()
if day_of_week == 4 and (time >= datetime.time(2,45) and time < datetime.time(9)):
#do something
elif (day_of_week == 3 and time > datetime.time(22)) or (day_of_week == 4 and time < datetime.time(2,45)):
#wait until 2h45
else:
#do nothing
I want the script to continue only on a certain day and when it's around 19h00 PST/PDT (2h45 GMT/UTC). The one I wrote works fine with GMT/UTC, but I'm certain that it will fail when daylight savings starts or if I change the local timezone to a different one.
What could I do to make the script check for the current time in PST/PDT and continue from there, ignoring or converting the local timezone?
#Ruben, please try this code below. It always look at the time in US/Pacific timezone. Hope it helps. Thanks.
import datetime
import pytz
pst_timezone = pytz.timezone("US/Pacific")
time=datetime.datetime.now(pst_timezone).time()
print(time)
Result: 12:13:10.136603
You should look into pytz
Python: display the time in a different time zone will be helpful too
Pythons Datetime pulls from the system time. So if the system is setup to use DST then you will be fine.
If you don't or cant trust the system time then no build in function will be accurate. You will then need to have your program go out and pull time from another server.