Transform datetime to timestamp python - python-3.x

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

Related

How to supress warning: Datetime with no tzinfo will be considered UTC?

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).

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.

Given a datetime.time(X,Y) object, how to convert it to time string?

I am given the following object:
datetime.time(18, 50)
I want to convert to a string:
"18:50:00"
Please advise how can I do this?
Tried strftime but getting errors.
datetime.time(18, 50).strftime("%H:%M:%S")
You need to do:
from datetime import time
print(time(18, 50).strftime("%H:%M:%S"))
Importing the time function from datetime module seems to be different from datetime instance's time method.

How to parse specific date format with strptime?

I am using python3 and I have a time data format like '01-FEB-17' and I need to parse it with datetime.strptime. Does anyone know which format is the correct for this? I tried '%d-%b-%Y', '%d-%m-%Y',... and none of them worked.
Thanks
You can use this
from datetime import datetime
t = datetime.strptime('01-FEB-17', '%d-%b-%y')
NOTE: You can head here for a full list of format specifiers to use

Time conversion so I can insert data in mysql

Excuse my lack of knowledge playing with time in python3 but I am stuck for 30 minutes already in this problem so any help will be highly appreciated.
I have salesforce time coming in my script as 2016-11-15T23:49:48.000Z. How can I convert it into mysql datetime format without storing it in a variable.
Desired output format: 2016-11-16 12:11:21.525885
You can use the time module's strptime referenced here.
I'm making some assumptions about the input data, but here's a format that will probably work:
import time
import datetime
def translate_timestamp(timestamp):
t = time.strptime(x, "%Y-%m-%dT%H:%M:%S.%fZ")
s = datetime.datetime.fromtimestamp(time.mktime(t))
return s.strftime("%Y-%m-%d %H:%M:%S.%f")
An example might be:
>>> translate_timestamp("2016-11-15T23:49:48.000Z")
'2016-11-15 23:49:48.000000'

Resources