Template request | Bug report | Generate Data Product
Tags: #python #convert #units #snippet #operations #time
Author: Benjamin Filly
Description: This notebook shows you how to convert time using Python.
References:
value
: starting unit valuefrom_unit
: is the unit of the starting value, the one you want to convertto_unit
: this is the unit we want to achieve with this script
value = 3600
from_unit = 's'
to_unit = 'h'
def convert_time(value, from_unit, to_unit):
units = {
's': 1.0,
'min': 60.0,
'h': 3600.0,
'day': 86400.0,
'week': 604800.0,
'month': 2628000.0,
'year': 31536000.0
# Add other time units here
}
if from_unit not in units or to_unit not in units:
raise ValueError("Invalid unit.")
return value * units[from_unit] / units[to_unit]
result = convert_time(value, from_unit, to_unit)
result