web scraping convert unix timestamp to date format - python

I'm trying to webscrape a flight data website using beautifulsoup in python but the timestamp is in unix timestamp how can i convert to regular datetime format. There are several such columns to be converted.
#scheduled_departure
result_items[0]['flight']['time']['scheduled']['departure']
and the output is shown as 1655781000. how can I convert it to Tue, Jun 21, 2022 8:40 AM

import time
print(time.strftime("%a, %b %d, %Y %H:%M %p", time.localtime(1655781000)))

There is only one Unix time and it is created by using the UTC/GMT time zone. This means you might have convert time zones to calculate timestamps.
import datetime
from pytz import timezone
local_datetime = datetime.datetime.fromtimestamp(1655781000)
local_time_str = datetime.datetime.strftime(local_datetime, "%a, %d %b %Y %H:%M:%S %p")
print(f'Local time: {local_time_str}')
other_timezone = 'Asia/Kolkata' # Replace your interest timezone here
remote_datetime = local_datetime.astimezone(timezone(other_timezone))
remote_time_str = datetime.datetime.strftime(remote_datetime, "%a, %d %b %Y %H:%M:%S %p")
print(f'Time at {other_timezone }: {remote_time_str}')

Related

How can I convert datetime to unix timestamp in python

I have one date format "Mon, 15 Jun 2020 22:11:06 PT" I want to convert this format to unix timestamp.
I am using the following code ===>
news_date = datetime.strptime(news_date, '%a, %d %b %Y %H:%M:%S %Z')
news_date = calendar.timegm(news_date.utctimetuple())
But gives the following error ===>
ValueError: time data 'Mon, 15 Jun 2020 22:11:06 PT' does not match format '%a, %d %b %Y %H:%M:%S %Z'
How can i solve it and get the unix timestamp from this?
%Z can't parse the timezone name PT - I suggest you skip parsing it and add it "manually" instead:
from datetime import datetime
import dateutil
news_date = "Mon, 15 Jun 2020 22:11:06 PT"
# parse string without the timezone:
news_date = datetime.strptime(news_date[:-3], '%a, %d %b %Y %H:%M:%S')
# add the timezone:
news_date = news_date.replace(tzinfo=dateutil.tz.gettz('US/Pacific'))
# extract POSIX (seconds since epoch):
news_date_posix = news_date.timestamp()
# 1592284266.0
if you have multiple strings with different timezones, you could use a dict to map the abbreviations to time zone names, e.g.
tzmapping = {'PT': 'US/Pacific'}
news_date = "Mon, 15 Jun 2020 22:11:06 PT"
# get appropriate timezone from string, according to tzmapping:
tz = dateutil.tz.gettz(tzmapping[news_date.split(' ')[-1]])
# parse string and add timezone:
news_date_datetime = datetime.strptime(news_date[:-3], '%a, %d %b %Y %H:%M:%S')
news_date_datetime = news_date_datetime.replace(tzinfo=tz)

Python strptime not parsing time zone EST %z

I am trying to parse the string '10/23/2019 6:02:05 PM EST' into a datetime with time zone using Python 3.7.
Code:
from datetime import datetime
timestamp = datetime.strptime(date_str, '%m/%d/%Y %I:%M:%S %p %Z')
Error:
ValueError: time data '10/23/2019 6:02:05 PM EST' does not match format '%m/%d/%Y %I:%M:%S %p %Z'
When I create a datetime and output it using the same formatting I get the correct output. The only difference is that there is a 0 in front of the hour, but adding 0 in front of the 6 in my date string results in the same error.
My current solution is to parse the datetime without the timezone and then localize it, but this is not ideal.
date_lst = date.split()
date_str = ' '.join(date_lst[0:3])
timestamp = datetime.strptime(date_str, '%m/%d/%Y %I:%M:%S %p')
new_tz = pytz.timezone(date_lst[3])
timestamp_tz = new_tz.localize(timestamp)```
It is reasonable to expect that parsing a string with a timezone included would produce a timezone aware datetime object.
Try it
>>timestamp = datetime.strptime('10/23/2019 6:02:05 PM EST', '%m/%d/%Y %I:%M:%S %p EST')
>>2019-10-23 06:02:05
You can try this.

How to create date timestamp logs like git log

Using datetime.datetime.now(), I receive some badly formatted timestamps.
Is there an intuitive way of creating a date timestamp in this format?
Wed Aug 7 13:38:59 2019 -0500
This is seen in git log.
You can use datetime.datetime.strftime() to format dates as shown below:
from datetime import datetime
d = '2019-08-07 13:38:59-0500'
d2 = datetime.strptime(d, '%Y-%m-%d %H:%M:%S%z')
d3 = d2.strftime('%a %b %d %H:%M:%S %Y %z')
print(d3)
This returns:
Wed Aug 07 13:38:59 2019 -050000
This website is a great resource for strftime formatting.
You can still use the datetime library. Using strftime, you can rewrite the datetime object into a nicely formatted string.
In your case, you are going for Wed Aug 7 13:38:59 2019 -0500, which translated to strftime formatting is "%a %b %d %H:%M:%S %Y %z".
Overall, it'd be
datetime.datetime.now().strftime("%a %b %d %H:%M:%S %Y %z")
Which will give a string that looks like 'Wed Aug 7 13:38:59 2019 -0500'.
I would do the following:
from time import gmtime, strftime
if __name__ == "__main__":
time = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
print(time)
This was found on the documentation page of the time module. There are also a lot of additional features you might be interested in using outlined here: https://docs.python.org/3/library/time.html#time.strftime

Convert datetime in python

How do i convert this datetime using python?
2017-10-16T08:27:16+0000
I tried to use strptime but getting ValueError: time data '2017-10-16T08:27:16+0000' does not match format 'The %d %B %Y at. %H:%M'
import datetime
datetime.datetime.strptime("2017-10-16T08:27:16+0000", "The %d %B %Y at. %H:%M")
'''
I want my output to look like this
The 16 october 2017 at. 08:27
'''
First parse the string correctly, then print it in the desired format:
import datetime
date = datetime.datetime.strptime("2017-10-16T08:27:16+0000", "%Y-%m-%dT%H:%M:%S%z")
print(date.strftime("The %d %B %Y at. %H:%M"))
https://blogs.harvard.edu/rprasad/2011/09/21/python-string-to-a-datetime-object/
You have to first strip your date using strptime() and then rebuild it using strftime()
import datetime
time = "2017-10-16T08:27:16+0000"
stripedTime = datetime.datetime.strptime(time, '%Y-%m-%dT%I:%M:%S%z')
rebuildTime = stripedTime.strftime('The %d %B %Y at. %H:%M')
print(rebuildTime)

Python 3 time: difference in seconds between two timestamps

I have two timestamp strings. I want to find the difference between them in seconds.
I've tried:
from time import gmtime, strptime
a = "Mon 11 Dec 2017 13:54:36 -0700"
b = "Mon 11 Dec 2017 13:54:36 -0000"
time1 = strptime(a, "%a %d %b %Y %H:%M:%S %z")
time2 = strptime(b, "%a %d %b %Y %H:%M:%S %z")
time1-time2
Getting an error: TypeError: unsupported operand type(s) for -: 'time.struct_time' and 'time.struct_time'
So, how do I calculate the difference using package time?
I was successful using package datetime - in the code below, but I think I read that datetime ignores seconds in leap years, or something to that effect. Thus, I am trying to use 'time':
from datetime import datetime
time1 = datetime.strptime(a, "%a %d %b %Y %H:%M:%S %z")
time2 = datetime.strptime(b, "%a %d %b %Y %H:%M:%S %z")
dif = time1 - time2
print(int(dif.total_seconds()))
Thank you very much!
First of all, you're using time.strptime, which returns <class 'time.struct_time'>, and it doesn't support the substract operator, one possible way to achieve what you want would be converting to datetime:
from datetime import datetime
from time import mktime
from time import gmtime, strptime
a = "Mon 11 Dec 2017 13:54:36 -0700"
b = "Mon 11 Dec 2017 13:54:36 -0000"
time1 = strptime(a, "%a %d %b %Y %H:%M:%S %z")
time2 = strptime(b, "%a %d %b %Y %H:%M:%S %z")
print(datetime.fromtimestamp(mktime(time1))-datetime.fromtimestamp(mktime(time2)))
Or even better, use datetime.datetime.strptime so you don't need intermediate conversions.
For a more detailed description of the supported operations of datetime please refer to the section supported operations in the docs here. Especially the section where it says:
If both are aware and have different tzinfo attributes, a-b acts as if
a and b were first converted to naive UTC datetimes first. The result
is (a.replace(tzinfo=None) - a.utcoffset()) - (b.replace(tzinfo=None)
- b.utcoffset()) except that the implementation never overflows.
In any case, maybe your best chance is considering an alternative method like the one proposed in this answer

Categories

Resources