tsukimoji 0.1.2 β†’ 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +68 -51
  3. data/lib/tsukimoji.rb +229 -68
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: db5da3254050bfb603c97f5c55a0c990d8c49676af3df8baf054a08b6b83c08d
4
- data.tar.gz: e7824f345eb8a39f1002363a05bc6ef9470be1f82b431f07cdbdd93ecc0c2e9c
3
+ metadata.gz: 859e0295728438790d6d6b32ec895f2392ab4c6542f117938552b4399948fa53
4
+ data.tar.gz: 8ed389f6ffc23855ec35bee715323b0a49741d3ca22e977efe777696f88f12bb
5
5
  SHA512:
6
- metadata.gz: bd030575b8e4e7a7908a58ff44ad95fd903c8e9ec1d1e404889a094b1b591f4f3f22f8a3f4ee40dc2d5211e01a60745a665fa210b205cdeba20f5725cba37cdb
7
- data.tar.gz: ac14edf172c6166962bd112a0b8e64f35c6c4a31da149599ed47c4dcffd56f2fdca81de3f0a989d558b32af8b8f9f154fcdf7ec243172be545edd3aaf3149866
6
+ metadata.gz: 18c2ccb4c75fc9c07abc03d05fe88f3a1f72bec1858f145c5185be2b5808ddecf271d1b65dd4b2ec1c0899157a3d98fd2aee64e719a59ca27c64c353391d014a
7
+ data.tar.gz: 1a042009eaa4084aed001b0680ceba2d04e3bc4db99b7162edfd458325d2744406fe586db81d056fecf0dee87074d38c13fdb0fe95045f56f4a29c67e35b53ae
data/README.md CHANGED
@@ -1,51 +1,68 @@
1
- # tsukiMOJi
2
-
3
- Moon phase emoji for any date. 月 (tsuki, "moon") + moji, as in emoji.
4
-
5
- ```ruby
6
- require "tsukimoji"
7
-
8
- phase = Tsukimoji.phase
9
- phase.emoji # => "πŸŒ”"
10
- phase.name # => "Waxing Gibbous"
11
- phase.age_days # => 12.3
12
- phase.illumination # => 0.87
13
-
14
- Tsukimoji.emoji # just the emoji, for "now"
15
- Tsukimoji.phase(Time.utc(2026, 1, 1)) # phase for any specific time
16
-
17
- # Moon faces: 🌚 for New Moon and 🌝 for Full Moon
18
- Tsukimoji.emoji(Time.utc(2026, 10, 26, 4), faces: true) # => "🌝"
19
- ```
20
-
21
- ## Installation
22
-
23
- ```
24
- gem install tsukimoji
25
- ```
26
-
27
- Or add to your Gemfile:
28
-
29
- ```ruby
30
- gem "tsukimoji"
31
- ```
32
-
33
- ## How it works
34
-
35
- tsukiMOJi measures elapsed time since a known new moon (2000-01-06
36
- 18:14 UTC) against the synodic month (29.530588853 days) to find how far
37
- into the current lunar cycle a given moment falls, then maps that
38
- fraction onto the eight standard moon-phase emoji (πŸŒ‘πŸŒ’πŸŒ“πŸŒ”πŸŒ•πŸŒ–πŸŒ—πŸŒ˜).
39
- Illumination is derived separately from the same cycle position via a
40
- cosine curve, for display purposes.
41
-
42
- ## License
43
-
44
- MIT
45
-
46
- ---
47
-
48
- <p align="center">
49
- <img src="https://raw.githubusercontent.com/samlehman/tsukimoji/main/made-in-baltimore.png" alt="Made in Baltimore" width="88" align="middle">
50
- &nbsp;&nbsp;β˜• <a href="https://paypal.me/samlehman">Buy me a coffee</a> Β· <a href="https://paypal.me/samlehman">γ“γƒΌγ²γƒΌγ‚’γŠγ”γ£γ¦</a>
51
- </p>
1
+ # tsukiMOJi
2
+
3
+ Moon phase emoji for any date. 月 (tsuki, "moon") + moji, as in emoji.
4
+ In Japanese: γ€γγ‚‚γ˜.
5
+
6
+ ```ruby
7
+ require "tsukimoji"
8
+
9
+ phase = Tsukimoji.phase
10
+ phase.emoji # => "πŸŒ”"
11
+ phase.name # => "Waxing Gibbous"
12
+ phase.age_days # => 12.3
13
+ phase.illumination # => 0.87
14
+
15
+ Tsukimoji.emoji # just the emoji, for "now"
16
+ Tsukimoji.phase(Time.utc(2026, 1, 1)) # phase for any specific time
17
+
18
+ # Moon faces: 🌚 for New Moon and 🌝 for Full Moon
19
+ Tsukimoji.emoji(Time.utc(2026, 10, 26, 4), faces: true) # => "🌝"
20
+ ```
21
+
22
+ ### Phase calendar
23
+
24
+ ```ruby
25
+ Tsukimoji.calendar("2026-10") # one CalendarDay per day of October 2026
26
+ Tsukimoji.calendar_csv("2026-11", "2027-02") # November through February, as CSV
27
+ Tsukimoji.calendar_json(2026) # the whole year, as JSON
28
+ Tsukimoji.calendar_text("2026", "2027") # two years of month grids, as text
29
+ ```
30
+
31
+ Pass a year (`"2026"` or `2026`), a month (`"2026-10"`), a day
32
+ (`"2026-10-04"`) or a `Date` or `Time`, plus an optional second one to make a range.
33
+ Each new moon, quarter and full moon appears on the day it happens, with the
34
+ exact time. See the
35
+ [main README](https://github.com/samlehman/tsukimoji#phase-calendar) for the
36
+ formats.
37
+
38
+ ## Installation
39
+
40
+ ```
41
+ gem install tsukimoji
42
+ ```
43
+
44
+ Or add to your Gemfile:
45
+
46
+ ```ruby
47
+ gem "tsukimoji"
48
+ ```
49
+
50
+ ## How it works
51
+
52
+ tsukiMOJi measures elapsed time since a known new moon (2000-01-06
53
+ 18:14 UTC) against the synodic month (29.530588853 days) to find how far
54
+ into the current lunar cycle a given moment falls, then maps that
55
+ fraction onto the eight standard moon-phase emoji (πŸŒ‘πŸŒ’πŸŒ“πŸŒ”πŸŒ•πŸŒ–πŸŒ—πŸŒ˜).
56
+ Illumination is derived separately from the same cycle position via a
57
+ cosine curve, for display purposes.
58
+
59
+ ## License
60
+
61
+ MIT
62
+
63
+ ---
64
+
65
+ <p align="center">
66
+ <img src="https://raw.githubusercontent.com/samlehman/tsukimoji/main/made-in-baltimore.png" alt="Made in Baltimore" width="88" align="middle">
67
+ &nbsp;&nbsp;β˜• <a href="https://paypal.me/samlehman">Buy me a coffee</a> Β· <a href="https://paypal.me/samlehman">γ“γƒΌγ²γƒΌγ‚’γŠγ”γ£γ¦</a>
68
+ </p>
data/lib/tsukimoji.rb CHANGED
@@ -1,68 +1,229 @@
1
- # frozen_string_literal: true
2
-
3
- require "time"
4
-
5
- # tsukiMOJi ("moon" + "moji", as in emoji) computes the current lunar
6
- # phase for any date and returns the matching emoji, phase name, age in
7
- # days, and illumination fraction.
8
- #
9
- # Ported from the original moon-phase.html JavaScript prototype.
10
- module Tsukimoji
11
- SYNODIC_MONTH_DAYS = 29.530588853
12
- SYNODIC_MONTH_SECONDS = SYNODIC_MONTH_DAYS * 24 * 60 * 60
13
-
14
- # A known new moon reference point: 2000-01-06 18:14 UTC.
15
- KNOWN_NEW_MOON = Time.utc(2000, 1, 6, 18, 14, 0)
16
-
17
- PHASES = [
18
- { emoji: "\u{1F311}", name: "New Moon" },
19
- { emoji: "\u{1F312}", name: "Waxing Crescent" },
20
- { emoji: "\u{1F313}", name: "First Quarter" },
21
- { emoji: "\u{1F314}", name: "Waxing Gibbous" },
22
- { emoji: "\u{1F315}", name: "Full Moon" },
23
- { emoji: "\u{1F316}", name: "Waning Gibbous" },
24
- { emoji: "\u{1F317}", name: "Last Quarter" },
25
- { emoji: "\u{1F318}", name: "Waning Crescent" },
26
- ].freeze
27
-
28
- # Optional face emoji, by phase index: 🌚 for New Moon, 🌝 for Full Moon.
29
- FACES = { 0 => "\u{1F31A}", 4 => "\u{1F31D}" }.freeze
30
-
31
- # Immutable result of a phase calculation.
32
- Phase = Struct.new(:emoji, :name, :age_days, :illumination, keyword_init: true) do
33
- def to_s
34
- emoji
35
- end
36
- end
37
-
38
- class << self
39
- # Returns a Tsukimoji::Phase for the given time (defaults to now, UTC).
40
- # Pass faces: true to get 🌚 and 🌝 for new and full moons.
41
- def phase(time = Time.now.utc, faces: false)
42
- time = time.utc
43
- elapsed_seconds = time.to_f - KNOWN_NEW_MOON.to_f
44
- cycles = elapsed_seconds / SYNODIC_MONTH_SECONDS
45
- age = ((cycles % 1) + 1) % 1
46
- index = ((age + 1.0 / 16) * 8).floor % 8
47
- illumination = (1 - Math.cos(age * 2 * Math::PI)) / 2
48
-
49
- data = PHASES[index]
50
- Phase.new(
51
- emoji: (faces && FACES[index]) || data[:emoji],
52
- name: data[:name],
53
- age_days: age * SYNODIC_MONTH_DAYS,
54
- illumination: illumination
55
- )
56
- end
57
-
58
- # Convenience shortcut: just the emoji for the given time.
59
- def emoji(time = Time.now.utc, faces: false)
60
- phase(time, faces: faces).emoji
61
- end
62
-
63
- # Convenience shortcut: just the phase name for the given time.
64
- def name(time = Time.now.utc)
65
- phase(time).name
66
- end
67
- end
68
- end
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+ require "time"
5
+
6
+ # tsukiMOJi ("moon" + "moji", as in emoji) computes the current lunar
7
+ # phase for any date and returns the matching emoji, phase name, age in
8
+ # days, and illumination fraction.
9
+ #
10
+ # Ported from the original moon-phase.html JavaScript prototype.
11
+ module Tsukimoji
12
+ SYNODIC_MONTH_DAYS = 29.530588853
13
+ SYNODIC_MONTH_SECONDS = SYNODIC_MONTH_DAYS * 24 * 60 * 60
14
+
15
+ # A known new moon reference point: 2000-01-06 18:14 UTC.
16
+ KNOWN_NEW_MOON = Time.utc(2000, 1, 6, 18, 14, 0)
17
+
18
+ PHASES = [
19
+ { emoji: "\u{1F311}", name: "New Moon" },
20
+ { emoji: "\u{1F312}", name: "Waxing Crescent" },
21
+ { emoji: "\u{1F313}", name: "First Quarter" },
22
+ { emoji: "\u{1F314}", name: "Waxing Gibbous" },
23
+ { emoji: "\u{1F315}", name: "Full Moon" },
24
+ { emoji: "\u{1F316}", name: "Waning Gibbous" },
25
+ { emoji: "\u{1F317}", name: "Last Quarter" },
26
+ { emoji: "\u{1F318}", name: "Waning Crescent" },
27
+ ].freeze
28
+
29
+ # Optional face emoji, by phase index: 🌚 for New Moon, 🌝 for Full Moon.
30
+ FACES = { 0 => "\u{1F31A}", 4 => "\u{1F31D}" }.freeze
31
+
32
+ # New moon, first quarter, full moon and last quarter are a quarter cycle apart.
33
+ QUARTER_SECONDS = SYNODIC_MONTH_SECONDS / 4
34
+
35
+ MONTH_NAMES = %w[January February March April May June July
36
+ August September October November December].freeze
37
+
38
+ DAY_NAMES = %w[sunday monday tuesday wednesday thursday friday saturday].freeze
39
+
40
+ # Immutable result of a phase calculation.
41
+ Phase = Struct.new(:emoji, :name, :age_days, :illumination, keyword_init: true) do
42
+ def to_s
43
+ emoji
44
+ end
45
+ end
46
+
47
+ # One day of a phase calendar. event_time is set on the day of an exact
48
+ # new moon, quarter or full moon, and nil otherwise.
49
+ CalendarDay = Struct.new(:date, :emoji, :name, :event_time, :age_days, :illumination,
50
+ keyword_init: true)
51
+
52
+ class << self
53
+ # Returns a Tsukimoji::Phase for the given time (defaults to now, UTC).
54
+ # Pass faces: true to get 🌚 and 🌝 for new and full moons.
55
+ #
56
+ # The phase is the one for the time's UTC day, the same as calendar: new
57
+ # moon, first quarter, full moon and last quarter only on the day they
58
+ # happen, crescent or gibbous on the days in between. age_days and
59
+ # illumination are for the exact moment.
60
+ def phase(time = Time.now.utc, faces: false)
61
+ time = time.utc
62
+ elapsed_seconds = time.to_f - KNOWN_NEW_MOON.to_f
63
+ cycles = elapsed_seconds / SYNODIC_MONTH_SECONDS
64
+ age = ((cycles % 1) + 1) % 1
65
+ index, = day_phase(Time.utc(time.year, time.month, time.day))
66
+ illumination = (1 - Math.cos(age * 2 * Math::PI)) / 2
67
+
68
+ data = PHASES[index]
69
+ Phase.new(
70
+ emoji: (faces && FACES[index]) || data[:emoji],
71
+ name: data[:name],
72
+ age_days: age * SYNODIC_MONTH_DAYS,
73
+ illumination: illumination
74
+ )
75
+ end
76
+
77
+ # Convenience shortcut: just the emoji for the given time.
78
+ def emoji(time = Time.now.utc, faces: false)
79
+ phase(time, faces: faces).emoji
80
+ end
81
+
82
+ # Convenience shortcut: just the phase name for the given time.
83
+ def name(time = Time.now.utc)
84
+ phase(time).name
85
+ end
86
+
87
+ # One Tsukimoji::CalendarDay per UTC day from the start of +from+ to the
88
+ # end of +to+ (or just +from+). Each is a year ("2026" or 2026), a month
89
+ # ("2026-10"), a day ("2026-10-04"), a Date or a Time.
90
+ #
91
+ # New moon, first quarter, full moon and last quarter each fall on exactly
92
+ # one day, the day they happen, with event_time set. Days in between get
93
+ # the crescent or gibbous phase. age_days and illumination are at 12:00 UTC.
94
+ def calendar(from, to = nil, faces: false)
95
+ first = span(from).first
96
+ last = span(to.nil? ? from : to).last
97
+ raise ArgumentError, "to is before from" if last < first
98
+
99
+ (first..last).map do |day|
100
+ day_start = Time.utc(day.year, day.month, day.day)
101
+ index, event = day_phase(day_start)
102
+ noon = phase(day_start + 43_200)
103
+ CalendarDay.new(
104
+ date: day,
105
+ emoji: (faces && FACES[index]) || PHASES[index][:emoji],
106
+ name: PHASES[index][:name],
107
+ event_time: event,
108
+ age_days: noon.age_days,
109
+ illumination: noon.illumination
110
+ )
111
+ end
112
+ end
113
+
114
+ # The calendar as CSV, one row per day, with a header row.
115
+ def calendar_csv(from, to = nil, faces: false)
116
+ lines = ["date,emoji,name,event_time,age_days,illumination"]
117
+ calendar(from, to, faces: faces).each do |d|
118
+ event = d.event_time ? minute_iso(d.event_time) : ""
119
+ lines << [d.date.iso8601, d.emoji, d.name, event,
120
+ format("%.2f", d.age_days), format("%.3f", d.illumination)].join(",")
121
+ end
122
+ "#{lines.join("
123
+ ")}
124
+ "
125
+ end
126
+
127
+ # The calendar as a JSON array, one object per day.
128
+ def calendar_json(from, to = nil, faces: false)
129
+ rows = calendar(from, to, faces: faces).map do |d|
130
+ event = d.event_time ? "\"#{minute_iso(d.event_time)}\"" : "null"
131
+ %( {"date": "#{d.date.iso8601}", "emoji": "#{d.emoji}", "name": "#{d.name}", ) +
132
+ %("event_time": #{event}, ) +
133
+ %("age_days": #{format("%.2f", d.age_days)}, "illumination": #{format("%.3f", d.illumination)}})
134
+ end
135
+ "[
136
+ #{rows.join(",
137
+ ")}
138
+ ]
139
+ "
140
+ end
141
+
142
+ # The calendar as plain text: a grid per month, followed by that month's
143
+ # exact new moon, quarter and full moon times. Weeks start on Sunday; pass
144
+ # week_start: :monday (or any day name) to change that, and
145
+ # numbers: false for a grid of just the emoji, without day numbers.
146
+ def calendar_text(from, to = nil, faces: false, numbers: true, week_start: :sunday)
147
+ first_day = DAY_NAMES.index(week_start.to_s.downcase)
148
+ raise ArgumentError, "week_start must be a day name, got #{week_start}" if first_day.nil?
149
+
150
+ headings = Array.new(7) { |i| DAY_NAMES[(first_day + i) % 7][0, 2].capitalize }
151
+ months = calendar(from, to, faces: faces).group_by { |d| [d.date.year, d.date.month] }
152
+
153
+ blocks = months.map do |(year, month), days|
154
+ length = Date.new(year, month, -1).day
155
+ offset = (Date.new(year, month, 1).wday - first_day) % 7
156
+ cells = Array.new(offset + length)
157
+ days.each { |d| cells[offset + d.date.day - 1] = d }
158
+
159
+ header = headings.map { |h| numbers ? " #{h}" : h }.join(" ")
160
+ lines = ["#{MONTH_NAMES[month - 1]} #{year}", header]
161
+ cells.each_slice(7) do |week|
162
+ week.pop until week.empty? || week.last
163
+ next if week.empty?
164
+
165
+ lines << week.map { |d| text_cell(d, numbers) }.join(" ")
166
+ end
167
+ events = days.select(&:event_time)
168
+ unless events.empty?
169
+ lines << ""
170
+ events.each do |d|
171
+ t = minute_iso(d.event_time)
172
+ lines << "#{d.emoji} #{d.name.ljust(13)} #{t[0, 10]} #{t[11, 5]} UTC"
173
+ end
174
+ end
175
+ lines.join("
176
+ ")
177
+ end
178
+ "#{blocks.join("
179
+
180
+ ")}
181
+ "
182
+ end
183
+
184
+ private
185
+
186
+ # The phase of the UTC day starting at +day_start+: [phase index, event time or nil].
187
+ def day_phase(day_start)
188
+ # The last principal phase before the end of this day.
189
+ n = ((day_start.to_f + 86_400 - KNOWN_NEW_MOON.to_f) / QUARTER_SECONDS).ceil - 1
190
+ event = KNOWN_NEW_MOON + (n * QUARTER_SECONDS)
191
+ is_event = event >= day_start
192
+ [((n % 4) * 2) + (is_event ? 0 : 1), is_event ? event : nil]
193
+ end
194
+
195
+ # "2026", "2026-10", "2026-10-04", 2026, a Date or a Time -> [first day, last day].
196
+ def span(value)
197
+ case value
198
+ when Time then return [value.getutc.to_date] * 2
199
+ when DateTime then return [value.new_offset(0).to_date] * 2
200
+ when Date then return [value, value]
201
+ end
202
+
203
+ m = /\A(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?\z/.match(value.to_s) if value.is_a?(String) || value.is_a?(Integer)
204
+ year = m ? m[1].to_i : 0
205
+ month = m && m[2] ? m[2].to_i : 1
206
+ raise ArgumentError, "Expected YYYY, YYYY-MM or YYYY-MM-DD, got #{value}" if !m || year < 1 || !month.between?(1, 12)
207
+
208
+ if m[3]
209
+ raise ArgumentError, "No such date: #{value}" unless Date.valid_date?(year, month, m[3].to_i)
210
+
211
+ return [Date.new(year, month, m[3].to_i)] * 2
212
+ end
213
+ return [Date.new(year, month, 1), Date.new(year, month, -1)] if m[2]
214
+
215
+ [Date.new(year, 1, 1), Date.new(year, 12, 31)]
216
+ end
217
+
218
+ def text_cell(day, numbers)
219
+ return numbers ? " " : " " if day.nil?
220
+
221
+ numbers ? "#{day.date.day.to_s.rjust(2)} #{day.emoji}" : day.emoji
222
+ end
223
+
224
+ # "2026-10-04T00:02Z": event times are only accurate to minutes at best.
225
+ def minute_iso(time)
226
+ time.utc.strftime("%Y-%m-%dT%H:%MZ")
227
+ end
228
+ end
229
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tsukimoji
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Lehman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-10-05 00:00:00.000000000 Z
11
+ date: 2026-11-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: tsukiMOJi computes the current (or any) lunar phase and returns the matching
14
14
  emoji, phase name, age in days, and illumination fraction.