How to search in local time with a Python Script? - python

I have a script with TwitterSearch using different variables, so that I can convert the retrieved tweets in local time.
However, when I try to add the set_until condition, it begins to search at 8pm instead of midnight (the difference between UTC and my timezone). Can I force it to begin 4 hours later?
Here are the relevant portions of my code :
from time import strftime
from email.utils import parsedate, parsedate_tz, mktime_tz
from datetime import datetime, date
import pytz
The timestamp conversion
timestamp = mktime_tz(parsedate_tz(tweet_time_string))
return datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
The search criteria
tso.set_until(date(2015,04,18))
Thank you in advance!

Related

How to get EST timezone in Python

I would like to get the EST time with Python. I have the following code:
import datetime
from pytz import timezone
import time
now_EST = datetime.datetime.today().astimezone(timezone('EST'))
print(now_EST)
And the output is:
2022-03-29 09:52:55.130992-05:00
But when I google the EST time zone, I find out that the time right now is 10:52 am EST, which essentially is the right time.
Why does my code show the 1 hour earlier time compared to the correct one?
use a proper IANA time zone name to avoid ambiguities of the abbreviations.
from datetime import datetime
import pytz
print(datetime.now(pytz.timezone("America/New_York")))
# 2022-03-29 11:20:30.917144-04:00
If you happen to use Python 3.9 or higher, use the built-in zoneinfo module to set the time zone (pytz is deprecated):
from datetime import datetime
from zoneinfo import ZoneInfo
print(datetime.now(ZoneInfo("America/New_York")))
# 2022-03-29 11:20:30.917144-04:00
Daylight Saving Time. Try "EST5EDT"

AWS Glue job, Python script for datetime.today().strftime(%y/%m/%d) getting UTC date & time?

AWS Glue job, Python script for datetime.today().strftime(%y/%m/%d) getting UTC date & time NOT USA date & time. Basically tomorrow date & time, if you run above function.
How to get US date & time?
If you want the current time in UTC with your given format %y/%m/%d, you can use the following:
from datetime import datetime
time_now_utc = datetime.utcnow().strftime("%y/%m/%d")
If you want the current time at a different timezone, you can use pytz lib, and do the following:
import pytz
tz = pytz.timezone('America/New_York')
time_now_ny = datetime.now(tz).strftime("%y/%m/%d")
Since USA has multiple timezones, select the specific region you want.
To list all the regions in pytz, run the following:
>>> import pytz
>>> pytz.all_timezones
['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', ...]

How to get time in specific timezone in telethon event handler

I am using the telethon library to making telegram bots.
when I using event.date it was printing the time on +00.00 time zone.
How can I print time on specific timezone
See python - Convert UTC datetime string to local datetime. Stealing the top answer which uses python-dateutil:
from dateutil import tz
local_datetime = event.date.astimezone(tz.tzlocal())
from datetime import datetime
print(datetime.now().isoformat(timespec='minutes'))
have a look in datetime library

What is the easiest way to get current GMT time in Unix timestamp format?

Python provides different packages (datetime, time, calendar) as can be seen here in order to deal with time. I made a big mistake by using the following to get current GMT time time.mktime(datetime.datetime.utcnow().timetuple())
What is a simple way to get current GMT time in Unix timestamp?
I would use time.time() to get a timestamp in seconds since the epoch.
import time
time.time()
Output:
1369550494.884832
For the standard CPython implementation on most platforms this will return a UTC value.
import time
int(time.time())
Output:
1521462189
Does this help?
from datetime import datetime
import calendar
d = datetime.utcnow()
unixtime = calendar.timegm(d.utctimetuple())
print unixtime
How to convert Python UTC datetime object to UNIX timestamp
python2 and python3
it is good to use time module
import time
int(time.time())
1573708436
you can also use datetime module, but when you use strftime('%s'), but strftime convert time to your local time!
python2
from datetime import datetime
datetime.utcnow().strftime('%s')
python3
from datetime import datetime
datetime.utcnow().timestamp()
Python 3 seconds with microsecond decimal resolution:
from datetime import datetime
print(datetime.now().timestamp())
Python 3 integer seconds:
print(int(datetime.now().timestamp()))
WARNING on datetime.utcnow().timestamp()!
datetime.utcnow() is a non-timezone aware object. See reference: https://docs.python.org/3/library/datetime.html#aware-and-naive-objects
For something like 1am UTC:
from datetime import timezone
print(datetime(1970,1,1,1,0,tzinfo=timezone.utc).timestamp())
or
print(datetime.fromisoformat('1970-01-01T01:00:00+00:00').timestamp())
if you remove the tzinfo=timezone.utc or +00:00, you'll get results dependent on your current local time. Ex: 1am on Jan 1st 1970 in your current timezone - which could be legitimate - for example, if you want the timestamp of the instant when you were born, you should use the timezone you were born in. However, the timestamp from datetime.utcnow().timestamp() is neither the current instant in local time nor UTC. For example, I'm in GMT-7:00 right now, and datetime.utcnow().timestamp() gives a timestamp from 7 hours in the future!
Or just simply using the datetime standard module
In [2]: from datetime import timezone, datetime
...: int(datetime.now(tz=timezone.utc).timestamp() * 1000)
...:
Out[2]: 1514901741720
You can truncate or multiply depending on the resolution you want. This example is outputting millis.
If you want a proper Unix timestamp (in seconds) remove the * 1000
At least in python3, this works:
>>> datetime.strftime(datetime.utcnow(), "%s")
'1587503279'
I like this method:
import datetime, time
dts = datetime.datetime.utcnow()
epochtime = round(time.mktime(dts.timetuple()) + dts.microsecond/1e6)
The other methods posted here are either not guaranteed to give you UTC on all platforms or only report whole seconds. If you want full resolution, this works, to the micro-second.
from datetime import datetime as dt
dt.utcnow().strftime("%s")
Output:
1544524990
#First Example:
from datetime import datetime, timezone
timstamp1 =int(datetime.now(tz=timezone.utc).timestamp() * 1000)
print(timstamp1)
Output: 1572878043380
#second example:
import time
timstamp2 =int(time.time())
print(timstamp2)
Output: 1572878043
Here, we can see the first example gives more accurate time than second one.
Here I am using the first one.

Django - String to Date - Date to UNIX Timestamp

I need to convert a date from a string (entered into a url) in the form of 12/09/2008-12:40:49. Obviously, I'll need a UNIX Timestamp at the end of it, but before I get that I need the Date object first.
How do I do this? I can't find any resources that show the date in that format? Thank you.
You need the strptime method. If you're on Python 2.5 or higher, this is a method on datetime, otherwise you have to use a combination of the time and datetime modules to achieve this.
Python 2.5 up:
from datetime import datetime
dt = datetime.strptime(s, "%d/%m/%Y-%H:%M:%S")
below 2.5:
from datetime import datetime
from time import strptime
dt = datetime(*strptime(s, "%d/%m/%Y-%H:%M:%S")[0:6])
You can use the time.strptime() method to parse a date string. This will return a time_struct that you can pass to time.mktime() (when the string represents a local time) or calendar.timegm() (when the string is a UTC time) to get the number of seconds since the epoch.

Categories