Beispiel #1
1
    def normalize_field(self, value):
        if isinstance(value, basestring):
            locale = settings.get_locale()

            date_str, time_str = value.split(' ')
            date = dates.parse_date(date_str, locale)
            time = dates.parse_time(time_str, locale)
            dtime = datetime.datetime(date.year, date.month, date.day, time.hour, time.minute, time.second)
            dtime = settings.get_timezone().localize(dtime)
            utc_dt = dates.UTC.normalize(dtime)
            return utc_dt.replace(tzinfo=None)

        return super(DateTimeField, self).normalize_field(value)
Beispiel #2
0
 def test_digits(self):
     self.assertEqual(dates.parse_time('15:30', locale='en_US'),
                      time(15, 30), "OK")
     self.assertEqual(dates.parse_time('3:30', locale='en_US'), time(3, 30),
                      "OK")
     self.assertEqual(dates.parse_time('00:30', locale='en_US'),
                      time(0, 30), "OK")
Beispiel #3
0
 def test_pm(self):
     self.assertEqual(dates.parse_time('03:30 PM', locale='en_US'),
                      time(15, 30))
     self.assertEqual(dates.parse_time('03:30 pM', locale='en_US'),
                      time(15, 30))
     self.assertEqual(dates.parse_time('03:30 pm', locale='en_US'),
                      time(15, 30))
     self.assertEqual(dates.parse_time('03:30 Pm', locale='en_US'),
                      time(15, 30))
Beispiel #4
0
def parselocal_time(txt, locale):
    """TODO

    :param txt: TODO
    :param locale: the current locale (e.g: en, en_us, it)
    """
    return dates.parse_time(txt, locale)
Beispiel #5
0
def parselocal_time(txt, locale):
    """add???
    
    :param txt: add???
    :param locale: add???
    :returns: add???
    """
    return dates.parse_time(txt, locale)
Beispiel #6
0
 def clean(self, value):
     locale=get_current_language()
     if value != "":
         try:
             value = parse_time(value, locale=locale)
         except:
             raise forms.ValidationError(self.error_messages['invalid'])
     return super(TimeField, self).clean(value)     
Beispiel #7
0
def parselocal_time(txt, locale):
    """add???
    
    :param txt: add???
    :param locale: add???
    :returns: add???
    """
    return dates.parse_time(txt, locale)
Beispiel #8
0
    def parse_time(self, string):
        """Parse a time from a string

        This function uses the time format for the locale as a hint to determine
        the order in which the time fields appear in the string.

        In:
          - ``string`` -- the string containing the time

        Return:
          - a ``datetime.time`` object
        """
        return dates.parse_time(string, self)
Beispiel #9
0
 def extract(self):
     value, error = super(TimeWidgetExtractor, self).extract()
     if value is not NO_VALUE:
         if value:
             try:
                 locale = ILocale(self.request, default='en')
                 value = parse_time(value, locale=str(locale))
                 return value, error
             except (ValueError, IndexError) as err:
                 return None, 'Unknown time pattern'
         else:
             value = None
     return value, error
Beispiel #10
0
    def parse_time(self, string):
        """Parses a time from a string.

        This function uses the time format for the locale as a hint to
        determine the order in which the time fields appear in the string.
        Example::

            >>> parse_time('15:30:00', locale='en_US')
            datetime.time(15, 30)

        :param string:
            The string containing the time.
        :returns:
            The parsed time object.
        """
        return dates.parse_time(string, locale=self.locale)
Beispiel #11
0
    def parse_time(self, string):
        """Parses a time from a string.

        This function uses the time format for the locale as a hint to
        determine the order in which the time fields appear in the string.
        Example::

            >>> parse_time('15:30:00', locale='en_US')
            datetime.time(15, 30)

        :param string:
            The string containing the time.
        :returns:
            The parsed time object.
        """
        return dates.parse_time(string, locale=self.locale)
Beispiel #12
0
    def parse_time(self, string):
        """Parse a time from a string

        This function uses the time format for the locale as a hint to determine
        the order in which the time fields appear in the string.

        >>> Locale('en', 'US').parse_time('15:30:00')
        datetime.time(15, 30)

        In:
          - ``string`` -- the string containing the time

        Return:
          - a ``datetime.time`` object
        """
        return dates.parse_time(string, self)
Beispiel #13
0
 def deserialize(self, node, cstruct):
     if not cstruct:
         return null
     try:
         # Now Babel does not understand time without seconds
         cstruct = "%s:00" % cstruct
         result = parse_time(cstruct, locale=get_locale_name())
     except:
         raise Invalid(
             node,
             _(
                 self.err_template,
                 mapping={'val': cstruct}
             )
         )
     return result
def parse_time(string, locale=None):
    """Parses a time from a string.

    This function uses the time format for the locale as a hint to determine
    the order in which the time fields appear in the string.

    .. code-block:: python

       >>> parse_time('15:30:00', locale='en_US')
       datetime.time(15, 30)

    :param string:
        The string containing the time.
    :param locale:
        A locale code. If not set, uses the currently loaded locale.
    :returns:
        The parsed time object.
    """
    locale = locale or get_locale()
    return dates.parse_time(string, locale=locale)
Beispiel #15
0
def parse_time(string, locale=None):
    """Parses a time from a string.

    This function uses the time format for the locale as a hint to determine
    the order in which the time fields appear in the string.

    .. code-block:: python

       >>> parse_time('15:30:00', locale='en_US')
       datetime.time(15, 30)

    :param string:
        The string containing the time.
    :param locale:
        A locale code. If not set, uses the currently loaded locale.
    :returns:
        The parsed time object.
    """
    locale = locale or get_locale()
    return dates.parse_time(string, locale=locale)
Beispiel #16
0
def parse_time(string):
    """Parse a time from a string.
    """
    return dates.parse_time(string, locale=get_locale())
Beispiel #17
0
def test_parse_time():
    assert dates.parse_time('15:30:00', locale='en_US') == time(15, 30)
Beispiel #18
0
def parse_time(string):
    """Parse a time from a string.
    """
    return dates.parse_time(string, locale=get_locale())
Beispiel #19
0
def test_parse_time():
    assert dates.parse_time('15:30:00', locale='en_US') == time(15, 30)
Beispiel #20
0
def test_parse_time():
    assert dates.parse_time('15:30:33', locale='en_US') == time(15, 30, 33)
    assert dates.parse_time('15:30', locale='en_US') == time(15, 30)
    assert dates.parse_time('3:30', locale='en_US') == time(3, 30)
    assert dates.parse_time('00:30', locale='en_US') == time(0, 30)
    assert dates.parse_time('03:30 PM', locale='en_US') == time(15, 30)
    assert dates.parse_time('03:30 pM', locale='en_US') == time(15, 30)
    assert dates.parse_time('03:30 pm', locale='en_US') == time(15, 30)
    assert dates.parse_time('03:30 Pm', locale='en_US') == time(15, 30)
    assert dates.parse_time('03:30:21 AM', locale='en_US') == time(3, 30, 21)
    assert dates.parse_time('03:30:00 AM', locale='en_US') == time(3, 30)
    assert dates.parse_time('03:30:00 AM', locale='en_US') == time(3, 30)
Beispiel #21
0
def parselocal_time(txt, locale):
    """TODO
    
    :param txt: TODO
    :param locale: the current locale (e.g: en, en_us, it)"""
    return dates.parse_time(txt, locale)
Beispiel #22
0
def test_parse_time_exception():
    invalid_times = ('', 'a', 'aaa', '15', '15:')
    for invalid_time in invalid_times:
        with pytest.raises(ParseTimeException):
            dates.parse_time(invalid_time, 'en_US')
