Module: Jade::Stdlib::Text
Overview
Text representations of stdlib types, read in Ruby. Lives here rather than in Decimal / Clock / Calendar: those import Decode, so the instance cannot be registered the other way round. The types are compiled Jade, so constants resolve on first use.
Accepts exactly what the Jade parsers did, down to String.to_int
being Integer(s, 10) — whitespace and underscores included.
Constant Summary collapse
- EPOCH_RATA_DIE =
719163- DAY_MS =
86_400_000
Instance Method Summary collapse
-
#from_instant(instant) ⇒ Object
Seconds precision, always UTC.
- #to_date(s) ⇒ Object
- #to_decimal(s) ⇒ Object
-
#to_instant(s) ⇒ Object
No timezone offsets:
Clock.from_isonever took them either.
Instance Method Details
#from_instant(instant) ⇒ Object
Seconds precision, always UTC. rjust matches pad_to, which
left-pads without truncating: a negative year renders "00-5".
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/jade/stdlib/text.rb', line 63 def from_instant(instant) ms = instant._1 day_ms = ms % DAY_MS d = Jade::Calendar::Internal.from_rata_die(ms / DAY_MS + EPOCH_RATA_DIE) [ pad(d.year, 4), '-', pad(Jade::Calendar::Internal.month_to_int(d.month), 2), '-', pad(d.day, 2), 'T', pad(day_ms / 3_600_000, 2), ':', pad(day_ms % 3_600_000 / 60_000, 2), ':', pad(day_ms % 60_000 / 1_000, 2), 'Z', ].join end |
#to_date(s) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/jade/stdlib/text.rb', line 44 def to_date(s) parts = s.split('-') return nil unless parts.length == 3 year, month, day = parts.map { int(it) } return nil unless year && month && day # Answers Jan outside 1..12 and never validates the day — calling # it keeps that. Don't cache the Months: a second compile # redefines the class. Jade::Calendar::Date[ year, Jade::Calendar::Internal.month_from_int(month), day, ] end |
#to_decimal(s) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/jade/stdlib/text.rb', line 16 def to_decimal(s) coefficient, exponent = s.split('e', 2) return nil unless exponent m = int(coefficient) e = int(exponent) m && e ? Jade::Decimal::Decimal[m, e] : nil end |
#to_instant(s) ⇒ Object
No timezone offsets: Clock.from_iso never took them either.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/jade/stdlib/text.rb', line 27 def to_instant(s) date_part, time_part = split_dt(s) return nil unless date_part d = to_date(date_part) time = split_time(time_part) return nil unless d && time hour, minute, second, sub_ms = time days = Jade::Calendar::Internal.to_rata_die(d) - EPOCH_RATA_DIE Jade::Clock::Instant[ days * DAY_MS + hour * 3_600_000 + minute * 60_000 + second * 1_000 + sub_ms ] end |