Guest User

Untitled

a guest
Oct 22nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. TO DIFF TIMEZONE:
  2. In [39]: from pprint import pprint
  3. ...: import pytz
  4. ...: from datetime import datetime
  5. ...:
  6. ...: a = [["tehran", str(datetime.now())], ["turkey", str(datetime.now())], ["toronto", str(datetime.now())]]
  7. ...: result = []
  8. ...: for i,d in enumerate(a):
  9. ...: #if i == 0:
  10. ...: # result.append([d[0], datetime.strptime(d[1], "%Y-%m-%d %H:%M")])
  11. ...: # continue
  12. ...: timezone = [x for x in pytz.all_timezones if d[0] in x.lower()][0]
  13. ...: print(timezone)
  14. ...: datetime = datetime.strptime(d[1], "%Y-%m-%d %H:%M:%S.%f")
  15. ...: #datetime = d[1]
  16. ...: result.append([d[0], datetime.astimezone(pytz.timezone(timezone))])
  17. ...: pprint(result)
  18. ...:
  19. Asia/Tehran
  20. Turkey
  21. America/Toronto
  22. [['tehran',
  23. datetime.datetime(2018, 10, 20, 17, 35, 11, 609910, tzinfo=<DstTzInfo 'Asia/Tehran' +0330+3:30:00 STD>)],
  24. ['turkey',
  25. datetime.datetime(2018, 10, 20, 17, 5, 11, 609917, tzinfo=<DstTzInfo 'Turkey' +03+3:00:00 STD>)],
  26. ['toronto',
  27. datetime.datetime(2018, 10, 20, 10, 5, 11, 609918, tzinfo=<DstTzInfo 'America/Toronto' EDT-1 day, 20:00:00 DST>)]]
  28.  
  29.  
  30. ALL TO TIMEZONE:
  31. In [52]: from pprint import pprint
  32. ...: import pytz
  33. ...: from datetime import datetime
  34. ...: a = [["tehran", "2018-10-15 12:00"], ["turkey", "2018-10-15 14:30"], ["toronto", "2018-10-15 18:30"]]
  35. ...: result = []
  36. ...: for i,d in enumerate(a):
  37. ...: #if i == 0:
  38. ...: # result.append([d[0], datetime.strptime(d[1], "%Y-%m-%d %H:%M")])
  39. ...: # continue
  40. ...: timezone = [x for x in pytz.all_timezones if d[0] in x.lower()][0]
  41. ...: datetime1 = datetime.strptime(d[1], "%Y-%m-%d %H:%M")
  42. ...: datetime1.replace(tzinfo=pytz.timezone(timezone))
  43. ...: g = datetime1.astimezone(pytz.timezone(timezone))
  44. ...: result.append([d[0], g.astimezone(pytz.timezone('UTC'))])
  45. ...: pprint(result)
  46. ...:
  47. [['tehran', datetime.datetime(2018, 10, 15, 8, 30, tzinfo=<UTC>)],
  48. ['turkey', datetime.datetime(2018, 10, 15, 11, 0, tzinfo=<UTC>)],
  49. ['toronto', datetime.datetime(2018, 10, 15, 15, 0, tzinfo=<UTC>)]]
Add Comment
Please, Sign In to add comment