Skip to content

Instantly share code, notes, and snippets.

@danilo-bc
Created April 16, 2024 23:52
Show Gist options
  • Select an option

  • Save danilo-bc/4e7d3f3e344101b226a7c3bfe6b8f31a to your computer and use it in GitHub Desktop.

Select an option

Save danilo-bc/4e7d3f3e344101b226a7c3bfe6b8f31a to your computer and use it in GitHub Desktop.
Transform a double into scientific time string (ps, ns, ms, etc)
def scientific_time(time_in_seconds):
"""String of time converted to closest scientific representation
Args:
time_in_seconds (double): time in seconds
Returns:
str: Converted time. e.g., 0.005 => 5 ms
"""
powers = [(1e-12, 'ps'), (1e-9, 'ns'), (1e-6, 'μs'), (1e-3, 'ms'), (1, 's'), (60, 'min'), (3600, 'hr'), (86400, 'day')]
closest_power = min(powers, key=lambda x: abs(time_in_seconds - x[0]))
return f"{time_in_seconds / closest_power[0]} {closest_power[1]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment