def finding_ambiguous_datetimes(onebike_datetimes):
    # Loop over trips
    for trip in onebike_datetimes:
        # Rides with ambiguous start
        if tz.datetime_ambiguous(trip['start']):
            print("Ambiguous start at " + str(trip['start']))
        # Rides with ambiguous end
        if tz.datetime_ambiguous(trip['end']):
            print("Ambiguous end at " + str(trip['end']))
Пример #2
0
 def get_problematic_time_value_error():
     if datetime_ambiguous(dt, tz):
         return ProblematicTimeValueError(
             '{!r} is ambiguous in time zone {!r} (because of '
             'daylight-saving-time intricacies...)'.format(dt, tz))
     else:
         assert not datetime_exists(dt, tz)
         return ProblematicTimeValueError(
             '{!r} does not exist in time zone {!r} (because of '
             'daylight-saving-time intricacies...)'.format(dt, tz))
Пример #3
0
 def find_existing_unambiguous_dt(search_direction):
     assert search_direction in ('earlier', 'later')
     MAX_ATTEMPTS = 32
     delta = TIME_DELTA_ONE_HOUR
     if search_direction == 'earlier':
         delta = (-delta)
     for i in range(MAX_ATTEMPTS):
         tried_dt = dt + i*delta
         if datetime_exists(tried_dt, tz) and not datetime_ambiguous(tried_dt, tz):
             return tried_dt
     raise RuntimeError('failed to find existing unambiguous datetime '
                        'that would be close enough to dt={!r} (maybe '
                        'class of tz={!r} is defective?)'.format(dt, tz))
Пример #4
0
def localize(dt, tzi, is_dst=False):
    """
    Mimicks `pytz`'s `localize` function using the `fold` attribute.
    """
    if dt.tzinfo is not None:
        raise ValueError('localize can only be used with naive datetimes')

    if is_dst is None:
        # If is_dst is None, we want to raise an error for uncertain situations
        dt_out = dt.replace(tzinfo=tzi)
        if tz.datetime_ambiguous(dt_out):
            raise AmbiguousTimeError(f"Ambiguous time {dt} in zone {tzi}")
        elif not tz.datetime_exists(dt_out):
            raise NonExistentTimeError(
                f"Time {dt} does not exist in zone {tzi}")
    else:
        dt_out = dt.replace(fold=(not is_dst), tzinfo=tzi)

    return dt_out
Пример #5
0
 def get_reaction_if_time_is_problematic():
     if datetime_ambiguous(dt, tz):
         return on_ambiguous_time
     if not datetime_exists(dt, tz):
         return on_non_existent_time
     return None
############### Exercise ################
# Finding ambiguous datetimes
# At the end of lesson 2, we saw something anomalous in our bike trip duration data. Let's see if we can identify what the problem might be.

# The data has is loaded as onebike_datetimes, and tz has already been imported from dateutil.

############# Instructions ##############
# Loop over the trips in onebike_datetimes:
# Print any rides whose start is ambiguous.
# Print any rides whose end is ambiguous.

# Loop over trips
for trip in onebike_datetimes:
    # Rides with ambiguous start
    if tz.datetime_ambiguous(trip['start']):
        print("Ambiguous start at " + str(trip['start']))
    # Rides with ambiguous end
    if tz.datetime_ambiguous(trip['end']):
        print("Ambiguous end at " + str(trip['end']))

############### Exercise ################
# Cleaning daylight saving data with fold
# As we've just discovered, there is a ride in our data set which is being messed up by a Daylight Savings shift. Let's clean up the data set so we actually have a correct minimum ride length. We can use the fact that we know the end of the ride happened after the beginning to fix up the duration messed up by the shift out of Daylight Savings.

# Since Python does not handle tz.enfold() when doing arithmetic, we must put our datetime objects into UTC, where ambiguities have been resolved.

# onebike_datetimes is already loaded and in the right timezone. tz and timezone have been imported. Use tz.UTC for your timezone.

############# Instructions ##############
# Complete the if statement to be true only when a ride's start comes after its end.
Пример #7
0
    def ambiguous(self):
        """ Returns a boolean indicating whether the :class:`Arrow <arrow.arrow.Arrow>` object is ambiguous."""

        return dateutil_tz.datetime_ambiguous(self._datetime)
Пример #8
0
def is_ambiguous(dt):
    return tz.datetime_ambiguous(dt)
Пример #9
0
 def ambiguous(self):
     return dtz.datetime_ambiguous(self._dt)