Beispiel #23
0
def test_parse_time(input, expected):
    assert dates.parse_time(input, locale='en_US') == expected
Beispiel #24
0
    async def night_time(self,
                         ctx: MyContext,
                         night_start: str = None,
                         night_end: str = None):
        """
        Set the night time. Only some exclusive ducks spawn during the night.

        Times are specified in UTC. The bot does *not* honor daylight savings time (DST). You might need to edit this
        setting twice a year if you care about DST
        """
        db_channel = await get_from_db(ctx.channel)
        _ = await ctx.get_translate_function()
        language_code = await ctx.get_language_code()

        if night_start is not None and night_end is not None:
            time_format = str(
                get_time_format(locale=language_code, format='medium'))
            time_example = format_time(datetime.datetime.now(),
                                       locale=language_code,
                                       format='medium')
            try:
                parsed_night_start = parse_time(night_start,
                                                locale=language_code)
            except IndexError:
                await ctx.send(
                    _(
                        "❌ I'm sorry, I couldn't understand the time you entered for night_start. "
                        "I'm looking for something following this format: `{time_format}` (ex: `{time_example}`)",
                        time_format=time_format,
                        time_example=time_example))
                return False

            seconds_night_start = parsed_night_start.hour * HOUR + parsed_night_start.minute * MINUTE + parsed_night_start.second * SECOND

            try:
                parsed_night_end = parse_time(night_end, locale=language_code)
            except IndexError:
                await ctx.send(
                    _(
                        "❌ I'm sorry, I couldn't understand the time you entered for night_end. "
                        "I'm looking for something following this format: `{time_format}` (ex: `{time_example}`)",
                        time_format=time_format,
                        time_example=time_example))
                return False

            seconds_night_end = parsed_night_end.hour * HOUR + parsed_night_end.minute * MINUTE + parsed_night_end.second * SECOND

            db_channel.night_start_at = seconds_night_start
            db_channel.night_end_at = seconds_night_end

            await db_channel.save()

        sun, duration_of_night, time_left_sun = await compute_sun_state(
            ctx.channel)

        duration_of_night_td = format_timedelta(
            datetime.timedelta(seconds=duration_of_night),
            locale=language_code)
        time_left_sun_td = format_timedelta(
            datetime.timedelta(seconds=time_left_sun),
            locale=language_code,
            add_direction=True)

        if duration_of_night == 0:
            await ctx.send(
                _(
                    "On {channel.mention}, it's currently daytime. The day will last forever.",
                    channel=ctx.channel,
                ))
        elif sun == "day":
            await ctx.send(
                _(
                    "On {channel.mention}, it's currently daytime, and night will fall {time_left_sun_td}. "
                    "Night will last for {duration_of_night_td}.",
                    channel=ctx.channel,
                    time_left_sun_td=time_left_sun_td,
                    duration_of_night_td=duration_of_night_td))
        else:
            await ctx.send(
                _(
                    "On {channel.mention}, it's currently nighttime, and the sun will rise {time_left_sun_td}. "
                    "A full night will last for {duration_of_night_td}.",
                    channel=ctx.channel,
                    time_left_sun_td=time_left_sun_td,
                    duration_of_night_td=duration_of_night_td))
Beispiel #25
0
 def test_am(self):
     self.assertEqual(dates.parse_time('03:30:21 AM', locale='en_US'),
                      time(3, 30, 21))
     self.assertEqual(dates.parse_time('03:30:00 AM', locale='en_US'),
                      time(3, 30))
Beispiel #26
-3
 def render(self, name, value, attrs=None):
     locale=get_current_language()
     if value is None: value = ""
     if value and isinstance(value, basestring):
         try:
             value = parse_time(value, locale=locale)
         except:
             pass
     if value is not "" and not isinstance(value, basestring):
         value = format_time(value, locale=locale)
     return super(TimeWidget, self).render(name, value, attrs)