Unix timestamp to iso 8601 time format - python

When I convert unix time 1463288494 to isoformat i get 2016-05-14T22:01:34. How can I get the output including the -07:00. In this format 2016-05-14T22:01:34-07:00
from datetime import datetime
t = int("1463288494")
print(datetime.fromtimestamp(t).isoformat())

You can pass a tzinfo instance representing your timezone offset to fromtimestamp(). The problem then is how to get the tzinfo object. The easiest way is to use the pytz module which provides a tzinfo compatible object:
import pytz
from datetime import datetime
tz = pytz.timezone('America/Los_Angeles')
print(datetime.fromtimestamp(1463288494, tz).isoformat())
#2016-05-14T22:01:34-07:00

Related

How to convert str time to timestamp?

I have a string "15:15:00"
I need to convert it to timestamp like 1410748201
Python
A UNIX timestamp always needs a relation between a date and a time to actually form it, since it is the number of seconds that are passed since 1970-01-01 00:00. Therefore I would recommend you to use the datetime module.
Let's assume you have given the following format: "2022-10-31 15:15:00".
With datetime you can convert the string into a datetime object by using the strptime() function.
Afterwards, a datetime object gives you the ability to convert your datetime into a UNIX timestamp with the timestamp() method of your datetime object.
from datetime import datetime
datetime_str = "2022-10-31 15:15:00"
datetime_obj = datetime.strptime(datetime_str, "%Y-%m-%d %H:%M:%S")
print(datetime_obj.timestamp())

How to get the UTC offset from a timezone string using pytz?

I am trying to get the UTC offset as a string from my timezone using the Python library pytz.
I am defining it as follows:
import pytz
tz = pytz.timezone('Africa/Cairo')
Now, I want to get '+02:00' from the tz variable, as that is the corresponding UTC offset for Africa/Cairo.
How can I do this? Thanks!
You can use datetime to get current date/time in given timezone and then extract UTC offset,
import pytz
import datetime
tz = pytz.timezone('Africa/Cairo')
print(datetime.datetime.now(tz).utcoffset().total_seconds()/3600)
# output,
2.0

Easy way to convert UTC to another timezone in python

I'm trying to convert a UNIX time stamp to UTC+9. I've been searching for hours and it's all very confusing what with the different libraries etc
Here's what I've got so far
from datetime import datetime
from pytz import timezone
import datetime
time = 1481079600
utc_time = datetime.datetime.fromtimestamp(time)#.strftime('%Y-%m-%d %H:%M:%S')
print utc_time.strftime(fmt)
tz = timezone('Japan')
print tz.localize(utc_time).strftime(fmt)
This just prints the same time, what am I doing wrong
I am going to shamelessly plug this new datetime library I am obsessed with, Pendulum.
pip install pendulum
import pendulum
t = 1481079600
pendulum.from_timestamp(t).to_datetime_string()
>>> '2016-12-07 03:00:00'
And now to change it to your timezone super quick and easy!
pendulum.from_timestamp(t, 'Asia/Tokyo').to_datetime_string()
>>> '2016-12-07 12:00:00'
Your utc_time datetime is naive - it has no timezone associated with it. localize assigns a timezone to it, it doesn't convert between timezones. The simplest way to do that is probably to construct a timezone-aware datetime:
import pytz
utc_time = datetime.datetime.fromtimestamp(time, pytz.utc)
Then convert to the timezone you want when you're ready to display it:
print utc_time.astimezone(tz).strftime(fmt)

pytz - Converting UTC and timezone to local time

I have a datetime in utc time zone, for example:
utc_time = datetime.datetime.utcnow()
And a pytz timezone object:
tz = timezone('America/St_Johns')
What is the proper way to convert utc_time to the given timezone?
I think I got it:
pytz.utc.localize(utc_time, is_dst=None).astimezone(tz)
This line first converts the naive (time zone unaware) utc_time datetime object to a datetime object that contains a timezone (UTC). Then it uses the astimezone function to adjust the time according to the requested time zone.
It's the exact purpose of fromutc function:
tz.fromutc(utc_time)
(astimezone function calls fromutc under the hood, but tries to convert to UTC first, which is unneeded in your case)
I agree with Tzach's answer. Just wanted to include that the is_dst parameter is not required:
pytz.utc.localize(datetime.utcnow()).astimezone(tz)
That code converts the current UTC time to a timezone aware current datetime.
Whereas the code below converts the current UTC time to a timezone aware datetime which is not necessarily current. The timezone is just appended into the UTC time value.
tz.localize(datetime.utcnow())
May I recommend to use arrow? If I understood the question:
>>> import arrow
>>> utc = arrow.utcnow()
>>> utc
<Arrow [2014-08-12T13:01:28.071624+00:00]>
>>> local = utc.to("America/St_Johns")
>>> local
<Arrow [2014-08-12T10:31:28.071624-02:30]>
You can also use
tz.fromutc(utc_time)
Another very easy way:
Because utcnow method returns a naive object, so you have to convert the naive object into aware object. Using replace method you can convert a naive object into aware object. Then you can use the astimezone method to create new datetime object in a different time zone.
from datetime import datetime
import pytz
utc_time = datetime.utcnow()
tz = pytz.timezone('America/St_Johns')
utc_time =utc_time.replace(tzinfo=pytz.UTC) #replace method
st_john_time=utc_time.astimezone(tz) #astimezone method
print(st_john_time)
You can also use the sample below, I use it for similar task
tz = pytz.timezone('America/St_Johns')
time_difference=tz.utcoffset(utc_time).total_seconds() #time difference between UTC and local timezones in 5:30:00 format
utc_time = date + timedelta(0,time_difference)
It works fast and you don't need to import additional libraries.

Facebook timestamp to Python DateTime Object

Facebook returns 'created_time' in this format:
2012-07-23T08:52:04+0000
I want to convert this timestamp to a normal Python DateTime object.
Have you tried dateutil
It's extremely easy to use
import dateutil.parser as dateparser
dateparser.parse('2012-07-23T08:52:04+0000')
dateutil is very helpful to deal with timezone info, and it can handle lots of time formats.
s = "2005-12-06T12:13:14"
from datetime import datetime
from time import strptime
print datetime(*strptime(s, "%Y-%m-%dT%H:%M:%S")[0:6])

Categories