Class: Float
- Inherits:
-
Object
- Object
- Float
- Defined in:
- lib/tago.rb
Overview
A new method to print time as text.
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2024-2025 Yegor Bugayenko
- License
-
MIT
Instance Method Summary collapse
Instance Method Details
#seconds(*args) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/tago.rb', line 14 def seconds(*args) s = self s = -s if s.negative? if args.include?(:pretty) locales = { en: { numbers: { 0 => 'zero', 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six', 7 => 'seven', 8 => 'eight', 9 => 'nine', 10 => 'ten' }, units: { microsecond: %w[microsecond microseconds], millisecond: %w[millisecond milliseconds], second: %w[second seconds], minute: %w[minute minutes], hour: %w[hour hours], day: %w[day days], week: %w[week weeks] }, short_units: { microsecond: 'μs', millisecond: 'ms', second: 'sec', minute: 'min', hour: 'hr', day: 'd', week: 'wk' } } }.freeze if s < 0.001 val = (s * 1_000_000).to_i unit = :microsecond elsif s < 1 val = (s * 1000).to_i unit = :millisecond elsif s < 60 val = s.to_i unit = :second elsif s < 60 * 60 val = (s / 60).to_i unit = :minute elsif s < 24 * 60 * 60 val = (s / (60 * 60)).to_i unit = :hour elsif s < 7 * 24 * 60 * 60 val = (s / (24 * 60 * 60)).to_i unit = :day else val = (s / (7 * 24 * 60 * 60)).to_i unit = :week end short = args.include?(:short) number_to_words = val <= 10 ? locales[:en][:numbers][val] : val.to_s num = short ? val : number_to_words names = locales[:en][:units][unit] unit_symbol = val == 1 ? names[0] : names[1] unit_name = short ? locales[:en][:short_units][unit] : unit_symbol return format('%<num>s %<unit_name>s', num:, unit_name:) end if s < 0.001 format('%dμs', s * 1000 * 1000) elsif s < 1 format('%dms', s * 1000) elsif s < 10 ms = (s * 1000 % 1000).to_i if args.include?(:round) || args.include?(:short) || ms.zero? format('%ds', s) else format('%<s>ds%<ms>dms', s:, ms:) end elsif s < 100 format('%ds', s) elsif s < 60 * 60 mins = (s / 60).to_i secs = (s % 60).to_i if args.include?(:round) || args.include?(:short) || secs.zero? format('%dm', mins) else format('%<mins>dm%<secs>ds', mins:, secs:) end elsif s < 24 * 60 * 60 hours = (s / (60 * 60)).to_i mins = ((s % (60 * 60)) / 60).to_i if args.include?(:round) || args.include?(:short) || mins.zero? format('%dh', hours) else format('%<hours>dh%<mins>dm', hours:, mins:) end elsif s < 7 * 24 * 60 * 60 days = (s / (24 * 60 * 60)).to_i hours = ((s % (24 * 60 * 60)) / (60 * 60)).to_i if args.include?(:round) || args.include?(:short) || hours.zero? format('%dd', days) else format('%<days>dd%<hours>dh', days:, hours:) end else weeks = (s / (7 * 24 * 60 * 60)).to_i days = (s / (24 * 60 * 60) % 7).to_i if args.include?(:round) || args.include?(:short) || days.zero? format('%dw', weeks) else format('%<weeks>dw%<days>dd', weeks:, days:) end end end |