pastebin - collaborative debugging tool
time.kpaste.net RSS


Python Time Helpers
Posted by Anonymous on Thu 1st Nov 2018 21:38
raw | new post

  1. import datetime
  2. import time
  3. from pytz import timezone
  4.  
  5. def unixTimestampToUTC(theUnixTimestamp):
  6.     #dt=datetime.datetime.utcfromtimestamp(int(theUnixTimestamp))
  7.     dt=datetime.datetime.fromtimestamp(int(theUnixTimestamp),timezone('UTC'))
  8.     return dt
  9.  
  10. def getLocalTime(aDateTime):
  11.     glt=aDateTime.astimezone(timezone('America/Chicago'))
  12.     return glt
  13.  
  14. def getUTCTime(aDateTime):
  15.     glt=aDateTime.astimezone(timezone('UTC'))
  16.     return glt
  17.  
  18. def ymdToUTC(year,month,day,hour,minute,second):
  19.     dt = datetime.datetime(year=int(year),month=int(month),day=int(day),hour=int(hour),minute=int(minute),second=int(second),tzinfo=timezone('UTC'))
  20.     return dt
  21.  
  22. def dtToUnixTimestamp(aDateTime):
  23.     return aDateTime.timestamp()
  24.  
  25. print("=====================")
  26. unixTimestamp="1540989296"
  27. print(unixTimestamp+"   :2018-10-31 12:34:56")
  28. dateOne=unixTimestampToUTC(unixTimestamp)
  29. print("As Local Time:"+str(getLocalTime(dateOne)))
  30. print("As UTC   Time:"+str(getUTCTime(dateOne)))
  31. print(dtToUnixTimestamp(dateOne))
  32.  
  33. print("=====================")
  34. unixTimestamp="1541006299"
  35. print(unixTimestamp+"   :2018-10-31 17:18:19")
  36. dateOne=unixTimestampToUTC(unixTimestamp)
  37. print("As Local Time:"+str(getLocalTime(dateOne)))
  38. print("As UTC   Time:"+str(getUTCTime(dateOne)))
  39. print(dtToUnixTimestamp(dateOne))
  40.  
  41. print("=====================")
  42. unixTimestamp="1541024604"
  43. print(unixTimestamp+"   :2018-10-31 22:23:24")
  44. dateOne=unixTimestampToUTC(unixTimestamp)
  45. print("As Local Time:"+str(getLocalTime(dateOne)))
  46. print("As UTC   Time:"+str(getUTCTime(dateOne)))
  47. print(dtToUnixTimestamp(dateOne))
  48.  
  49. print("=====================")
  50. unixTimestamp="1540958706"
  51. print(unixTimestamp+"   :2018-10-31 04:05:06")
  52. dateOne=unixTimestampToUTC(unixTimestamp)
  53. print("As Local Time:"+str(getLocalTime(dateOne)))
  54. print("As UTC   Time:"+str(getUTCTime(dateOne)))
  55. print(dtToUnixTimestamp(dateOne))
  56.  
  57. print("=*==*================")
  58. print("             :2018-10-31 04:05:06")
  59. dateOne=ymdToUTC("2018","10","31","04","05","06")
  60. print("As Local Time:"+str(getLocalTime(dateOne)))
  61. print("As UTC   Time:"+str(getUTCTime(dateOne)))
  62. print(dtToUnixTimestamp(dateOne))
  63.  
  64. dateOne=dateOne.replace(hour=0,minute=0,second=0,microsecond=0)
  65. print("As Local Time:"+str(getLocalTime(dateOne)))
  66. print("As UTC   Time:"+str(getUTCTime(dateOne)))
  67. print(dtToUnixTimestamp(dateOne))

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at