How to supress warning: Datetime with no tzinfo will be considered UTC? - python-3.x

I have the following code which am using to monitor Azure ADF pipeline runs. The code uses 'RunFilterParameters' to apply a date range filter in extracting run results:
filter_params = RunFilterParameters(last_updated_after=datetime.now() - timedelta(1), last_updated_before=datetime.now() + timedelta(1))
query_response = adf_client.activity_runs.query_by_pipeline_run(resource_group, adf_name, row.latest_runid,filter_params)
The above works ok, however it is throwing a warning:
Datetime with no tzinfo will be considered UTC
Not sure how to add timezone to this or just suppress the warning?
Please help.

"no tzinfo" means that naive datetime is used, i.e. datetime with no defined time zone. Since Python assumes local time by default for naive datetime, this can cause unexpected behavior.
In the code example given in the question, you can create an aware datetime object (with tz specified to be UTC) like
from datetime import datetime, timezone
# and use
datetime.now(timezone.utc)
If you need to use another time zone than UTC, have a look at zoneinfo (Python 3.9+ standard library).

Related

Python Datetime Timezone Shift

I have a time string disseminated from a MQTT broker that I would like to read and convert from its native timezone (U.S. Central Time) to Coordinated Universal Time (UTC). I am currently using Python 3.8.5 in Ubuntu 20.04 Focal Fossa, with the machine timezone set to UTC.
The time string is as follows: 1636039288.815212
To work with this time in Python, I am using a combination of the datetime and pytz libraries. My current core of code is as follows:
from datetime import datetime, timedelta
import pytz
input = 1636039288.815212
srctime = datetime.fromtimestamp(input, tz=pytz.timezone('US/Central'))
After running this chunk, I receive the following undesired time output:
datetime.datetime(2021, 11, 4, 10, 21, 28, 815212, tzinfo=<DstTzInfo 'US/Central' CDT-1 day, 19:00:00 DST>)
It appears that despite explicitly defining 'US/Central' inside the initial timestamp conversion, 5 hours were subsequently subtracted the initial time provided.
What additional steps/alterations can I make to ensure that the initial time provided is unchanged, defined as US/Central, and that I can subsequently change to UTC?
Python's fromtimestamp assumes that your input is UNIX time, which should refer to 1970-01-01 UTC, not some arbitrary time zone. If you encounter such a thing nevertheless (another epoch time zone), you'll need to set UTC and then replace the tzinfo:
from datetime import datetime
from dateutil import tz # pip install python-dateutil
ts = 1636039288.815212
dt = datetime.fromtimestamp(ts, tz=tz.UTC).replace(tzinfo=tz.gettz("US/Central"))
print(dt)
# 2021-11-04 16:21:28.815212-05:00
# or in UTC:
print(dt.astimezone(tz.UTC))
# 2021-11-04 20:21:28.815212+00:00
Note that I'm using dateutil here so that the replace operation is safe. Don't do that with pytz (you must use localize there). Once you upgrade to Python 3.9 or higher, use zoneinfo instead, so you only need the standard library.

datetime.now() returns utc-5:00 value when running from django application but returns correct system datetime when running from terminal in python

I need to set a value to the system datetime in my code.
In settings.py in the django application, I have commented TIME_ZONE and USE_TZ= True.
And in my file the code looks like:
import datetime
checkedin_dt = datetime.datetime.now()
logger.info(checkedin_dt) # -- gives me a value that is utc-5:00
However, when I run the same thing in the python terminal, it gives me the correct systemdatetime.
I want my code to fetch the systemdatetime without setting any specific timezone in settings.py
Any idea why this isn't working in the code. The db is sqlite and the application is a django application.
Set USE_TZ = True and use timezone():
from django.utils import timezone
checkedin_dt = timezone.now()
Documentation: https://docs.djangoproject.com/en/3.0/topics/i18n/timezones/
Django doesn't include a way to determine the system time zone, but you can use the tzlocal package for that.
I've never tried it myself, but I think you could get what you want by doing the following in your settings file:
from tzlocal import get_localzone
TIME_ZONE = get_localzone().zone
It could still fail if the name of the timezone can't be determined, since TIME_ZONE must be a string name.

Changing datetime format in py

I have the following code which gives the following output:
print(df1['Diff'].mean())
outputs:
10 days 16:13:29.467455
But since i just want the days value and not the time, i have done this:
print(datetime.strptime(df1['Diff'].mean(), format ='%d')
but i am getting the following error:
^
SyntaxError: unexpected EOF while parsing
Why am i getting this error?
For date, time, and datetime objects
You should be using strftime to format the time, not to parse the time (as in strptime).
print(obj.strftime('%d'))
strptime expects a string to be passed in (and you were passing in a datetime object), whereas strftime formats an existing datetime object.
For timedelta objects
print(obj.days)
This gets the days counterpart you're looking for.
I think the instance of df1['Diff'].mean() is str and datetime.strptime() can be use only in datetime methods. So to only get date you have to take slice of df1['Diff'].mean() like df1['Diff'].mean()[:-14]
Which is in your case.

Python timezone conversion adding minutes to the hour?

So I'm trying to convert a bunch of hours (10:00:00, 14:00:00, etc) from a given timezone to UTC.
When I do so, I keep maddeningly getting things back like "15:51:00".
When you get to that line, and print what value it's using, it's using something like:
1900-01-01 12:00:00-05:51
Which is fine, except for the -05:51 bit. I have no idea why that -05:51 is there, and it's causing me problems. UTC conversion is hour to hour, yes? I think it's got something to do with my timezone conversions, but I really don't get why they would be doing that.
Here's a minimal example that has the same erroneous output; it returns 15:51:00 when it should just return a flat hour, no minutes.
import datetime
from dateutil import tz
jj = datetime.datetime.strptime("10:00:00", "%H:%M:%S")
tzz = tz.gettz('US/Central')
def constructstring(tzz,x):
inter2 = x.replace(tzinfo=tzz) #ERROR HAPPENS HERE (I'm pretty sure anyways)
inter3 = inter2.astimezone(tz.tzutc())
return inter3
print(constructstring(tzz,jj).strftime("%H:%M:%S"))
You are not specifying a date when you create the jj datetime object, so the default date of 1900-01-01 is used. Timezones are not fixed entities; they change over time, and the US/Central timezone used a different offset back in 1900.
At the very least, use a recent date, like today for example:
# use today's date, with the time from jj, and a given timezone.
datetime.datetime.combine(datetime.date.today(), jj.time(), tzinfo=tzz)
If all you need is a time, then don't create datetime objects to store those; the datetime module has a dedicated time() object. I'd also not use strftime() to create objects from literals. Just use the constructor to pass in integers:
jj = datetime.time(10, 0, 0) # or just .time(10)
Other good rules of thumb: If you have to deal with dates with timezones, try to move those to datetime objects in UTC the moment your code receives or loads them. If you only have a time of day, but still need timezone support, attach them to today's date, so you get the right timezone. Only convert to strings again as late as possible.

Transform datetime to timestamp python

I need your help.
I have parsed a web site, and I have harvested this:
2018-08-18T23:31:00
I searched, but couldn't find how to change datetime to timestamp
Example of my desired result :
1535414693077
The previous answer is incorrect as there is no '%s' formatter in datetime, even though it works, sometimes. See this answer for how wrong it is to use '%s'.
from datetime import datetime
import pytz
pytz.utc.localize(datetime.strptime('2018-08-18T23:31:00', '%Y-%m-%dT%H:%M:%S')).timestamp()
Output (float): 1534635060.0

Resources