Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def time_in_24h(time,time_day): #time is eastern time
- ''' (number, str)--> number
- This function converts a 12-hour time, represented by am/pm,to its equivalent 24-hour time.
- Returns time in the 24-hour format.
- '''
- cCentral = 'Central Time(CT)' #-1 hour offset
- mMountain = 'Mountain Time(MT)'#-2 hours offset
- pPacific = 'Pacific Time(PT)'#-3 hours offset
- eEst = 'EST is'
- if time_day!='pm' and time_day!='am':
- print('Input is not in the right format') #this is where input is not in the right format
- return 0
- else:
- print(time, time_day, eEst, time-1, cCentral)
- print(time, time_day, eEst, time-2, mMountain)
- print(time, time_day, eEst, time-3, pPacific)
- time_in_24h(7,'am')
- 7 am EST is 6 Central Time(CT)
- 7 am EST is 5 Mountain Time(MT)
- 7 am EST is 4 Pacific Time(PT)
- 1 pm EST is 12 pm Central Time(CT)
- 1 pm EST is 11 am Mountain Time(MT)
- 1 pm EST is 10 am Pacific Time(PT)
- def to_24hour(hour, ampm):
- """Convert a 12-hour time and "am" or "pm" to a 24-hour value."""
- if ampm == 'am':
- return 0 if hour == 12 else hour
- else:
- return 12 if hour == 12 else hour + 12
- def to_12hour(hour):
- """Convert a 24-hour clock value to a 12-hour one."""
- if hour == 0:
- return (12, 'am')
- elif hour < 12:
- return (hour, 'am')
- elif hour == 12:
- return (12, 'pm')
- else:
- return (hour - 12, 'pm')
- def time_in_24h(time,time_day): #time is eastern time
- ''' (number, str)--> number
- This function converts a 12-hour time, represented by am/pm,to its equivalent 24-hour time.
- Returns time in the 24-hour format.
- '''
- cCentral = 'Central Time(CT)' #-1 hour offset
- mMountain = 'Mountain Time(MT)'#-2 hours offset
- pPacific = 'Pacific Time(PT)'#-3 hours offset
- eEst = 'EST is'
- if time_day!='pm' and time_day!='am':
- print('Input is not in the right format') #this is where input is not in the right format
- return 0
- else:
- est_24hour = to_24hour(time, time_day)
- hour, ampm = to_12hour((est_24hour - 1 + 24) % 24)
- print(time, time_day, eEst, hour, ampm, cCentral)
- hour, ampm = to_12hour((est_24hour - 2 + 24) % 24)
- print(time, time_day, eEst, hour, ampm, mMountain)
- hour, ampm = to_12hour((est_24hour - 3 + 24) % 24)
- print(time, time_day, eEst, hour, ampm, pPacific)
- >>> time_in_24h(1, 'pm')
- 1 pm EST is 12 pm Central Time(CT)
- 1 pm EST is 11 am Mountain Time(MT)
- 1 pm EST is 10 am Pacific Time(PT)
- now = datetime.datetime(2014, 3, 14, 12, 34)
- et = pytz.timezone('America/New_York')
- ct = pytz.timezone('America/Chicago')
- mt = pytz.timezone('America/Denver')
- pt = pytz.timezone('America/Los_Angeles')
- now_et = et.normalize(et.localize(now))
- now_ct = ct.normalize(now_et.astimezone(ct))
- now_mt = mt.normalize(now_et.astimezone(mt))
- now_pt = pt.normalize(now_et.astimezone(pt))
- print('{} Eastern in various other timezones:'.format(now_et.strftime('%I %p')))
- print('Central : {}'.format(now_ct.strftime('%I %p')))
- print('Mountain: {}'.format(now_mt.strftime('%I %p')))
- print('Pacific : {}'.format(now_pt.strftime('%I %p')))
- def time_in_24h(hours, am_or_pm):
- """Convert to 24 hours."""
- if not (1 <= hours <= 12):
- raise ValueError("hours must be in 01,..,12 range, got %r" % (hours,))
- hours %= 12 # accept 12am as 00, and 12pm as 12
- am_or_pm = am_or_pm.lower()
- if am_or_pm == 'am':
- pass
- elif am_or_pm == 'pm':
- hours += 12
- else:
- raise ValueError("am_or_pm must be 'am' or 'pm', got: %r" % (am_or_pm,))
- return hours
- from datetime import datetime
- def time_in_24h(hours, am_or_pm):
- return datetime.strptime("%02d%s" % (hours, am_or_pm), '%I%p').hour
- from datetime import datetime, time
- import pytz
- def astimezone(aware_dt, dest_tz):
- """Convert the time to `dest_tz` timezone"""
- return dest_tz.normalize(aware_dt.astimezone(dest_tz))
- def print_times(hours, am_or_pm, date=None, is_dst=None):
- source_tz = pytz.timezone('US/Eastern')
- # 2. add date
- if date is None: # if date is not specified; use today
- date = datetime.now(source_tz).date()
- naive_dt = datetime.combine(date, time(time_in_24h(hours, am_or_pm), 0, 0))
- # 3. create timezone-aware datetime object in the source timezone
- source_dt = source_tz.localize(naive_dt, is_dst=is_dst)
- #NOTE: these timezone names are deprecated,
- # use Continent/City format instead
- fmt = '%I %p %Z'
- for tzname in 'US/Central', 'US/Mountain', 'US/Pacific':
- dest_dt = astimezone(source_dt, pytz.timezone(tzname))
- print("{source_dt:{fmt}} is {dest_dt:{fmt}} ({tzname})".format(
- **locals()))
- >>> print_times(7, 'am')
- 07 AM EDT is 06 AM CDT (US/Central)
- 07 AM EDT is 05 AM MDT (US/Mountain)
- 07 AM EDT is 04 AM PDT (US/Pacific)
- >>> print_times(1, 'pm')
- 01 PM EDT is 12 PM CDT (US/Central)
- 01 PM EDT is 11 AM MDT (US/Mountain)
- 01 PM EDT is 10 AM PDT (US/Pacific)
- >>> print_times(9, 'pm')
- 09 PM EDT is 08 PM CDT (US/Central)
- 09 PM EDT is 07 PM MDT (US/Mountain)
- 09 PM EDT is 06 PM PDT (US/Pacific)
- >>> from datetime import datetime
- >>> from pytz import timezone
- >>> import pytz
- >>> central = timezone('US/Central')
- >>> mountain = timezone('US/Mountain')
- >>> mountain_date = mountain.localize(datetime.now()) # set time as right now mountain time
- >>> fmt = '%Y-%m-%d %I:%M:%S %p'
- >>> central_date = central.normalize(mountain_date.astimezone(central))
- >>> print central_date.strftime(fmt), central, 'is', mountain_date.strftime(fmt), mountain
- 2014-03-15 07:29:17 PM US/Mountain is 2014-03-15 08:29:17 PM US/Central
- >>> def time_in_24h(hour, am_pm):
- """hour is an integer that represents eastern time on the 12-hour clock, and am_pm
- will take any string that begins with 'a' or 'p'"""
- if not am_pm.lower().startswith(('a', 'p')): # return None if am_pm does not start with 'a' or 'p'
- return
- central = timezone('US/Central')
- mountain = timezone('US/Mountain')
- pacific = timezone('US/Pacific')
- eastern = timezone('US/Eastern')
- if hour == 12:
- mod_hour = 0 if am_pm.lower().startswith('a') else 12
- else:
- 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
- fmt = '%H:%M %p'
- eastern_time = eastern.localize(datetime(1900, 1, 2, mod_hour))
- for tz in (central, mountain, pacific):
- d = tz.normalize(eastern_time.astimezone(tz))
- print hour, am_pm, 'EST is', d.strftime(fmt), tz # tz displays as pytz timezone; can change
- >>> time_in_24h(1, 'am')
- 1 am EST is 00:00 AM US/Central
- 1 am EST is 23:00 PM US/Mountain
- 1 am EST is 22:00 PM US/Pacific
- >>> time_in_24h(9, 'pm')
- 9 pm EST is 20:00 PM US/Central
- 9 pm EST is 19:00 PM US/Mountain
- 9 pm EST is 18:00 PM US/Pacific
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement