Convert tweet creation time to UTC - python

Building a Twitter scraper I'm stuck at converting tweet creation datetime (I get it as local timezone) into UTC.
Creation date from data-original-title -attribute's format is 12:17 AM - 8 Apr 2018. How can I convert it to UTC?

First of all you need to convert your string into a python datetime format then i recommend you using pytz module to change the timezone used into UTC timezone like this example:
import datetime
import pytz
a = '12:17 AM - 8 Apr 2018'
final = datetime.datetime.strptime(a, '%I:%M %p - %d %b %Y').replace(tzinfo=pytz.UTC)
print(final)
# 2018-04-08 00:17:00+00:00
Also, if you want to check the converted time into a string representation, you can do :
str_time = final.strftime('%d/%m/%Y %H:%M:%S')
print(str_time)
# '08/04/2018 00:17:00'
Ps: If you don't have pytz module installed in your PC, you can install it by :
sudo pip install pytz

Try Below:
import pandas as pd
datestr = '12:17 AM - 8 Apr 2018'
utcDate = pd.to_datetime(datestr, format='%H:%M %p - %d %b %Y', utc=True)

Related

Parse timestamp from a txt file?

I have a large text file on the web that I am using requests to obtain and parse data from. The text file begins each line with a format like [Mon Oct 10 08:58:26 2022]. How can I get the latest 7 days or convert only the datetime to an object or string for storing and parsing later? I simply want to extract the timestamps from the log and print them
You can use TimedRotatingFileHandler for daily or 7-days logs.
read more about timed rotating file handler here
and
read more about extracting timestamps from files
Can you tell me if this snippet solves your problem?
from datetime import datetime
log_line = "[Sun Oct 09 06:14:26 2022] Wiladoc is browsing your wares."
_datetime = log_line[1:25]
_datetime_strp = datetime.strptime(_datetime, '%a %b %d %H:%M:%S %Y')
print(_datetime)
print(_datetime_strp)

localize date time in python

Overview I receive the timestamp from server_x, my application is on server_y, both are in different regions, my application calls server_x api and receives json which has timestamp, now i need to perform some calculation on server_y, for that i need to make sure that the timestamp i receive from server_x could be used to covert the local datetime of server_y , so both are in sync
I want to convert datetime.now() to the timezone I receive from server for e.g., UTC-07:00
Current solution, I pass server_timestamp to the function and then I pass its zone info to the datetime.now
Server_timestamp = "2020-04-04T10:24:49.000-0700"
dt = datetime.strptime(Server_timestamp, "%Y-%m-%dT%H:%M:%S.%f%z")
convert_local = datetime.now(dt.tzinfo)
Problem:
I need to save the timezone of the server in db and then use that instead of passing server_timestamp everytime, the tzinfo gives a type datetime.timezone = UTC-07:00, after storing this string how can I use it to change the localtime.
Here's a function that utilizes the datetime library to convert a datetime object from one timezone to another:
from datetime import datetime
import pytz
def convert_tz(dt, current_tz, out_tz):
return dt.replace(tzinfo=current_tz).astimezone(tz=out_tz)
now = datetime.now(tz=pytz.timezone('US/Eastern'))
convert = datetime.now().astimezone().tzinfo
print(now)
print(utc_to_local(now, now.tzinfo, convert))
Output:
2020-05-10 17:02:44.245703-04:00
2020-05-10 16:02:44.245703-05:00
I used the pytz library for demonstration purposes. For you, to get the server's timezone, use the line datetime.now().astimezone().tzinfo.
I implemented a solution.
I now save the last 5 char of the timestamp in the db "-0700".
time_zone = query_from_db
tz = datetime.strptime(time_zone, "%z")
datetime_now = datetime.now(tz.tzinfo)

Python datetime does not match format

I went through datetime python page, and other related pages, but unable to get this thing to work.
I have the following string that I want to convert to python date object.
May 29, 2018 10:40:06 CDT AM:
I use the following to match, but python2.7 is giving me doesnt match error.
datetime_object = datetime.strptime(line, '%B %d, %Y %I:%M:%S %Z %p:')
It seems as though CDT is not a valid timezone name as it works with GMT.
>>> str(datetime.strptime('May 29, 2018 10:40:06 GMT AM:', '%B %d, %Y %I:%M:%S %Z %p:'))
'2018-05-29 10:40:06'
from dateutil import parser
print (parser.parse("May 29 13:40:06 CDT 2018"))
Output
2018-05-29 13:40:06
Reference: python-dateutil

Dealing with timezone dates in MongoDB and pymongo

I do not seem to be able to query records and get what I expect. For example I am searching
today = datetime.datetime.today()
past = today + timedelta(days=-200)
results = mongo.stuff.find({"date_added": {"gt": past}}, {"id":1})
I have the following date specified in MongoDB:
"date_added": {
"$date": "2016-04-19T18:47:54.101Z"
},
But I get no results! Is this down to the timezone which appears in the MongoDB date which is screwing things up.
This is just a typing error:
Try with the following code:
results = mongo.stuff.find({"date_added": {"$gt": past}}, {"id":1})
You forgot about the $ sign in $gt.
Use an aware datetime object (with timezone info).
# E.g. with UTC timezone :
import pytz
import datetime
today = datetime.datetime.today()
past = today + timedelta(days=-200)
pytc.utc.localize(past)
results = mongo.stuff.find({"date_added": {"gt": past}}, {"id":1})
To use a different timezone to localize, try something like pytz.timezone('US/Mountain')
P.S. you'll need pip install pytz

Django Date Input Parsing?

I'm trying to get a date for an event from a user.
The input is just a simple html text input.
My main problem is that I don't know how to parse the date.
If I try to pass the raw string, I get a TypeError, as expected.
Does Django have any date-parsing modules?
If you are using django.forms look at DateField.input_formats. This argument allows to define several date formats. DateField tries to parse raw data according to those formats in order.
Django doesn't, so to speak, by Python does. It seems I'm wrong here, as uptimebox's answer shows.
Say you're parsing this string: 'Wed Apr 21 19:29:07 +0000 2010' (This is from Twitter's JSON API)
You'd parse it into a datetime object like this:
import datetime
JSON_time = 'Wed Apr 21 19:29:07 +0000 2010'
my_time = datetime.datetime.strptime(JSON_time, '%a %b %d %H:%M:%S +0000 %Y')
print type(my_time)
You'd get this, confirming it is a datetime object:
<type 'datetime.datetime'>
More information on strptime() can be found here.
(In 2017), you could now use django.utils.dateparse
The DateField can be used outside of Django forms.
Example, when used {{ value|date:"SHORT_DATE_FORMAT" }} in template:
from django.forms import DateField
from django.utils import formats
# need '%d.%m.%Y' instead of 'd.m.Y' from get_format()
dformat = ('.' + formats.get_format("SHORT_DATE_FORMAT", lang=request.LANGUAGE_CODE)).replace('.', '.%').replace('-', '-%').replace('/', '/%')[1:]
dfield = DateField(input_formats=(dformat,))
<date> = dfield.to_python(<string>)

Categories

Resources