Calculate time after X hours? - python

For example, if the start time is 8:00am how do I calculate the time after 20 hours have passed?

Need to use something like timedelta
from datetime import datetime, timedelta
twenty_hours= datetime.now() + timedelta(hours=20)
ofcourse you'll change datetime.now() to your 8am or what ever time you wish
>>> format(twenty_hours, '%H:%M:%S')
'23:24:31'

There are at least two possible interpretations of your question:
Find the local time that is exactly 20 hours from now:
#!/usr/bin/env python3
from datetime import datetime, timedelta, timezone
now = datetime.now(timezone.utc) # current time in UTC
in20hours = (now + timedelta(hours=20)).astimezone() # local time in 20 hours
print("Local time in 20 hours: {in20hours:%I:%M %p}".format(**vars())) # AM/PM
Find the time while ignoring any changes in the local UTC offset (due to DST transitions or any other reason):
#!/usr/bin/env python
from datetime import datetime, timedelta, timezone
now = datetime.now() # local time
by20hours = now + timedelta(hours=20) # move clock by 20 hours
print("Move clock by 20 hours: {by20hours:%I:%M %p}".format(**vars())) # AM/PM
Related: python time + timedelta equivalent
Both methods produce the same result if the local utc offset won't change during the next 20 hours. Otherwise the second method fails, to find the time after 20 hours have passed.
You might need the second method if you want to do something at the same local time no matter how many hours have passed in between e.g., if you want to get up at 6am no matter whether 24 hours have passed or not. See How can I subtract a day from a Python date?
More details on the time arithmetics if you are working with local time see at Find if 24 hrs have passed between datetimes - Python.

Related

Trying to find the difference between 2 datetime objects and getting only Hours, Minutes, and Seconds [duplicate]

This question already has answers here:
Convert timedelta to total seconds
(4 answers)
Closed 27 days ago.
I am pulling an ending time from a json api response. Then I am trying to calculate the time remaining, before the end time, to call a function after it ends.
end_time_string = data['endTime'] # Get the end time in a string in weird format
date_format = "%Y%m%dT%H%M%S.%fZ" # Specify the date format for the datetime parser
end_time = datetime.strptime(end_time_string, date_format) # turn the date time string into datetime object
current_time = datetime.utcnow() # Get current time in UTC time zone, which is what CoC uses.
time_remaining = end_time - current_time # Get the time remaining till end of war
My end_time is a datetime object. My current_time is a datetime object. But time_remaining is a timedelta object. I am able to pull the hours, minutes and seconds from the object using:
hours, minutes, seconds = map(float, str(time_remaining).split(':'))
But the problem is that sometimes the time_remaining has days in it, and sometimes it doesn't.
1 day, 4:55:22.761359
-1 days, 23:59:08.45766
When there are days involved, specifically when the timedelta object goes negative, my script fails.
What is the best find the amount of time between my two datetime objects in ONLY hours, minutes, and seconds, without days included?
timedelta is an object. One of its methods is total_seconds() so dividing by 3600 gives hours. Also dividing by another timedelta gives a float result of the ratio, so divide by timedelta(hours=1) to get the time in hours:
>>> import datetime as dt
>>> x = dt.timedelta(days=1, seconds=5000)
>>> x.total_seconds() / 3600
25.38888888888889
>>> x / dt.timedelta(hours=1)
25.38888888888889
or in hours, minutes, seconds:
>>> hours, remaining_seconds = divmod(x.total_seconds(), 3600)
>>> minutes, seconds = divmod(remaining_seconds, 60)
>>> hours, minutes, seconds
(25.0, 23.0, 20.0)
So, a timedelta object has days, seconds and microseconds. Multiply the days by 24 to convert it into hours, and then some nice math with modulo (%) and the usefull // operator, for which I will quote something:
//: Divides the number on its left by the number on its right, rounds
down the answer, and returns a whole number.
combining everything you get a nice f-string with padding for the zeros:
f"{td.seconds//3600 + td.days*24:02}:{(td.seconds//60)%60:02}:{td.seconds%60:02}:{td.microseconds:06}"
To put this into code:
from datetime import datetime, timedelta
# 3670 seconds is 1h1m10s
tomorrow = datetime.utcnow() + timedelta(1, 3670, 123)
current_time = datetime.utcnow()
td = tomorrow - current_time
print(td)
print(td.days)
print(td.seconds)
print(td.microseconds)
print(f"{td.seconds//3600 + td.days*24:02}:{(td.seconds//60)%60:02}:{td.seconds%60:02}:{td.microseconds:06}")
Which generates the following output:
1 day, 1:01:10.000123
1
3670
123
25:01:10:000123

