Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 KB | None | 0 0
  1. def time_in_24h(time,time_day): #time is eastern time
  2. ''' (number, str)--> number
  3. This function converts a 12-hour time, represented by am/pm,to its equivalent 24-hour time.
  4. Returns time in the 24-hour format.
  5. '''
  6. cCentral = 'Central Time(CT)' #-1 hour offset
  7. mMountain = 'Mountain Time(MT)'#-2 hours offset
  8. pPacific = 'Pacific Time(PT)'#-3 hours offset
  9. eEst = 'EST is'
  10.  
  11. if time_day!='pm' and time_day!='am':
  12.  
  13. print('Input is not in the right format') #this is where input is not in the right format
  14. return 0
  15.  
  16. else:
  17. print(time, time_day, eEst, time-1, cCentral)
  18. print(time, time_day, eEst, time-2, mMountain)
  19. print(time, time_day, eEst, time-3, pPacific)
  20.  
  21. time_in_24h(7,'am')
  22.  
  23. 7 am EST is 6 Central Time(CT)
  24. 7 am EST is 5 Mountain Time(MT)
  25. 7 am EST is 4 Pacific Time(PT)
  26.  
  27. 1 pm EST is 12 pm Central Time(CT)
  28. 1 pm EST is 11 am Mountain Time(MT)
  29. 1 pm EST is 10 am Pacific Time(PT)
  30.  
  31. def to_24hour(hour, ampm):
  32. """Convert a 12-hour time and "am" or "pm" to a 24-hour value."""
  33. if ampm == 'am':
  34. return 0 if hour == 12 else hour
  35. else:
  36. return 12 if hour == 12 else hour + 12
  37.  
  38. def to_12hour(hour):
  39. """Convert a 24-hour clock value to a 12-hour one."""
  40. if hour == 0:
  41. return (12, 'am')
  42. elif hour < 12:
  43. return (hour, 'am')
  44. elif hour == 12:
  45. return (12, 'pm')
  46. else:
  47. return (hour - 12, 'pm')
  48.  
  49. def time_in_24h(time,time_day): #time is eastern time
  50. ''' (number, str)--> number
  51. This function converts a 12-hour time, represented by am/pm,to its equivalent 24-hour time.
  52. Returns time in the 24-hour format.
  53. '''
  54. cCentral = 'Central Time(CT)' #-1 hour offset
  55. mMountain = 'Mountain Time(MT)'#-2 hours offset
  56. pPacific = 'Pacific Time(PT)'#-3 hours offset
  57. eEst = 'EST is'
  58.  
  59. if time_day!='pm' and time_day!='am':
  60.  
  61. print('Input is not in the right format') #this is where input is not in the right format
  62. return 0
  63.  
  64. else:
  65. est_24hour = to_24hour(time, time_day)
  66. hour, ampm = to_12hour((est_24hour - 1 + 24) % 24)
  67. print(time, time_day, eEst, hour, ampm, cCentral)
  68. hour, ampm = to_12hour((est_24hour - 2 + 24) % 24)
  69. print(time, time_day, eEst, hour, ampm, mMountain)
  70. hour, ampm = to_12hour((est_24hour - 3 + 24) % 24)
  71. print(time, time_day, eEst, hour, ampm, pPacific)
  72.  
  73. >>> time_in_24h(1, 'pm')
  74. 1 pm EST is 12 pm Central Time(CT)
  75. 1 pm EST is 11 am Mountain Time(MT)
  76. 1 pm EST is 10 am Pacific Time(PT)
  77.  
  78. now = datetime.datetime(2014, 3, 14, 12, 34)
  79.  
  80. et = pytz.timezone('America/New_York')
  81. ct = pytz.timezone('America/Chicago')
  82. mt = pytz.timezone('America/Denver')
  83. pt = pytz.timezone('America/Los_Angeles')
  84.  
  85. now_et = et.normalize(et.localize(now))
  86. now_ct = ct.normalize(now_et.astimezone(ct))
  87. now_mt = mt.normalize(now_et.astimezone(mt))
  88. now_pt = pt.normalize(now_et.astimezone(pt))
  89.  
  90. print('{} Eastern in various other timezones:'.format(now_et.strftime('%I %p')))
  91. print('Central : {}'.format(now_ct.strftime('%I %p')))
  92. print('Mountain: {}'.format(now_mt.strftime('%I %p')))
  93. print('Pacific : {}'.format(now_pt.strftime('%I %p')))
  94.  
  95. def time_in_24h(hours, am_or_pm):
  96. """Convert to 24 hours."""
  97. if not (1 <= hours <= 12):
  98. raise ValueError("hours must be in 01,..,12 range, got %r" % (hours,))
  99. hours %= 12 # accept 12am as 00, and 12pm as 12
  100. am_or_pm = am_or_pm.lower()
  101. if am_or_pm == 'am':
  102. pass
  103. elif am_or_pm == 'pm':
  104. hours += 12
  105. else:
  106. raise ValueError("am_or_pm must be 'am' or 'pm', got: %r" % (am_or_pm,))
  107. return hours
  108.  
  109. from datetime import datetime
  110.  
  111. def time_in_24h(hours, am_or_pm):
  112. return datetime.strptime("%02d%s" % (hours, am_or_pm), '%I%p').hour
  113.  
  114. from datetime import datetime, time
  115. import pytz
  116.  
  117. def astimezone(aware_dt, dest_tz):
  118. """Convert the time to `dest_tz` timezone"""
  119. return dest_tz.normalize(aware_dt.astimezone(dest_tz))
  120.  
  121. def print_times(hours, am_or_pm, date=None, is_dst=None):
  122. source_tz = pytz.timezone('US/Eastern')
  123.  
  124. # 2. add date
  125. if date is None: # if date is not specified; use today
  126. date = datetime.now(source_tz).date()
  127. naive_dt = datetime.combine(date, time(time_in_24h(hours, am_or_pm), 0, 0))
  128.  
  129. # 3. create timezone-aware datetime object in the source timezone
  130. source_dt = source_tz.localize(naive_dt, is_dst=is_dst)
  131.  
  132. #NOTE: these timezone names are deprecated,
  133. # use Continent/City format instead
  134. fmt = '%I %p %Z'
  135. for tzname in 'US/Central', 'US/Mountain', 'US/Pacific':
  136. dest_dt = astimezone(source_dt, pytz.timezone(tzname))
  137. print("{source_dt:{fmt}} is {dest_dt:{fmt}} ({tzname})".format(
  138. **locals()))
  139.  
  140. >>> print_times(7, 'am')
  141. 07 AM EDT is 06 AM CDT (US/Central)
  142. 07 AM EDT is 05 AM MDT (US/Mountain)
  143. 07 AM EDT is 04 AM PDT (US/Pacific)
  144. >>> print_times(1, 'pm')
  145. 01 PM EDT is 12 PM CDT (US/Central)
  146. 01 PM EDT is 11 AM MDT (US/Mountain)
  147. 01 PM EDT is 10 AM PDT (US/Pacific)
  148. >>> print_times(9, 'pm')
  149. 09 PM EDT is 08 PM CDT (US/Central)
  150. 09 PM EDT is 07 PM MDT (US/Mountain)
  151. 09 PM EDT is 06 PM PDT (US/Pacific)
  152.  
  153. >>> from datetime import datetime
  154. >>> from pytz import timezone
  155. >>> import pytz
  156. >>> central = timezone('US/Central')
  157. >>> mountain = timezone('US/Mountain')
  158. >>> mountain_date = mountain.localize(datetime.now()) # set time as right now mountain time
  159. >>> fmt = '%Y-%m-%d %I:%M:%S %p'
  160. >>> central_date = central.normalize(mountain_date.astimezone(central))
  161. >>> print central_date.strftime(fmt), central, 'is', mountain_date.strftime(fmt), mountain
  162. 2014-03-15 07:29:17 PM US/Mountain is 2014-03-15 08:29:17 PM US/Central
  163.  
  164. >>> def time_in_24h(hour, am_pm):
  165. """hour is an integer that represents eastern time on the 12-hour clock, and am_pm
  166. will take any string that begins with 'a' or 'p'"""
  167. if not am_pm.lower().startswith(('a', 'p')): # return None if am_pm does not start with 'a' or 'p'
  168. return
  169. central = timezone('US/Central')
  170. mountain = timezone('US/Mountain')
  171. pacific = timezone('US/Pacific')
  172. eastern = timezone('US/Eastern')
  173. if hour == 12:
  174. mod_hour = 0 if am_pm.lower().startswith('a') else 12
  175. else:
  176. mod_hour = hour + 12 if am_pm.lower().startswith('p') else hour # add 12 if am_pm starts with 'p', but keep original hour for display purposes
  177. fmt = '%H:%M %p'
  178. eastern_time = eastern.localize(datetime(1900, 1, 2, mod_hour))
  179. for tz in (central, mountain, pacific):
  180. d = tz.normalize(eastern_time.astimezone(tz))
  181. print hour, am_pm, 'EST is', d.strftime(fmt), tz # tz displays as pytz timezone; can change
  182.  
  183.  
  184. >>> time_in_24h(1, 'am')
  185. 1 am EST is 00:00 AM US/Central
  186. 1 am EST is 23:00 PM US/Mountain
  187. 1 am EST is 22:00 PM US/Pacific
  188. >>> time_in_24h(9, 'pm')
  189. 9 pm EST is 20:00 PM US/Central
  190. 9 pm EST is 19:00 PM US/Mountain
  191. 9 pm EST is 18:00 PM US/Pacific
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement