Tag « conversion »

python, conversion

Convert string to duration

I found myself wanting to convert a string to a duration (int), for some configuration.

Something you can call like this:

string_to_duration("1d", target="days") # returns 1
string_to_duration("1d", target="hours") # returns 24
string_to_duration("3m", target="hours") # returns 3 * 24 * 30

The code :

`python from typing import Literal

def string_to_duration …