You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Date/time library built on Pendulum with business day support and financial calculations. Parsing is 1.2-6x faster than pendulum thanks to a Rust-based dateutil port.
pip install opendate
Quick Reference
# Preferred import (using canonical module name)importopendatefromopendateimportDate, DateTime, Time, IntervalfromopendateimportEST, UTC, LCL, WeekDayfromopendateimportget_calendar, set_default_calendar# Basicstoday=Date.today()
now=DateTime.now()
parsed=Date.parse('T-3b') # 3 business days ago# Business daystoday.b.add(days=5) # 5 business days forwardtoday.calendar('LSE').b.subtract(days=3) # Using London calendar# Change default calendar globallyset_default_calendar('LSE')
Module Functions
Function
Description
date(y, m, d)
Create Date
datetime(y, m, d, h, m, s, tz=UTC)
Create DateTime (defaults to UTC)
time(h, m, s, tz=UTC)
Create Time (defaults to UTC)
interval(start, end)
Create Interval
parse(s, calendar=None)
Parse to DateTime (calendar optional, uses module default)
instance(obj)
Convert datetime/date/time (preserves obj's tz if present, else UTC)
now(tz=None)
Current DateTime (local tz if None)
today(tz=None)
Today at 00:00:00 (local tz if None)
get_calendar(name)
Get calendar instance
set_default_calendar(name)
Set module default calendar
get_default_calendar()
Get current default calendar name
available_calendars()
List available calendars
Date
Constructors
Method
Description
Date(y, m, d)
Create from components
Date.today()
Current date (uses local tz for current day)
Date.parse(s, calendar='NYSE')
Parse string. Calendar used for business day codes (T-3b, P), defaults to NYSE
Same as Date: add, subtract, diff, start_of, end_of, first_of, last_of, nth_of, next, previous
Business Day
Same as Date: business(), .b, calendar(name), is_business_day(), business_hours()
Formatting
Method
Description
format(fmt, locale=None)
Format with tokens
isoformat()
ISO 8601 string
rfc3339()
RFC 3339 (same as isoformat)
to_date_string()
YYYY-MM-DD
to_datetime_string()
YYYY-MM-DD HH:MM:SS
to_time_string()
HH:MM:SS
to_iso8601_string()
Full ISO 8601
Time
Constructors
Method
Description
Time(h, m, s, us, tzinfo)
Create from components
Time.parse(s, fmt=None)
Parse string (always UTC)
Time.instance(obj, tz=None)
From datetime.time, datetime. Preserves obj's tz if present, else uses tz param, else UTC
Properties
hour, minute, second, microsecond, tzinfo
Methods
Method
Description
in_timezone(tz)
Convert to timezone
in_tz(tz)
Alias
Interval
Constructor
Interval(start, end) # start/end are Date or DateTime
Properties
Property
Description
days
Calendar days (business days if .b)
months
Float with fractional months (differs from pendulum)
years
Complete years (floors)
quarters
Approximate (days/365*4)
start
Start date
end
End date
Methods
Method
Description
business() or .b
Enable business day mode
calendar(name)
Set calendar
range(unit, amount=1)
Iterate by unit (days/weeks/months/years)
is_business_day_range()
Yields bool for each day
start_of(unit)
List of period starts in interval
end_of(unit)
List of period ends in interval
yearfrac(basis)
Year fraction (Excel-compatible)
yearfrac Basis
Basis
Convention
Use
0
US (NASD) 30/360
Corporate bonds
1
Actual/actual
Treasury bonds
2
Actual/360
Money market
3
Actual/365
Some bonds
4
European 30/360
Eurobonds
5
Actual/365.25
Avg year length
Parsing
OpenDate uses a dateutil-compatible Rust parser that handles virtually any date/time format. The parser supports fuzzy matching, multiple locales, and automatic format detection.
Special Codes
Code
Meaning
T or N
Today
Y
Yesterday
P
Previous business day
M
Last day of previous month
Business Day Offsets
Pattern
Example
Meaning
{code}±{n}
T-5
5 calendar days ago
{code}±{n}b
T-3b
3 business days ago
Parser Capabilities
Dates: ISO 8601, US/European formats, compact, natural language
Combined: Any date + time combination, Unix timestamps (auto-detects ms/s)
Fuzzy: Extracts dates from text containing other content
# All of these workDate.parse('2024-01-15')
Date.parse('Jan 15, 2024')
Date.parse('15/01/2024')
DateTime.parse('2024-01-15T09:30:00Z')
DateTime.parse(1640995200) # Unix timestampDateTime.parse('meeting on Jan 15 at 3pm') # Fuzzy
# Add/subtract business daysDate(2024, 3, 29).b.add(days=1) # Skips Good Friday + weekend# Period boundariesDate(2024, 7, 1).b.start_of('month') # First business day of JulyDate(2024, 4, 30).b.end_of('month') # Last business day of April# Iterate business days onlyfordinInterval(start, end).b.range('days'):
print(d)
# Count business daysInterval(start, end).b.days
fromopendateimportis_business_day, is_within_business_hours, overlap_daysis_business_day() # Is today a business day?is_within_business_hours() # Is current time in market hours?overlap_days(int1, int2) # Do intervals overlap? (bool)overlap_days(int1, int2, days=True) # Day count of overlap (int)