Module: Vishnu::StandardTime

Defined in:
lib/vishnu/standard_time.rb

Defined Under Namespace

Classes: Parts

Constant Summary collapse

VST_SPEC_VERSION =
"0.1.0"
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

Class Method Details

.comma6(value) ⇒ Object



80
81
82
83
# File 'lib/vishnu/standard_time.rb', line 80

def comma6(value)
  s = value.to_i.to_s.rjust(6, "0")
  "#{s[0, 3]},#{s[3, 3]}"
end

.format_flow_count(parts) ⇒ Object



85
86
87
# File 'lib/vishnu/standard_time.rb', line 85

def format_flow_count(parts)
  "#{comma6(parts.flow)} / 259,200 flows"
end

.format_flow_of_minute(parts) ⇒ Object



89
90
91
# File 'lib/vishnu/standard_time.rb', line 89

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



93
94
95
# File 'lib/vishnu/standard_time.rb', line 93

def format_percent_of_day(parts, digits = 2)
  "%0#{digits + 3}.#{digits}f%% OF DAY" % parts.percent_of_day
end

.format_vst(parts) ⇒ Object



97
98
99
# File 'lib/vishnu/standard_time.rb', line 97

def format_vst(parts)
  "VST #{format_vst_day(parts)} · #{format_flow_count(parts)} · #{format_percent_of_day(parts)}"
end

.format_vst_day(parts) ⇒ Object



75
76
77
78
# File 'lib/vishnu/standard_time.rb', line 75

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



101
102
103
# File 'lib/vishnu/standard_time.rb', line 101

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



37
38
39
# File 'lib/vishnu/standard_time.rb', line 37

def from_day_ms(day, day_ms)
  parts_from_day_ms(Integer(day), Integer(day_ms))
end

.from_unix_ms(unix_ms) ⇒ Object



31
32
33
34
35
# File 'lib/vishnu/standard_time.rb', line 31

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



63
64
65
66
67
68
69
# File 'lib/vishnu/standard_time.rb', line 63

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

.nowObject



59
60
61
# File 'lib/vishnu/standard_time.rb', line 59

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

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/vishnu/standard_time.rb', line 41

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

.to_h(parts) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/vishnu/standard_time.rb', line 105

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



71
72
73
# File 'lib/vishnu/standard_time.rb', line 71

def to_unix_ms(parts)
  VST_EPOCH_UNIX_MS + parts.day * MS_PER_DAY + parts.day_ms
end