Need to check current time from NTP server is within some time range in python

I need to check if current time from NTP server (using ntplib module) is within some required time range.
I have written the below code but I need some help to finish this task.
#!/usr/bin/python
import ntplib
from time import ctime
ntp_client = ntplib.NTPClient()
response = ntp_client.request('pool.ntp.org')
print (ctime(response.tx_time))
Output:
Fri Aug 16 13:26:16 2019
Now I need to check the ntp current time "13:26:16" is within the time range "09:30:00 - 15:30:00", so how can we check this in Python?
It must say current ntp time is/not in time range "09:30:00 - 15:30:00"
Convert the epoch timestamp to a suitable representation such as a Python datetime object and inspect the hours and minutes fields.
from time import gmtime
# ...
dt = time.gmtime(response.tx_time)
if (dt.tm_hour == 9 and dt.tm_min >= 30) or (
10 <= dt.tm_hour <= 14) or (
dt.tm_hour == 15 and dt.tm_min <= 30):
print('yes indeed')
else:
print('not')
Time zones are always tricky; the above gets you UTC (as vaguely implied by the name gmtime). If you want to compare to the value in your local time zone, try time.localtime instead.

Converting a UTC Time to Epoch format

I'm trying to convert a specific UTC time & date to seconds since the Epoch; however, I have not been able to figure out how to do this since I am not in the UTC time zone.
I tried using the datetime module to convert a date & time to seconds since the epoch, but python has been using my system's local time so the returned value is off by 7 hours. I understand that I could simply subtract 7*60 so that it would work in my time zone; however, I need this to work in multiple time zones without hardcoding the time change into my program.
This works except it uses the system time (MST), but I am looking for a solution that is specifically UTC time. Note the variables defined here represent an example of a time in UTC that I am trying to convert to seconds since the epoch.
import datetime
year=2019
month=5
day=9
hour=21
minute=45
Time=datetime.datetime(year, month, day, hour, minute).timestamp()
print(Time)
Output:
1557463500.0
Desired output (7 hours earlier):
1557438300.0
import datetime
year=2019
month=5
day=9
hour=21
minute=45
e = datetime.datetime(1970, 1, 1, 0, 0)
t = datetime.datetime(year, month, day, hour, minute)
print((t-e).total_seconds())
You can caclulate the timestamp of epoch date using datetime.datetime.utcfromtimestamp(0).timestamp() and then subtract your current timestamp from it
import datetime
year=2019
month=5
day=9
hour=21
minute=45
#Date timestamp
dt_timestamp=datetime.datetime(year, month, day, hour, minute).timestamp()
#Epoch timestamp
epoch = datetime.datetime.utcfromtimestamp(0).timestamp()
#Epoch
print(dt_timestamp-epoch)
The output will be
1557438300.0

How to compute the time difference between two time zones in python?

