Conversion of timezone of datetime.time - python

I am writing Python code using arrow library to convert timezone (which will be UTC by default) in the string (without date) to another timezone.
A sample string value is: 18:30+0000
The code snippet is :
start = '18:30+0000'
start_time = arrow.get(start,'hh:mmZ')
print(start_time.format('hh:mm A ZZ'))
print(start_time.to('Australia/Melbourne').format('hh:mm A ZZ'))
# This should print 05:30 AM +11:00 - but showing 04:09 AM +09:39
I have also tried to convert to other timezones as well.
print(start_time.to('Europe/London').format('hh:mm A ZZ'))
# This should print 06:30 PM +00:00 - but showing 06:28 PM -00:01
Getting UTC now and then converting it to different timezone working perfectly fine
print(arrow.utcnow().to('Australia/Melbourne').format('hh:mm A ZZ'))
SOLUTION:
We have to add temporary date value for conversion if we don't have date value.
def convert_timezone(time_to_convert,timezone):
time_to_convert='20111111 '+time_to_convert
return arrow.get(time_to_convert).to(timezone)
print(convert_timezone('18:30+0000','Australia/Melbourne').format('hh:mm A ZZ'))

add an appropriate date to the input string and this works as you expect:
import arrow
start = '2021-02-01 18:30+0000'
start_time = arrow.get(start)
print(start_time.to('Australia/Melbourne').format('hh:mm A ZZ'))
# 05:30 AM +11:00

Related

Split URL at - With Python

