def parse(self, date_string, settings):

        _time = self._parse_time(date_string, settings)

        def apply_time(dateobj, timeobj):
            if not isinstance(_time, time):
                return dateobj

            return dateobj.replace(
                hour=timeobj.hour, minute=timeobj.minute,
                second=timeobj.second, microsecond=timeobj.microsecond
            )

        if settings.RELATIVE_BASE:
            if 'local' not in settings.TIMEZONE.lower():
                self.now = localize_timezone(
                    settings.RELATIVE_BASE, settings.TIMEZONE)
            else:
                self.now = settings.RELATIVE_BASE

        elif 'local' in settings.TIMEZONE.lower():
            self.now = datetime.now()

        else:
            utc_dt = datetime.utcnow()
            self.now = apply_timezone(utc_dt, settings.TIMEZONE)

        date, period = self._parse_date(date_string)

        if date:
            date = apply_time(date, _time)
            if settings.TO_TIMEZONE:
                date = apply_timezone(date, settings.TO_TIMEZONE)

            if not settings.RETURN_AS_TIMEZONE_AWARE:
                date = date.replace(tzinfo=None)

        self.now = None
        return date, period
    def parse(self, date_string, settings):
        # print(date_string)
        date_string = strip_braces(date_string)
        date_string, ptz = pop_tz_offset_from_string(date_string)
        _time = self._parse_time(date_string, settings)

        _settings_tz = settings.TIMEZONE.lower()

        def apply_time(dateobj, timeobj):
            if not isinstance(_time, time):
                return dateobj

            return dateobj.replace(hour=timeobj.hour,
                                   minute=timeobj.minute,
                                   second=timeobj.second,
                                   microsecond=timeobj.microsecond)

        if settings.RELATIVE_BASE:
            self.now = settings.RELATIVE_BASE

            if 'local' not in _settings_tz:
                self.now = localize_timezone(self.now, settings.TIMEZONE)

            if ptz:
                if self.now.tzinfo:
                    self.now = self.now.astimezone(ptz)
                else:
                    self.now = ptz.localize(self.now)

            if not self.now.tzinfo:
                self.now = self.get_local_tz().localize(self.now)

        elif ptz:
            _now = datetime.now(ptz)

            if 'local' in _settings_tz:
                self.now = _now
            else:
                self.now = apply_timezone(_now, settings.TIMEZONE)

        else:
            if 'local' not in _settings_tz:
                utc_dt = datetime.utcnow()
                self.now = apply_timezone(utc_dt, settings.TIMEZONE)
            else:
                self.now = datetime.now(self.get_local_tz())

        date, period = self._parse_date(date_string,
                                        settings.PREFER_DATES_FROM)

        if date:
            date = apply_time(date, _time)
            if settings.TO_TIMEZONE:
                date = apply_timezone(date, settings.TO_TIMEZONE)

            if (not settings.RETURN_AS_TIMEZONE_AWARE
                    or (settings.RETURN_AS_TIMEZONE_AWARE
                        and 'default' == settings.RETURN_AS_TIMEZONE_AWARE
                        and not ptz)):
                date = date.replace(tzinfo=None)

        self.now = None
        return date, period
Example #3
0
 def test_localize_timezone_function(self, date, timezone, zone):
     tzaware_dt = localize_timezone(date, timezone)
     self.assertEqual(tzaware_dt.tzinfo.zone, zone)
Example #4
0
 def test_localize_timezone_function_exception(self, date, timezone, zone):
     tzaware_dt = localize_timezone(date, timezone)
     self.assertEqual(tzaware_dt.tzinfo._StaticTzInfo__name, zone)
Example #5
0
 def _is_valid_tz(self, tzstr):
     try:
         localize_timezone(datetime.datetime.now(), tzstr)
         return True
     except UnknownTimeZoneError:
         return False