How can I compute the time differential between two time zones in Python? That is, I don't want to compare TZ-aware datetime objects and get a timedelta; I want to compare two TimeZone objects and get an offset_hours. Nothing in the datetime library handles this, and neither does pytz.
Here is a solution using the Python library Pytz which solves the issue of ambiguous times at the end of daylight saving time.
from pytz import timezone
import pandas as pd
def tz_diff(date, tz1, tz2):
'''
Returns the difference in hours between timezone1 and timezone2
for a given date.
'''
date = pd.to_datetime(date)
return (tz1.localize(date) -
tz2.localize(date).astimezone(tz1))\
.seconds/3600
The examples below calculate the difference in hours between UTC and Australia time for the first of January and first of June respectively. Notice how daylight savings are taken into consideration.
utc = timezone('UTC')
aus = timezone('Australia/Sydney')
tz_diff('2017-01-01', utc, aus)
# 11.0
tz_diff('2017-06-01', utc, aus)
# 10.0
Thanks
The first thing you have to know is that the offset between two time zones depends not only on the time zones in question, but on the date you're asking about. For example, the dates on which Daylight Savings Time began and ended changed in the US in 2007. While fundamental time zone logistics change only infrequently in any single location, the rate of change globally is impossible to ignore. Therefore, you have to incorporate the date in question into your function.
Having completed the necessary preface, the actual function isn't too hard to write if you take advantage of the pendulum library. It should look something like this:
import pendulum
def tz_diff(home, away, on=None):
"""
Return the difference in hours between the away time zone and home.
`home` and `away` may be any values which pendulum parses as timezones.
However, recommended use is to specify the full formal name.
See https://gist.github.com/pamelafox/986163
As not all time zones are separated by an integer number of hours, this
function returns a float.
As time zones are political entities, their definitions can change over time.
This is complicated by the fact that daylight savings time does not start
and end on the same days uniformly across the globe. This means that there are
certain days of the year when the returned value between `Europe/Berlin` and
`America/New_York` is _not_ `6.0`.
By default, this function always assumes that you want the current
definition. If you prefer to specify, set `on` to the date of your choice.
It should be a `Pendulum` object.
This function returns the number of hours which must be added to the home time
in order to get the away time. For example,
```python
>>> tz_diff('Europe/Berlin', 'America/New_York')
-6.0
>>> tz_diff('Europe/Berlin', 'Asia/Kabul')
2.5
```
"""
if on is None:
on = pendulum.today()
diff = (on.set(tz=home) - on.set(tz=away)).total_hours()
# what about the diff from Tokyo to Honolulu? Right now the result is -19.0
# it should be 5.0; Honolulu is naturally east of Tokyo, just not so around
# the date line
if abs(diff) > 12.0:
if diff < 0.0:
diff += 24.0
else:
diff -= 24.0
return diff
As stated in the documentation, you may not get a stable result for this between any two given locations as you sweep across the days of the year. However, implementing a variant which chooses the median result over the days of the current year is an exercise left for the reader.
Here's another solution:
from datetime import datetime
from pytz import timezone
from dateutil.relativedelta import relativedelta
utcnow = timezone('utc').localize(datetime.utcnow()) # generic time
here = utcnow.astimezone(timezone('US/Eastern')).replace(tzinfo=None)
there = utcnow.astimezone(timezone('Asia/Ho_Chi_Minh')).replace(tzinfo=None)
offset = relativedelta(here, there)
offset.hours
Here what we're doing is converting a time to two different time zones. Then, we remove the time zone information so that when you calculate the difference between the two using relativedelta, we trick it into thinking that these are two different moments in time instead of the same moment in different time zones.
The above result will return -11, however this amount can change throughout the year since US/Eastern observes DST and Asia/Ho_Chi_Minh does not.
I created two functions to deal with timezone.
import datetime
import pytz
def diff_hours_tz(from_tz_name, to_tz_name, negative=False):
"""
Returns difference hours between timezones
res = diff_hours_tz("UTC", "Europe/Paris") : 2
"""
from_tz = pytz.timezone(from_tz_name)
to_tz = pytz.timezone(to_tz_name)
utc_dt = datetime.datetime.now(datetime.timezone.utc)
dt_from = dt_to = datetime.datetime.utcnow()
dt_from = from_tz.localize(dt_from)
dt_to = to_tz.localize(dt_to)
from_d = dt_from - utc_dt
if from_d.days < 0:
return diff_hours_tz(to_tz_name, from_tz_name, True)
dt_delta = dt_from - dt_to
negative_int = -1 if negative else 1
return int(dt_delta.seconds/3600)*negative_int
def dt_tz_to_tz(dt, from_tz_name, to_tz_name):
"""
Apply difference hours between timezones to a datetime object
dt_new = dt_tz_to_tz(datetime.datetime.now(), "UTC", "Europe/Paris")
"""
hours = diff_hours_tz(from_tz_name, to_tz_name)
return dt+datetime.timedelta(hours=hours)
# Usage example
res = diff_hours_tz("Europe/Paris", "America/New_York")
# Result : -6
res = diff_hours_tz("UTC", "Europe/Paris")
# Result : 2
now = datetime.datetime.now()
# Result : 2019-06-18 15:10:31.720105
dt_new = dt_tz_to_tz(now, "UTC", "Europe/Paris")
# Result : 2019-06-18 17:10:31.720105
dt_new = dt_tz_to_tz(now, "Europe/Paris", "America/New_York")
# Result : 2019-06-18 09:10:31.720105
dt_new = dt_tz_to_tz(now, "America/New_York", "Europe/Paris")
# Result : 2019-06-18 21:10:31.720105
I hope it will help !
Here is a code snippet to get the difference between UTC and US/Eastern, but it should work for any two timezones.
# The following algorithm will work no matter what is the local timezone of the server,
# but for the purposes of this discussion, let's assume that the local timezone is UTC.
local_timestamp = datetime.now()
# Assume that utc_timestamp == 2019-01-01 12:00.
utc_timestamp = pytz.utc.localize(local_timestamp)
# If it was 12:00 in New York, it would be 20:00 in UTC. So us_eastern_timestamp is a UTC
# timestamp with the value of 2019-01-01 20:00.
us_eastern_timestamp = timezone("US/Eastern").localize(local_timestamp).astimezone(pytz.utc)
# delta is a Python timedelta object representing the interval between the two timestamps,
# which, in our example, is -8 hours.
delta = utc_timestamp - us_eastern_timestamp
# In the last line, we convert the timedelta into an integer representing the number of
# hours.
print round(delta.total_seconds() / 60.0 / 60.0)
(tz_from.localize(date) - tz_to.localize(date)).seconds/3600.0
Where tz_from and tz_to are the starting and ending timezones. You must specify a particular date.
from datetime import datetime
from zoneinfo import ZoneInfo
dt = datetime.now() # 2020-09-13
tz0, tz1 = "Europe/Berlin", "US/Eastern" # +2 vs. -4 hours rel. to UTC
utcoff0, utcoff1 = dt.astimezone(ZoneInfo(tz0)).utcoffset(), dt.astimezone(ZoneInfo(tz1)).utcoffset()
print(f"hours offset between {tz0} -> {tz1} timezones: {(utcoff1-utcoff0).total_seconds()/3600}")
>>> hours offset between Europe/Berlin -> US/Eastern timezones: -6.0
a way to do this with Python 3.9's standard library.

Getting Python to Print the Hour of Day

I am using the following code to get the time:
import time
time = time.asctime()
print(time)
I end up with the following result:
'Tue Feb 25 12:09:09 2014'
How can I get Python to print just the hour?
You can use datetime:
>>> import datetime as dt
>>> dt.datetime.now().hour
9
Or, rather than now() you can use today():
>>> dt.datetime.today().hour
9
Then insert into any string desired:
>>> print('The hour is {} o\'clock'.format(dt.datetime.today().hour))
The hour is 9 o'clock
Note that datetime.today() and datetime.now() are both using your computer's notion of local time zone (ie, a 'naive' datetime object).
If you want to use time zone info, it is not so trivial. You can either be on Python 3.2+ and use datetime.timezone or use the third party pytz. I am assuming your computer's timezone is fine, and a naive (non time zone datetime object) is fairly easy to use.
import time
print (time.strftime("%H"))
time.asctime() will create a string, so extracting the hours part is hard. Instead, get a proper time.struct_time object, which exposes the components directly:
t = time.localtime() # gives you an actual struct_time object
h = t.tm_hour # gives you the hour part as an integer
print(h)
You can do it in one step if that's all you need the hour for:
print(time.localtime().tm_hour)

Categories

Resources