fuzzy-date
Python module to convert various time strings into datetime objects, written in Rust.
Date conversion
import fuzzydate as fd
fd.to_datetime('1 hour ago')
fd.to_datetime('last week')
fd.to_datetime('past 2 weeks')
fd.to_datetime('-1 week')
fd.to_datetime('tuesday next week')
fd.to_datetime('last week midnight')
fd.to_datetime('-1d 2h 5min 10s')
fd.to_datetime('tomorrow')
fd.to_datetime('prev Monday')
fd.to_datetime('prev June')
fd.to_datetime('last day of this month')
fd.to_datetime('next Summer')
Time duration
Duration seconds
import fuzzydate as fd
fd.to_seconds('1h 4min')
fd.to_seconds('+2 days')
fd.to_seconds('-1 hour')
fd.to_seconds('1 week')
fd.to_seconds('last week')
fd.to_seconds('1m 2w 30min')
Duration string
import fuzzydate as fd
fd.to_duration(3840.0)
fd.to_duration(3840.0, units='long')
fd.to_duration(3840.0, units='short')
fd.to_duration(3840.0, max='min', min='min')
Localization
import fuzzydate as fd
fd.config.add_tokens({
'måndag': fd.token.WDAY_MON,
'dagar': fd.token.LONG_UNIT_DAY,
})
fd.config.add_patterns({
'nästa [wday]': fd.pattern.NEXT_WDAY,
})
assert fd.to_date('next Monday') == fd.to_date('nästa Måndag')
assert fd.to_date('+5 days') == fd.to_date('+5 dagar')
assert fd.to_seconds('+5 days') == fd.to_seconds('+5 dagar')
fd.config.units = {
fd.unit.DAY: 'dag',
fd.unit.DAYS: 'dagar',
}
assert fd.to_duration(86400.0) == '1 dag'
Requirements
Installation
pip install fuzzy-date
Syntax support
Special
- Date
now
, today
, tomorrow
, yesterday
- Time of day
midnight
Relative
- Adjustment
first
, last
, prev
, past
, this
, next
or +
, -
- Units
next week
, next month
, next year
- Weekdays
next Mon
, next Monday
, Monday
- Months
next Jan
, next January
, January
- Numeric
(s)ec
, min
, (h)r
, (d)ay
, (w)eek
, (m)onth
, (y)ear
- Ranges
first/last day of
, first/last Monday of
Fixed
- Unix timestamp
@1680307200
- Date
- Numeric
2023-04-01
, 20230401
, 04/01/2023
, 01.04.2023
- Textual
April 1st 2023
, April 1 2023
, 1 April 2023
, 1. April 2023
- Combined
01-April-2023
, April-01-2023
, 2023-April-01
- Day and month
- Textual
April 1st
, April 1
, 1 April
, 1. April
, 1st of April
- With weekday
Sat, 1 April
, Sat, 1st of April
, Sat, April 1st
, Sat, April 1
- Week
- Numeric
2023W13
, 2023-W13
- Textual
Week 13
, Week 13, 2023
- Month and year
April
, April 2023
- Year
2023
- Datetime
Sat Apr 01 12:00:00 2023
, 2023-04-01T12:00:00
, 2023-04-01T12:00.410
- Time of day
14:00:00
, 14:00:00.410
, 2pm
, 2:00 pm
Methods
Conversion
fuzzydate.to_date(
source: str,
today: datetime.date = None,
weekday_start_mon: bool = True) -> datetime.date
fuzzydate.to_datetime(
source: str,
now: datetime.datetime = None,
weekday_start_mon: bool = True) -> datetime.datetime
fuzzydate.to_duration(
seconds: float,
units: str = None,
max: str = 'w',
min: str = 's') -> str
fuzzydate.to_seconds(
source: str) -> float
Configuration
fuzzydate.config.patterns: dict[str, str]
fuzzydate.config.tokens: dict[str, int]
fuzzydate.config.units: dict[str, str]
fuzzydate.config.units_long: dict[str, str]
fuzzydate.config.units_short: dict[str, str]
fuzzydate.config.add_patterns(
tokens: dict[str, str]) -> None
fuzzydate.config.add_tokens(
tokens: dict[str, int]) -> None
Background
This library was born out of the need to accept various user inputs for date range start and end
times, to convert user time tracking entries into exact durations etc. All very much alike to what
timelib does.
Other implementations are available, but I did not find one that would have worked for me - usually
they were missing support for some key wording I needed, or handled user vagueness and timezones in
a different way.
Also, I kinda wanted to learn Rust via some example project as well.
License
MIT