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 TimestringPy
# or
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.2.1.tar.gz (7.0 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.2.1-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for timestringpy-1.2.1.tar.gz
Algorithm Hash digest
SHA256 1cded87f262dc9b9518e007fcf540dd90396a29a47a816170a2f78d6f4dceebd
MD5 11788a647d8647f9f32d22d0dede4dc3
BLAKE2b-256 97a62cabd535fb1cc084015c59636531fb2e5e1388a9c2b238d66bd0d9729018

See more details on using hashes here.

File details

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

File metadata

  • Download URL: timestringpy-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 7.5 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.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 333f400e2bd962b0ad4b2078262d90a803e88e33a0b6ae39a56e0a724502920b
MD5 675ebcd73112eb471ee7a35aec8b6d0f
BLAKE2b-256 a2f8996a1d10684af68920fe9402bd128919fa4cc978fac9f3c17818a47c3b12

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