Example #6
0
    def parse(self, date_string, settings):
        date_string = strip_braces(date_string)
        date_string, ptz = pop_tz_offset_from_string(date_string)
        _time = self._parse_time(date_string, settings)

        _settings_tz = settings.TIMEZONE.lower()

        def apply_time(dateobj, timeobj):
            if not isinstance(_time, time):
                return dateobj

            return dateobj.replace(
                hour=timeobj.hour, minute=timeobj.minute,
                second=timeobj.second, microsecond=timeobj.microsecond
            )

        if settings.RELATIVE_BASE:
            self.now = settings.RELATIVE_BASE

            if 'local' not in _settings_tz:
                self.now = localize_timezone(self.now, settings.TIMEZONE)

            if ptz:
                if self.now.tzinfo:
                    self.now = self.now.astimezone(ptz)
                else:
                    if hasattr(ptz, 'localize'):
                        self.now = ptz.localize(self.now)
                    else:
                        self.now = self.now.replace(tzinfo=ptz)

            if not self.now.tzinfo:
                if hasattr(self.get_local_tz(), 'localize'):
                    self.now = self.get_local_tz().localize(self.now)
                else:
                    self.now = self.now.replace(tzinfo=self.get_local_tz())

        elif ptz:
            _now = datetime.now(ptz)

            if 'local' in _settings_tz:
                self.now = _now
            else:
                self.now = apply_timezone(_now, settings.TIMEZONE)

        else:
            if 'local' not in _settings_tz:
                utc_dt = datetime.utcnow()
                self.now = apply_timezone(utc_dt, settings.TIMEZONE)
            else:
                self.now = datetime.now(self.get_local_tz())

        date, period = self._parse_date(date_string, settings.PREFER_DATES_FROM)

        if date:
            old_date = date
            date = apply_time(date, _time)
            if settings.RETURN_TIME_AS_PERIOD and old_date != date:
                period = 'time'

            if settings.TO_TIMEZONE:
                date = apply_timezone(date, settings.TO_TIMEZONE)

            if (
                not settings.RETURN_AS_TIMEZONE_AWARE
                or (settings.RETURN_AS_TIMEZONE_AWARE
                    and 'default' == settings.RETURN_AS_TIMEZONE_AWARE and not ptz)
            ):
                date = date.replace(tzinfo=None)

        # self.now = None

        # This ^ causes issues with threading.
        # I think we're OK to take this "reset" out here.
        # self.now = None gets set in the init of this class.

        # Issue:
        # https://github.com/scrapinghub/dateparser/issues/276#issuecomment-290427553

        # Commit, with someone else also removing this reset
        # https://github.com/mistio/dateparser/commit/7bfc3f4ab6e7c6255ecbed9011078c07a63700ef
        return date, period
    def parse(self, date_string, settings):

        _time = self._parse_time(date_string, settings)

        date_string = strip_braces(date_string)
        date_string, ptz = pop_tz_offset_from_string(date_string)

        _settings_tz = settings.TIMEZONE.lower()

        def apply_time(dateobj, timeobj):
            if not isinstance(_time, time):
                return dateobj

            return dateobj.replace(
                hour=timeobj.hour, minute=timeobj.minute,
                second=timeobj.second, microsecond=timeobj.microsecond
            )

        if settings.RELATIVE_BASE:
            self.now = settings.RELATIVE_BASE

            if 'local' not in _settings_tz:
                self.now = localize_timezone(self.now, settings.TIMEZONE)

            if ptz:
                if self.now.tzinfo:
                    self.now = self.now.astimezone(ptz)
                else:
                    self.now = ptz.localize(self.now)

            if not self.now.tzinfo:
                self.now = self.get_local_tz().localize(self.now)

        elif ptz:
            _now = datetime.now(ptz)

            if 'local' in _settings_tz:
                self.now = _now
            else:
                self.now = apply_timezone(_now, settings.TIMEZONE)

        else:
            if 'local' not in _settings_tz:
                utc_dt = datetime.utcnow()
                self.now = apply_timezone(utc_dt, settings.TIMEZONE)
            else:
                self.now = datetime.now(self.get_local_tz())

        date, period = self._parse_date(date_string)

        if date:
            date = apply_time(date, _time)
            if settings.TO_TIMEZONE:
                date = apply_timezone(date, settings.TO_TIMEZONE)

            if (
                not settings.RETURN_AS_TIMEZONE_AWARE or
                (settings.RETURN_AS_TIMEZONE_AWARE and
                 'default' == settings.RETURN_AS_TIMEZONE_AWARE and not ptz)
            ):
                date = date.replace(tzinfo=None)

        self.now = None
        return date, period
Example #8
0
 def test_localize_timezone_function_exception(self, date, timezone, zone):
     tzaware_dt = localize_timezone(date, timezone)
     self.assertEqual(tzaware_dt.tzinfo._StaticTzInfo__name, zone)
Example #9
0
 def test_localize_timezone_function(self, date, timezone, zone):
     tzaware_dt = localize_timezone(date, timezone)
     self.assertEqual(tzaware_dt.tzinfo.zone, zone)