Skip to content

Instantly share code, notes, and snippets.

@thomasjslone
Created May 2, 2023 22:58
Show Gist options
  • Save thomasjslone/69c1774f27ec172fb52b3bdfa79ccd2a to your computer and use it in GitHub Desktop.
Save thomasjslone/69c1774f27ec172fb52b3bdfa79ccd2a to your computer and use it in GitHub Desktop.
most popular copy paste time methods
Time.class.class.class_eval{
def parse_seconds(s)
s = s.to_i
if s < 60 ; [0, 0, s]
elsif s < 3600 ; [0, s / 60, s % 60]
elsif s < 86400 ; [s / 3600, (s / 60) % 60, s % 60]
else
days = s / 86400
hours = (s / 3600) % 24
minutes = (s / 60) % 60
seconds = s % 60
[days, hours, minutes, seconds]
end
end
def stamp(time = Time.now, delimiter = '.')
if time.is_a?(Time)
[time.year.to_s, format("%02d", time.month), format("%02d", time.day),
format("%02d", time.hour), format("%02d", time.min), format("%02d", time.sec)].join(delimiter)
elsif time.is_a?(String)
t = time.split(delimiter)
Time.new(t[0], t[1], t[2], t[3], t[4], t[5])
end
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment