Parse a human readable time string into seconds or a specified return unit.
Orinigal source code: npm i timestring (Javascript)
pip install git+https://github.com/The-LukeZ/TimestringPy
import timestring
value = '1h 15m'
time = timestring.parse_timestring(value)
print(time) # will print 4500
By default the returned time value from timestring
will be a float
.
The time string can contain as many time groups as needed:
import timestring
value = '1d 3h 25m 18s'
time = timestring.parse_timestring(value)
print(time) # will print 98718
and can be as messy as you like:
import timestring
value = '1 d 3HOurS 25 min 1 8s'
time = timestring.parse_timestring(value)
print(time) # will print 98718
timestring
will parse the following default keywords into time values:
ms, milli, millisecond, milliseconds
- will parse to millisecondss, sec, secs, second, seconds
- will parse to secondsm, min, mins, minute, minutes
- will parse to minutesh, hr, hrs, hour, hours
- will parse to hoursd, day, days
- will parse to daysw, week, weeks
- will parse to weeksmon, mth, mths, month, months
- will parse to monthsy, yr, yrs, year, years
- will parse to years
Keywords can be used interchangeably:
import timestring
value = '1day 15h 20minutes 15s'
time = timestring.parse_timestring(value)
print(time) # will print 141615
By default the return time value will be in seconds. This can be changed by passing one of the strings form the default time-units or an element from the unit_map
-parameter:
ms
- Millisecondss
- Secondsm
- Minutesh
- Hoursd
- Daysw
- Weeksmth
- Monthsy
- Years
value = '22h 16m'
hours = timestring.parse_timestring(value, 'h')
days = timestring.parse_timestring(value, 'd')
weeks = timestring.parse_timestring(value, 'w')
print(hours) # will print 22.266666666666666
print(days) # will print 0.9277777777777778
print(weeks) # will print 0.13253968253968254
A few assumptions are made by default:
- There are 24 hours per day
- There are 7 days per week
- There are 4 weeks per month
- There are 12 months per year
- There are 365.25 days per year
These options can be changed by passing an dict
to the opts
-parameter.
The following options are configurable:
hoursPerDay
daysPerWeek
weeksPerMonth
monthsPerYear
daysPerYear
import timestring
value = '1d'
opts = {
'hoursPerDay': 1
}
time = timestring.parse_timestring(value, 'h', opts=opts) # 'h' because we want the number of hours
print(time) # will print 1.0
In the example above hoursPerDay
is being set to 1
. When the time string is being parsed, the return value is being specified as hours. Normally 1d
would parse to the number of seconds in one day à 24h
(as by default there are 24 hours in a day) but because hoursPerDay
has been set to 1
, 1d
will now only parse to the number of seconds in 1
hour (aka 1d
here).
This would be useful for specific application needs.
Example - Employees of my company work 7.5 hours a day, and only work 5 days a week. In my time tracking app, when they type 1d
i want 7.5 hours to be tracked. When they type 1w
i want 5 days to be tracked etc.
import timestring
opts = {
'hoursPerDay': 7.5,
'daysPerWeek': 5
}
hoursToday = timestring.parse_timestring('1d', 'h', opts)
daysThisWeek = timestring.parse_timestring('1w', 'd', opts)
print(hoursToday) # will print 7.5
print(daysThisWeek) # will print 5.0
You can also pass your own time units to make more languages available.
Example
You have the same example as above, but now you want your German users to type '1 Tag' instead of '1 day' (they may not know the wording), but you want the hours of the day and the amount of days they typed in.
value = '1 tag'
units = {
"d": ["tag", "d", "day", "days"]
}
opts = {
'hoursPerDay': 7.5,
'daysPerWeek': 5
}
hoursToday = timestring.parse_timestring(value, 'h', opts, units)
daysThisWeek = timestring.parse_timestring(value, 'd', opts, units)
print(hoursToday) # will print 7.5 (7.5 hours)
print(daysThisWeek) # will print 1.0 (1 day)