Skip to main content

Parse a timestring into a floating number.

Project description

TimestringPy

Parse a human readable time string into seconds or a specified return unit.

Orinigal source code: npm i timestring (Javascript)

Installation

pip install git+https://github.com/The-LukeZ/TimestringPy

Usage

Overview

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

Keywords

timestring will parse the following default keywords into time values:

  1. ms, milli, millisecond, milliseconds - will parse to milliseconds
  2. s, sec, secs, second, seconds - will parse to seconds
  3. m, min, mins, minute, minutes - will parse to minutes
  4. h, hr, hrs, hour, hours - will parse to hours
  5. d, day, days - will parse to days
  6. w, week, weeks - will parse to weeks
  7. mon, mth, mths, month, months - will parse to months
  8. y, 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

Return Time Value

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:

  1. ms - Milliseconds
  2. s - Seconds
  3. m - Minutes
  4. h - Hours
  5. d - Days
  6. w - Weeks
  7. mth - Months
  8. y - 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

Optional Configuration

A few assumptions are made by default:

  1. There are 24 hours per day
  2. There are 7 days per week
  3. There are 4 weeks per month
  4. There are 12 months per year
  5. 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:

  1. hoursPerDay
  2. daysPerWeek
  3. weeksPerMonth
  4. monthsPerYear
  5. 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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

timestringpy-1.1.3.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

timestringpy-1.1.3-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file timestringpy-1.1.3.tar.gz.

File metadata

  • Download URL: timestringpy-1.1.3.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for timestringpy-1.1.3.tar.gz
Algorithm Hash digest
SHA256 49245ecbcf3779a60eb330d08936ed9ed0b3f4b744332cbf509bfb9240a99c82
MD5 e68291cd0627f40a16d4de3e72f49d3b
BLAKE2b-256 d169e55e32d4a0aa0064ae0a5a6a6fcb12ac7c537fe85583141367794c486b64

See more details on using hashes here.

File details

Details for the file timestringpy-1.1.3-py3-none-any.whl.

File metadata

  • Download URL: timestringpy-1.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for timestringpy-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f5bcb1efc5ea75e9d1e7d3874b4ea8fb475515badabdaa59209fe307a4d1d623
MD5 4974b77831e51472338720c7eb729ef9
BLAKE2b-256 bdfc8e77ea7aa92f51049e1c5d0b99b6ec0b81a3c5fbf67434b9af11194b4f95

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page