Does anyone know how I can extract the end 6 characters in a absoloute URL e.g
/es/ideas-de-trading-y-noticias/el-ibex-35-insiste-en-buscar-los-7900-puntos-a-la-espera-de-las--221104
This is not a typical URL sometimetimes it ends -221104
Also, is there a way to turn 221104 into the date 04 11 2022 easily?
Thanks in advance
Mark
You should use the datetime module for parsing strings into datetimes, like so.
from datetime import datetime
url = 'https://www.ig.com/es/ideas-de-trading-y-noticias/el-ibex-35-insiste-en-buscar-los-7900-puntos-a-la-espera-de-las--221104'
datetime_string = url.split('--')[1]
date = datetime.strptime(datetime_string, '%y%m%d')
print(f"{date.day} {date.month} {date.year}")
the %y%m%d text tells the strptime method that the string of '221104' is formatted in the way that the first two letters are the year, the next two are the month, and the final two are the day.
Here is a link to the documentation on using this method:
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
If the url always has this structure (that is it has the date at the end after a -- and only has -- once), you can get the date with:
str_date = str(url).split("--")[1]
Relaxing the assumption to have only one --, we can have the code working by just taking the last element of the splitted list (again assuming the date is always at the end):
str_date = str(url).split("--")[-1]
(Thanks to #The Myth for pointing that out)
To convert the obtained date into a datetime.date object and get it in the format you want:
from datetime import datetime
datetime_date = datetime.strptime(str_date, "%y%m%d")
formatted_date = datetime_date.strftime("%d %m %Y")
print(formatted_date) # 04 11 2022
Docs:
strftime
strptime
behaviour of the above two functions and format codes
Taking into consideration the date is constant in the format yy-mm-dd. You can split the URL by:
url = "https://www.ig.com/es/ideas-de-trading-y-noticias/el-ibex-35-insiste-en-buscar-los-7900-puntos-a-la-espera-de-las--221104"
time = url[-6:] # Gets last 6 values
To convert yy-mm-dd into dd mm yy we will use the DateTime module:
import datetime as dt
new_time = dt.datetime.strptime(time, '%y%m%d') # Converts your date into datetime using the format
format_time = dt.datetime.strftime(new_time, '%d-%m-%Y') # Format
print(format_time)
The whole code looks like this:
url = "https://www.ig.com/es/ideas-de-trading-y-noticias/el-ibex-35-insiste-en-buscar-los-7900-puntos-a-la-espera-de-las--221104"
time = url[-6:] # Gets last 6 values
import datetime as dt
new_time = dt.datetime.strptime(time, '%y%m%d') # Converts your date into datetime using the format
format_time = dt.datetime.strftime(new_time, '%d %m %Y') # Format
print(format_time)
Learn more about datetime
You can use python built-in split function.
date = url.split("--")[1]
It gives us 221104
then you can modify the string by rearranging it
date_string = f"{date[4:6]} {date[2:4]} {date[0:2]}"
this gives us 04 11 22
Assuming that -- will only be there as it is in the url you posted, you can do something as follows:
You can split the URL at -- & extract the element
a = 'https://www.ig.com/es/ideas-de-trading-y-noticias/el-ibex-35-insiste-en-buscar-los-7900-puntos-a-la-espera-de-las--221104'
desired_value = a.split('--')[1]
& to convert:
from datetime import datetime
converted_date = datetime.strptime(desired_value , "%y%m%d")
formatted_date = datetime.strftime(converted_date, "%d %m %Y")

Convert Shell date format to Python date format

Can below piece of shell date format be converted to python date format?
date_used = $(date --date="$(date +%Y-%m-15) - 1 month" "+%Y_%m")
As per my understanding this above format is just taking day as 15 of current month and it simply subtracts 1 month and results in giving output in the format of year_month.
Output of the above shell date format -->
echo $date_used = 2022_05
Can this particular scenario be done using python?
Any update about it would be really appreciable.
An equivalent would be:
from datetime import datetime,timedelta
# Current date, replace date with 15, subtract 30 days and format
date_used = (datetime.now().replace(day=15) - timedelta(days=30)).strftime('%Y_%m')
print(date_used)
Output:
2022_05
You can use python's datetime module to do this
it has a function named strptime you can use to read date and time data with format code very similar to unix date format (or maybe its even same i'm not sure)
and strftime to output in a certain format as well
you can see the functions and the format code to see if there's any different on
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
Example code
from datetime import datetime, timedelta
date = datetime.strptime((datetime.now() - timedelta(days=30)).strftime("%Y-%m") + "-15", "%Y-%m-%d")
print(date)
print(date.strftime("%Y_%m"))
output
2022-05-15 00:00:00
2022_05

How to convert string datetime to UTC UNIX?

I have date in the as string in the following format: 202001010000
I am trying to convert this to UNIX format and get the result in UTC
I did:
import datetime
stime = "202001010000"
print(int(datetime.datetime.strptime(stime, "%Y%m%d%H%M").replace(tzinfo=datetime.timezone.utc).timestamp()))
and this is giving me the output in UNIX, but in CEST format.
With the above code I get: 1577836800 but I want the output to be 1577833200
What is the mistake I am doing?
You're setting time zone to UTC when converting to datetime. But since your input represents time in Germany you want a time zone that is active there. EX:
from datetime import datetime
from zoneinfo import ZoneInfo # Python 3.9+, can use backports.zoneinfo for older versions
stime = "202001010000"
# stime represents time in Germany so we use time zone
time_zone = ZoneInfo('Europe/Berlin')
# to datetime, with tz set:
dtobj = datetime.strptime(stime, "%Y%m%d%H%M").replace(tzinfo=time_zone)
# unix time
ts = dtobj.timestamp()
print(ts)
# 1577833200.0
# back to datetime, again specify time zone
dtobj = datetime.fromtimestamp(ts, tz=time_zone)
print(dtobj)
# 2020-01-01 00:00:00+01:00
Note that if the input represents the same time zone your OS is configured to use, this works correctly without setting a time zone. But I think it's better to be explicit here, to avoid confusion if you e.g. run this script on a machine configured to use another time zone.
What you're trying to get is 7 hours behind and you cannot do that from your start date. You must push your start date back 1 day and push your hours forward 17. This code will work for you
import datetime
stime = "201912310000"
my_date = datetime.datetime.strptime(stime, "%Y%m%d%H%M")
my_date_utc = my_date.replace(hour=17)
my_timestamp = my_date_utc.timestamp()
print(int(my_timestamp))

Does a module exist that can fix a faulty time format?

I have data in the format as follows:
12/07/2018 23:00
12/07/2018 24:00
13/07/2018 1:00
and wanted to know if there exists a module in python that can change the 12/07/2018 24:00 to 13/07/2018 0:00
This should cover all cases for you assuming your dateformat string is static:
from datetime import datetime, timedelta
def fix_time(time_string):
if '24:00' in time_string: # Does time_string contain silly format
_date, _ = time_string.split() # Ignore time part since it will default to 00:00
calendar_date= datetime.strptime(_date, '%d/%m/%Y')
corrected_time = calendar_date + timedelta(days=1) # Add one day to get correct date
time_string = corrected_time.strftime('%d/%m/%Y %H:%M') # Convert back to str
return time_string
Sample output:
fix_time('31/12/2018 24:00')>'01/01/2019 00:00'
Code could be made more concise but this should be a good start point.

How do I convert the time property in MixPanel?

I'm using MixPanel and trying to convert the ['property']['time'] field that I get when I'm reviewing events. I thought it was a UTC time, and this is how I'm trying to convert the value back to my local timezone.
from dateutil import tz
from_zone = tz.tzutc()
to_zone = tz.tzlocal()
def convert_from_mix_time(mix_time):
utc_date = datetime.fromtimestamp(int(mix_time))
utc_date = utc_date.replace(tzinfo=from_zone)
local_date = utc_date.astimezone(to_zone)
return local_date
Taking the MixPanel time 1394199886 (this should be 4:44 PM EST), the UTC time is 2014-03-07 08:44:46+00:00 and the converted time is 2014-03-07 03:44:46 (definitely not the right time). Anyone know how to do the conversion?
I don't get that timestamp to be 4:44 PM EST, but I could recommend utcfromtimestamp() if you want your code to be a bit simpler.
dt = datetime.datetime.utcfromtimestamp(ts)
You need to normalize the datetime object:
def convert_mix_time(mix_time):
utc_date = datetime.utcfromtimestamp(int(mix_time))
utc_date = utc_date.replace(tzinfo=from_zone)
local_date = utc_date.astimezone(to_zone)
return from_zone.normalize(local_date)
The timzeone of the data is defined per project. Click on the geat icon at the bottom right of Mixpanel's Web UI. It'll open a window. In the Data management section there's a Timezone field. Set it to UTC. that's the timezone Mixpanel will record the data coming in. Note that it won't affect past data. After that you can convert the timezone like you did.

Categories

Resources