def test_to_local_timezone(self): i18n.get_i18n().set_timezone('US/Eastern') format = '%Y-%m-%d %H:%M:%S %Z%z' # Test datetime with timezone set base = datetime.datetime(2002, 10, 27, 6, 0, 0, tzinfo=pytz.UTC) localtime = i18n.to_local_timezone(base) result = localtime.strftime(format) self.assertEqual(result, '2002-10-27 01:00:00 EST-0500') # Test naive datetime - no timezone set base = datetime.datetime(2002, 10, 27, 6, 0, 0) localtime = i18n.to_local_timezone(base) result = localtime.strftime(format) self.assertEqual(result, '2002-10-27 01:00:00 EST-0500')
def is_book_date_and_time_inside_schedule(self, schedule, booking_datetime_utc): booking_datetime_local = to_local_timezone(booking_datetime_utc) book_weekday_index = booking_datetime_local.weekday() (book_weekday_key, book_weekday_label) = utilities.time.get_day_of_the_week_from_python_weekday(book_weekday_index) # make an int with the time book_time_int = int(booking_datetime_local.hour) logging.info('Checking if weekday %s and hour %s is available' % (book_weekday_key, book_time_int)) if book_weekday_key == schedule.day: if book_time_int >= schedule.start_time and book_time_int <= schedule.end_time: return True
def create_bookings_dict(bookings): ''' create dict [date] = booking ''' bd = dict() for b in bookings: #logging.info('Adding Booking to DICT %s' % b) booking_datetime = to_local_timezone(b.datetime) booking_date = booking_datetime.date() if bd.has_key(booking_date): bd[booking_date].append(b) else: bd[booking_date] = [b] return bd
def is_book_date_and_time_inside_schedule(self, schedule, booking_datetime_utc): booking_datetime_local = to_local_timezone(booking_datetime_utc) book_weekday_index = booking_datetime_local.weekday() (book_weekday_key, book_weekday_label ) = utilities.time.get_day_of_the_week_from_python_weekday( book_weekday_index) # make an int with the time book_time_int = int(booking_datetime_local.hour) logging.info('Checking if weekday %s and hour %s is available' % (book_weekday_key, book_time_int)) if book_weekday_key == schedule.day: if book_time_int >= schedule.start_time and book_time_int <= schedule.end_time: return True
def rebase_datetime(value, zone): """Rebases a datetime to the given timezone. """ if not isinstance(value, datetime): return value if value.tzinfo is None: value = pytz.utc.localize(value) if isinstance(zone, basestring): try: tz = pytz.timezone(zone) except pytz.UnknownTimeZoneError: tz = None else: tz = None if tz: return value.astimezone(tz) else: return i18n.to_local_timezone(value)
def remove_confirmed_bookings_from_schedule(schedule_dict, bookings): ''' Remove all bookings from schedule dict ''' for b in bookings: # convert booking datetime to local timezone booking_datetime = to_local_timezone(b.datetime) booking_date = booking_datetime.date() service = b.service.get() duration = service.duration booking_datetime_end = booking_datetime + timedelta(mins=duration) if schedule_dict.has_key(booking_date): day_datetimes = schedule_dict[booking_date] tz = booking_datetime.tzinfo day_datetimes = set_timezones(day_datetimes, tz) if booking_datetime in day_datetimes: day_datetimes.remove(booking_datetime) schedule_dict[booking_date] = day_datetimes logging.info('removed %s from %s' % (booking_datetime, schedule_dict[booking_date])) return schedule_dict
def localize_field(self, value): if value: datetime = i18n.to_local_timezone(value) return i18n.format_datetime(datetime, _("MM/dd/YYYY HH:mm:ss")) return super(DateTimeField, self).localize_field(value)
def localize_field(self, value): if value: datetime = i18n.to_local_timezone(value) return i18n.format_datetime(datetime, _('MM/dd/YYYY HH:mm:ss')) return super(DateTimeField, self).localize_field(value)
def generate_complete_datetimes_dict(schedules, start_date, period, bookings): ''' Generate a dict of all dates in the period to list of hours available ''' dtm = dict() # create a dict [date_key][start_time] = schedule sm = create_schedule_dict(schedules) logging.info('BOOKINGS %s ' % bookings) bd = create_bookings_dict(bookings) # timezone adjustment tz = None if len(bookings) > 0: booking_datetime = to_local_timezone(bookings[0].datetime) tz = booking_datetime.tzinfo end_date = start_date + period d = start_date while d < end_date: logging.info('DATE %s' % d) dtm[d] = [] # day of week weekday_int = d.weekday() # map the number to the actual day day_key = get_days_of_the_week()[weekday_int][0] # check schedules for that day days_schedules = sm[day_key] # for each schedule on that day # create a list of tuples (start, end) schedule_period_tuples = [] for hour_key in sorted(days_schedules.keys()): s = days_schedules[hour_key] start_datetime = datetime.combine(d, time(hour=s.start_time)) end_datetime = datetime.combine(d, time(hour=s.end_time)) # adjust tz to match bookings if tz: start_datetime = start_datetime.replace(tzinfo=tz) end_datetime = end_datetime.replace(tzinfo=tz) start_end_tuple = (start_datetime, end_datetime) schedule_period_tuples.append(start_end_tuple) logging.info('SCHEDULE: %s ' % schedule_period_tuples) # remove bookings #for each booking that day, create tuple (start, end) day_bookings = bd.get(d, []) logging.info('DAY BOOKINGS: %s ' % day_bookings) booking_period_tuples = [] for b in day_bookings: booking_datetime = to_local_timezone(b.datetime) booking_date = booking_datetime.date() service = b.service.get() duration = service.duration booking_datetime_end = booking_datetime + timedelta(minutes=duration) b_start_end_tuple = (booking_datetime, booking_datetime_end) booking_period_tuples.append(b_start_end_tuple) logging.info('BOOKINGS TUPLES: %s ' % booking_period_tuples) available_period_tuples = subtract_booking_from_schedule(schedule_period_tuples, booking_period_tuples) logging.info('AVAILABLE: %s ' % available_period_tuples) datetimes_list = generate_datetimes_from_tuples_list(available_period_tuples) dtm[d].extend(datetimes_list) d = d + timedelta(days=1) return dtm