Module: Vishnu::StandardTime
- Defined in:
- lib/vishnu/standard_time.rb
Defined Under Namespace
Classes: Parts
Constant Summary collapse
- WEBSITE_URL =
"https://time.vishnu.store".freeze
- STANDARD_NAME =
"Vishnu Standard Time".freeze
- VST_SPEC_VERSION =
"0.1.1"- VST_EPOCH_UNIX_MS =
1_767_225_600_000- MS_PER_DAY =
86_400_000- FLOWS_PER_SECOND =
3- FLOWS_PER_MINUTE =
180- FLOWS_PER_DAY =
259_200- FLOW_LENGTH_MS_TEXT =
"333⅓"- VICKS_PER_DAY =
100_000- MS_PER_VICK =
864
Class Method Summary collapse
- .comma6(value) ⇒ Object
- .format_flow_count(parts) ⇒ Object
- .format_flow_of_minute(parts) ⇒ Object
- .format_percent_of_day(parts, digits = 2) ⇒ Object
- .format_vst(parts) ⇒ Object
- .format_vst_day(parts) ⇒ Object
- .format_vst_precise(parts) ⇒ Object
- .from_day_ms(day, day_ms) ⇒ Object
- .from_unix_ms(unix_ms) ⇒ Object
- .local_now(time = Time.now) ⇒ Object
- .now ⇒ Object
- .parts_from_day_ms(day, day_ms, unix_ms: nil, source: "standard") ⇒ Object
- .standard_link ⇒ Object
- .to_h(parts) ⇒ Object
- .to_unix_ms(parts) ⇒ Object
- .website_url ⇒ Object
Class Method Details
.comma6(value) ⇒ Object
91 92 93 94 |
# File 'lib/vishnu/standard_time.rb', line 91 def comma6(value) s = value.to_i.to_s.rjust(6, "0") "#{s[0, 3]},#{s[3, 3]}" end |
.format_flow_count(parts) ⇒ Object
96 97 98 |
# File 'lib/vishnu/standard_time.rb', line 96 def format_flow_count(parts) "#{comma6(parts.flow)} / 259,200 flows" end |
.format_flow_of_minute(parts) ⇒ Object
100 101 102 |
# File 'lib/vishnu/standard_time.rb', line 100 def format_flow_of_minute(parts) "FLOW #{parts.flow_of_minute.to_s.rjust(3, "0")} / 179" end |
.format_percent_of_day(parts, digits = 2) ⇒ Object
104 105 106 |
# File 'lib/vishnu/standard_time.rb', line 104 def format_percent_of_day(parts, digits = 2) "%0#{digits + 3}.#{digits}f%% OF DAY" % parts.percent_of_day end |
.format_vst(parts) ⇒ Object
108 109 110 |
# File 'lib/vishnu/standard_time.rb', line 108 def format_vst(parts) "VST #{format_vst_day(parts)} · #{format_flow_count(parts)} · #{format_percent_of_day(parts)}" end |
.format_vst_day(parts) ⇒ Object
86 87 88 89 |
# File 'lib/vishnu/standard_time.rb', line 86 def format_vst_day(parts) sign = parts.day.negative? ? "-" : "+" "D#{sign}#{parts.day.abs.to_s.rjust(6, "0")}" end |
.format_vst_precise(parts) ⇒ Object
112 113 114 |
# File 'lib/vishnu/standard_time.rb', line 112 def format_vst_precise(parts) "VST #{format_vst_day(parts)} · #{format_flow_of_minute(parts)} · #{format_flow_count(parts)} · #{format_percent_of_day(parts)}" end |
.from_day_ms(day, day_ms) ⇒ Object
48 49 50 |
# File 'lib/vishnu/standard_time.rb', line 48 def from_day_ms(day, day_ms) parts_from_day_ms(Integer(day), Integer(day_ms)) end |
.from_unix_ms(unix_ms) ⇒ Object
42 43 44 45 46 |
# File 'lib/vishnu/standard_time.rb', line 42 def from_unix_ms(unix_ms) ms = Integer(unix_ms) day, day_ms = (ms - VST_EPOCH_UNIX_MS).divmod(MS_PER_DAY) parts_from_day_ms(day, day_ms, unix_ms: ms, source: "standard") end |
.local_now(time = Time.now) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/vishnu/standard_time.rb', line 74 def local_now(time = Time.now) day_ms = ((time.hour * 60 + time.min) * 60 + time.sec) * 1000 + (time.usec / 1000) local_midnight_as_utc = Time.utc(time.year, time.month, time.day) epoch = Time.at(VST_EPOCH_UNIX_MS / 1000, in: "+00:00") day = ((local_midnight_as_utc - epoch) / 86_400).floor parts_from_day_ms(day, day_ms, unix_ms: (time.to_f * 1000).floor, source: "local") end |
.now ⇒ Object
70 71 72 |
# File 'lib/vishnu/standard_time.rb', line 70 def now from_unix_ms((Time.now.to_f * 1000).floor) end |
.parts_from_day_ms(day, day_ms, unix_ms: nil, source: "standard") ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/vishnu/standard_time.rb', line 52 def parts_from_day_ms(day, day_ms, unix_ms: nil, source: "standard") raise ArgumentError, "day_ms must be from 0 through 86399999" if day_ms.negative? || day_ms >= MS_PER_DAY flow = (day_ms * FLOWS_PER_DAY) / MS_PER_DAY Parts.new( day: day, day_ms: day_ms, flow: flow, flow_of_minute: flow % FLOWS_PER_MINUTE, second_of_day: day_ms / 1000, percent_of_day: (day_ms.to_f / MS_PER_DAY) * 100, vick: day_ms / MS_PER_VICK, pulse: day_ms % MS_PER_VICK, unix_ms: unix_ms, source: source ) end |
.standard_link ⇒ Object
12 13 14 |
# File 'lib/vishnu/standard_time.rb', line 12 def self.standard_link STANDARD_NAME + " — " + WEBSITE_URL end |
.to_h(parts) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/vishnu/standard_time.rb', line 116 def to_h(parts) { scale: "VST", version: VST_SPEC_VERSION, day: parts.day.to_s, flow: parts.flow, flow_of_minute: parts.flow_of_minute, flows_per_day: FLOWS_PER_DAY, percent_of_day: parts.percent_of_day.round(6), text: format_vst(parts), day_ms: parts.day_ms.to_s, vick: parts.vick, pulse: parts.pulse } end |
.to_unix_ms(parts) ⇒ Object
82 83 84 |
# File 'lib/vishnu/standard_time.rb', line 82 def to_unix_ms(parts) VST_EPOCH_UNIX_MS + parts.day * MS_PER_DAY + parts.day_ms end |
.website_url ⇒ Object
8 9 10 |
# File 'lib/vishnu/standard_time.rb', line 8 def self.website_url WEBSITE_URL end |