How to use the parsedatetime.Constants function in parsedatetime

To help you get started, we’ve selected a few parsedatetime examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bear / parsedatetime / tests / TestFrenchLocale.py View on Github external
def setUp(self):
        locale = 'fr_FR'
        self.ptc = pdt.Constants(locale, usePyICU=False)
        self.cal = pdt.Calendar(self.ptc)

        (self.yr, self.mth, self.dy, self.hr,
         self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime()

        if self.ptc.localeID != locale:
            raise unittest.SkipTest(
                'Locale not set to fr_FR - check if PyICU is installed')
github bear / parsedatetime / tests / TestLocaleBase.py View on Github external
def setUp(self):
        self.ptc = pdt.Constants('fr_FR', usePyICU=True)
        self.cal = pdt.Calendar(self.ptc)

        (self.yr, self.mth, self.dy, self.hr,
         self.mn, self.sec, self.wd, self.yd, self.isdst) = time.localtime()

        if self.ptc.localeID != 'fr_FR':
            raise unittest.SkipTest(
                'Locale not set to fr_FR - check if PyICU is installed')
github alephdata / aleph / aleph / data / parse.py View on Github external
def fuzzy_date_parser(text):
    """Thin wrapper around ``parsedatetime`` module.

    Since there's no upstream suppport for multiple locales, this wrapper
    exists.

    :param str text: Text to parse.
    :returns: A parsed date/time object. Raises exception on failure.
    :rtype: datetime
    """
    locales = parsedatetime._locales[:]

    # Loop through all the locales and try to parse successfully our string
    for locale in locales:
        const = parsedatetime.Constants(locale)
        const.re_option += re.UNICODE
        parser = parsedatetime.Calendar(const)
        try:
            parsed, ok = parser.parse(text)
        except:
            continue

        if ok:
            return datetime(*parsed[:6])

    raise Exception('Failed to parse the string.')
github bear / parsedatetime / examples / basic.py View on Github external
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import parsedatetime as pdt

# create an instance of Constants class so we can override some of the defaults

c = pdt.Constants()

c.BirthdayEpoch = 80    # BirthdayEpoch controls how parsedatetime
                        # handles two digit years.  If the parsed
                        # value is less than this value then the year
                        # is set to 2000 + the value, otherwise
                        # it's set to 1900 + the value

# create an instance of the Calendar class and pass in our Constants
# object instead of letting it create a default

p = pdt.Calendar(c)

# parse "tomorrow" and return the result

result = p.parse("tomorrow")
github ThreatConnect-Inc / tcex / tcex / utils / date_utils.py View on Github external
* in 5 minutes
        * 5 minutes from now
        * 5 hours before now
        * 2 hours before noon
        * 2 days from tomorrow

        Args:
            time_input (string): The time input string (see formats above).
            tz (string): The time zone for the returned datetime.
            source_datetime (datetime.datetime): The reference or source datetime.

        Returns:
            (datetime.datetime): Python datetime.datetime object.
        """

        c = pdt.Constants('en')
        cal = pdt.Calendar(c, version=2)
        tzinfo = None
        src_tzname = None
        if source_datetime is not None:
            tzinfo = source_datetime.tzinfo
            src_tzname = source_datetime.tzname()
        try:
            dt, status = cal.parseDT(time_input, sourceTime=source_datetime, tzinfo=tzinfo)
            if tz is not None:  # don't add tz if no tz value is passed
                if dt.tzinfo is None:
                    dt = self._replace_timezone(dt)
                # don't covert timezone if source timezone already in the correct timezone
                if tz != src_tzname:
                    dt = dt.astimezone(pytz.timezone(tz))
            if status.accuracy == 0:
                dt = None
github idpaterson / alfred-wunderlist-workflow / src / wunderlist / models / task_parser.py View on Github external
def _parse(self):
		phrase = self.phrase
		loc = locale.getlocale(locale.LC_TIME)[0]

		if not loc:
			# In some instances, getdefaultlocale() has been known to throw an
			# exception due apparently to no locale being set wherein UTF-8 is
			# attempted to substitute for the locale
			try:
				loc = locale.getdefaultlocale()[0]
			except:
				loc = 'en_US'

		c = Constants(loc)
		cal = Calendar(c)
		wf = workflow()
		lists = wf.stored_data('lists')

		match = re.search(_star_pattern, phrase)
		if match:
			self.starred = True
			self._starred_phrase = match.group()
			phrase = re.sub(_star_pattern, '', phrase)

		match = re.search(_list_title_pattern, phrase, re.IGNORECASE)
		if lists and match:
			if match.group(1):
				matching_lists = wf.filter(
					match.group(1),
					lists,
github bear / parsedatetime / examples / with_locale.py View on Github external
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import parsedatetime as pdt

# create an instance of Constants class so we can specify the locale

c = pdt.Constants("en")
p = pdt.Calendar(c)

# print out the values from Constants to show how the locale information
# is being used/stored internally

values = (c.uses24,                 # 24hr clock?
          c.usesMeridian,           # AM/PM used?
          c.usePyICU,               # was PyICU found/enabled?
          c.meridian,               # list of the am and pm values
          c.am,                     # list of the lowercase and stripped AM string
          c.pm,                     # list of the lowercase and stripped PM string
          c.dateFormats,            # dict of available date format strings
          c.timeFormats,            # dict of available time format strings
          c.timeSep,                # list of time separator, e.g. the ':' in '12:45'
          c.dateSep,                # list of date separator, e.g. the '/' in '11/23/2006'
          c.Months,                 # list of full month names