Understand the Calendar class ptc property
masterCalendar class contains a member property named ptc. This property is initialized during the class __init__ method and is an instance of parsedatetime_consts.CalendarConstants().repository·master·Indexed 20 days ago
https://github.com/bear/parsedatetimeA Python library for parsing human-readable date and time strings into structured Python datetime objects or tuples. It provides a Calendar class with methods like .parse() for time tuples and .parseDT() for timezone-aware datetime objects, with support for PyICU and built-in locale classes.
Calendar class contains a member property named ptc. This property is initialized during the class __init__ method and is an instance of parsedatetime_consts.CalendarConstants().You can install the parsedatetime library using pip. The current version targets Python 3 (tested with Python 3.9). If you require Python 2.7 compatibility, use the v2.6 release.
pip install parsedatetimewith_pyicu.py example demonstrates how to integrate parsedatetime with the PyICU library. This is useful for advanced internationalization and locale-specific date parsing capabilities provided by ICU.To parse a string with specific timezone awareness, use the .parseDT() method. Pass the datetimeString and a tzinfo object (e.g., from the pytz library). This method returns a datetime object and a parse status.
import parsedatetime
from pytz import timezone
cal = parsedatetime.Calendar()
datetime_obj, _ = cal.parseDT(datetimeString="tomorrow", tzinfo=timezone("US/Pacific"))The basic.py example demonstrates the standard workflow for using parsedatetime to parse natural language date/time strings into Python datetime objects. This typically involves initializing a Calendar object and using its parsing methods.
# Refer to basic.py for the implementation details
# Typical pattern:
# from parsedatetime import Calendar
# cal = Calendar()
# time_struct, parse_status = cal.parse('tomorrow at 5pm')To parse a human-readable string into a time tuple, instantiate a parsedatetime.Calendar() object and use its .parse() method. This returns a tuple representing the time structure and a parse status.
import parsedatetime
cal = parsedatetime.Calendar()
time_struct, parse_status = cal.parse("tomorrow")with_locale.py example shows how to use parsedatetime alongside its built-in locale classes to handle date parsing for different languages and cultural formats.To convert a parsed string directly into a standard Python datetime object, use the time tuple returned by cal.parse() and unpack it into the datetime constructor.
import parsedatetime
from datetime import datetime
cal = parsedatetime.Calendar()
time_struct, parse_status = cal.parse("tomorrow")
# Unpack the first 6 elements of the tuple into datetime
datetime_obj = datetime(*time_struct[:6])