Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TO DIFF TIMEZONE:
- In [39]: from pprint import pprint
- ...: import pytz
- ...: from datetime import datetime
- ...:
- ...: a = [["tehran", str(datetime.now())], ["turkey", str(datetime.now())], ["toronto", str(datetime.now())]]
- ...: result = []
- ...: for i,d in enumerate(a):
- ...: #if i == 0:
- ...: # result.append([d[0], datetime.strptime(d[1], "%Y-%m-%d %H:%M")])
- ...: # continue
- ...: timezone = [x for x in pytz.all_timezones if d[0] in x.lower()][0]
- ...: print(timezone)
- ...: datetime = datetime.strptime(d[1], "%Y-%m-%d %H:%M:%S.%f")
- ...: #datetime = d[1]
- ...: result.append([d[0], datetime.astimezone(pytz.timezone(timezone))])
- ...: pprint(result)
- ...:
- Asia/Tehran
- Turkey
- America/Toronto
- [['tehran',
- datetime.datetime(2018, 10, 20, 17, 35, 11, 609910, tzinfo=<DstTzInfo 'Asia/Tehran' +0330+3:30:00 STD>)],
- ['turkey',
- datetime.datetime(2018, 10, 20, 17, 5, 11, 609917, tzinfo=<DstTzInfo 'Turkey' +03+3:00:00 STD>)],
- ['toronto',
- datetime.datetime(2018, 10, 20, 10, 5, 11, 609918, tzinfo=<DstTzInfo 'America/Toronto' EDT-1 day, 20:00:00 DST>)]]
- ALL TO TIMEZONE:
- In [52]: from pprint import pprint
- ...: import pytz
- ...: from datetime import datetime
- ...: a = [["tehran", "2018-10-15 12:00"], ["turkey", "2018-10-15 14:30"], ["toronto", "2018-10-15 18:30"]]
- ...: result = []
- ...: for i,d in enumerate(a):
- ...: #if i == 0:
- ...: # result.append([d[0], datetime.strptime(d[1], "%Y-%m-%d %H:%M")])
- ...: # continue
- ...: timezone = [x for x in pytz.all_timezones if d[0] in x.lower()][0]
- ...: datetime1 = datetime.strptime(d[1], "%Y-%m-%d %H:%M")
- ...: datetime1.replace(tzinfo=pytz.timezone(timezone))
- ...: g = datetime1.astimezone(pytz.timezone(timezone))
- ...: result.append([d[0], g.astimezone(pytz.timezone('UTC'))])
- ...: pprint(result)
- ...:
- [['tehran', datetime.datetime(2018, 10, 15, 8, 30, tzinfo=<UTC>)],
- ['turkey', datetime.datetime(2018, 10, 15, 11, 0, tzinfo=<UTC>)],
- ['toronto', datetime.datetime(2018, 10, 15, 15, 0, tzinfo=<UTC>)]]
Add Comment
Please, Sign In to add comment