get time difference in seconds for time referring to another timezone - python

I intend to find the time difference between two time variables in seconds. The issue here is that I am referring to time in a different zone. I have managed to find a solution, but it is a mix of pandas.datetime function and python datetime library. I guess, the objective can be achieved with just pandas/numpy alone and with fewer lines of code. below is my code, appreciate any guidance on how can i achieve the final_output more efficiently.
import pandas as pd
from datetime import timedelta
local_time = pd.to_datetime('now').tz_localize('UTC').tz_convert('Asia/Dubai')
t1 = timedelta(hours=local_time.now('Asia/Dubai').hour, minutes=local_time.now('Asia/Dubai').minute)
t2 = timedelta(hours=9, minutes=14)
final_output = (t2 - t1).seconds

You may want to convert both times to UTC, then find the difference. Programmers usually like to work with UTC until the time reaches the front end.

Related

Convert from numpy.timedelta64 to time interval

I'm having a hard time converting numpy.timedelta64, which is directional, into a time span, which is not directional. I don't see any instructions in the docs
The .abs function does not seem to work on timedelta values
Use np.abs(x) or abs(x), both work fine.

datetime.timestamp() loses an time (an hour)

I encountered this issue but found the solution after a bit of research. I have posted my answer below to show my findings. If anyone has alternative suggestions please post them.
I needed to convert a datetime.datime object to a Unix timestamp. I tried using the datetime.timestamp. I found the result was 1 hour behind what I expected. I was able to replicate this issue with the following.
from datetime import datetime, timestamp
dt = datetime.utcfromtimestamp(1438956602.0)
dt now equals datetime.datetime(2015, 8, 7, 14, 10, 2)
Then:
dt_stamp = datetime.timestamp(dt)
Which gives dt_stamp = 1438953002.0 (which is different from our original timestamp). If we convert it back to datetime
datetime.utcfromtimestamp(dt_stamp)
We get:
datetime.datetime(2015, 8, 7, 13, 10, 2)
Which is an hour earlier than our original time.
For context I am using Python 3 and based in Britain where we're currently using British summer time (1 hour ahead of UTC).
My solution can be found below. If you think I have missed anything from my explanation or there's a better solution, please post your own answer.
I met the same problem recently, my case is that part EDF recording from one hostipal in UK have one hour bias, which is considered due to British summer time.
Following is the solution to my case.
from datetime import datetime as dt
Please use
dt = dt.utcfromtimestamp(#YOUR_TIME_STAMP)
INSTEAD of
dt = dt.fromtimestamp(#YOUR_TIME_STAMP)
The cause for this difference is actually shown in the
datetime.timestamp documentation.
Naive datetime instances are assumed to represent local time and this method relies on the platform C mktime() function to perform the conversion. Since datetime supports wider range of values than mktime() on many platforms, this method may raise OverflowError for times far in the past or far in the future.
Because I am in UTC+1 (during British summer time) this is the timezone datetime.timestamp uses to calculate the timestamp. This is where the mistake comes in. The documentation recommends a few ways to deal with this. I went with the following.
from datetime import datetime, timestamp
dt = datetime.utcfromtimestamp(1438956602.0)
dt_stamp = datetime.timestamp(dt.replace(tzinfo=timezone.utc))
By adding .replace(tzinfo=timezone.utc) to the end of dt it specifies that this is done in the UTC timezone. datetime.timestamp then knows to use the UTC time rather than whatever timezone my machine is running.
People in America or other parts of the world will encounter this issue if not using the UTC timezone. If this is the case you can set tzinfo to whatever your timezone is. Also note that datetime.utcfromtimestamp is also clearly designed for people using the UTC timezone.
I think you need a so called aware datetime object. Aware means it nows the time difference you have:
datetime.fromtimestamp(timestamp, timezone(timedelta(hours=1)))
Try it with that line of code, where timestamp is your Unix timestamp.

Is Pandas.tslib.normalize_date() timezone aware?

I found the following behaviour at normalizing Timestamps at daylight saving time change boundaries at pandas 0.16.2:
import pandas as pd
original_midnight = pd.Timestamp('20121104', tz='US/Eastern')
original_midday = pd.Timestamp('20121104T120000', tz='US/Eastern')
str(pd.tslib.normalize_date(original_midday))
`Out[10]:'2012-11-04 00:00:00-05:00'`
str(original_midnight)
`Out[12]:'2012-11-04 00:00:00-04:00'`
I believe the normalized Timestamp should have the same timezone than the original_midnight.
Is it a bug, or do I miss something?
The implementation simply truncates the time. It does not appear to manipulate the offset at all, so I will say no, it is not timezone aware.
Also, consider that this type of operation (in many languages) tends to gloss over the fact that not every local day has a midnight. For example, if the time zone is 'America/Sao_Paulo' (Brazil), and the date is on the spring-forward transition (such as 2015-10-18), the hour from 00:00 to 00:59 is skipped, meaning the start of the day is actually 01:00. If the function were to be updated to be timezone aware, it would have to adjust the time as well as the offset.

how to make time.mktime work consistently with datetime.fromtimestamp?

I am expecting the following code will return 0 but I get -3600, could someone explains why? and how to fix it? thanks
import datetime
import time
ts = time.mktime(time.gmtime(0))
print time.mktime(datetime.datetime.fromtimestamp(ts).timetuple())
time.mktime converts a time tuple in local time to seconds since the Epoch. Since time.gmtime(0) returns GMT time tuple, and the conversion assumes it was in your local time, you see this discrepancy.

Compare if datetime.timedelta is between two values

I have a datetime.timedelta time object in python (e.g. 00:02:00) I want to check if this time is less than 5 minutess and greater then 1 minute.
I'm not sure how to construct a timedelta object and I'm also not sure if this is the right format to compare times. Would anyone know the most efficient way to do this?
So if you start with a string that's rigorously and precisely in the format 'HH:MM:SS', timedelta doesn't directly offer a string-parsing function, but it's not hard to make one:
import datetime
def parsedelta(hhmmss):
h, m, s = hhmmss.split(':')
return datetime.timedelta(hours=int(h), minutes=int(m), seconds=int(s))
If you need to parse many different variants you'll be better off looking for third-party packages like dateutil.
Once you do have timedelta instance, the check you request is easy, e.g:
onemin = datetime.timedelta(minutes=1)
fivemin = datetime.timedelta(minutes=5)
if onemin < parsedelta('00:02:00') < fivemin:
print('yep')
will, as expected, display yep.

Categories