TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Tiny Moon – Swift library to calculate the moon phase

106 pointsby mannylopez10 months ago
Tiny Moon is a tiny Swift library to calculate the moon phase for any given date, works super fast, and works completely offline.<p>All of this started when I realized that we only have 12, sometimes 13, full moon&#x27;s in a year. That doesn’t seem like that many.<p>I set out to build a MacOS app to remind me when a full moon occurs, so that I could take a moment and step outside to appreciate it.<p>The MacOS app I ended up creating can be found at <a href="https:&#x2F;&#x2F;apps.apple.com&#x2F;us&#x2F;app&#x2F;tiny-moon&#x2F;id6502374344" rel="nofollow">https:&#x2F;&#x2F;apps.apple.com&#x2F;us&#x2F;app&#x2F;tiny-moon&#x2F;id6502374344</a> along with the source code [0], all powered by the Tiny Moon library.<p>I knew that I wanted the app to work offline, so working with a network request was out of the picture. Taking inspiration from SunCalc [1] and Moontool for Windows [2], I decided to create my own library and wrote Tiny Moon as a Swift Package to power my app.<p>The app tries to be as minimal as possible, does what it does very fast, and works completely offline.<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;mannylopez&#x2F;TinyMoonApp">https:&#x2F;&#x2F;github.com&#x2F;mannylopez&#x2F;TinyMoonApp</a><p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;mourner&#x2F;suncalc">https:&#x2F;&#x2F;github.com&#x2F;mourner&#x2F;suncalc</a><p>[2] <a href="https:&#x2F;&#x2F;www.fourmilab.ch&#x2F;moontoolw&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.fourmilab.ch&#x2F;moontoolw&#x2F;</a>

12 comments

adityapurwa10 months ago
If granular accuracy is not an important factor, you can also use Islamic calendar to calculate the moon phase. Muslims uses lunar cycles for some religious events (e.g. the Ayyamul Bidh or 3 days fasting during full moon). So when its 15 of an Islamic calendar, it’ll be a full moon.<p>I used this approach because most platforms supports islamic calendar.<p>I really like the offline first approach, and would definitely use the library when there’s a need.<p>Thanks for sharing!
评论 #41076084 未加载
rubiquity10 months ago
I know a lot of people into fishing that would love this as an app to quickly check the moon phase on a given date when planning a multi-day fishing trip or deciding when to go out. Apex predators are typically less hungry around and during a full moon due to the extra light making hunting at night easy.
评论 #41076410 未加载
ChrisMarshallNY10 months ago
I don&#x27;t really have use for this, but I must compliment the author on a job well-done. The code is well-structured, well-documented, well-tested, and well-designed.
评论 #41073334 未加载
shainvs10 months ago
Is there a plan to add this for linux &#x2F; windows? Either way, super cool project
评论 #41072983 未加载
评论 #41072253 未加载
评论 #41077660 未加载
jheriko10 months ago
this looks incredibly bad from the implementation. there is a much simpler algorithm that works for hundreds of years in either direction of the present...<p>by comparison this is an absolute mountain of code. here is a good one - but note that it is only so accurate for each quarter phase (...and not sure why it destroys the formatting):<p>function approximateMoonPhase(julianDay)<p>{<p><pre><code> &#x2F;&#x2F; from Meeus p.319 &#x2F;&#x2F; JDE = 2451 550.09765 + 29.530 588 853 k &#x2F;&#x2F; + 0.000 1337 T2 &#x2F;&#x2F; - 0.000 000 150 T3 &#x2F;&#x2F; + 0.000 000 000 73 T4 const t = toJulianCenturiesSinceJ2000(julianDay); const lhs = julianDay - 2451550.09765 + (-0.0001337 + (0.00000015 - 0.00000000073 * t) * t) * t * t; return lhs &#x2F; 29.530588853; </code></pre> }<p>enjoy. its from the same source as your work appears to use...<p>which, as someone who as implemented a ton of this stuff. its kind of a damning sentiment that we still refer to such an old book instead of learning the problem space and making better solutions... i have a whole book in me about this at some point.
评论 #41076765 未加载
评论 #41082536 未加载
评论 #41077603 未加载
评论 #41076791 未加载
jes519910 months ago
nice! I have needed this before. I think you&#x27;re using the Meeus algorithm from Astronomical Algorithms? it&#x27;s a classic, great choice
评论 #41072334 未加载
8mobile10 months ago
Hi,<p>Great library, super documentation and the code seems well written with lots of tests. Kudos, I will definitely check it out for my next ios project.<p>Thanks Otto
ks204810 months ago
Nice work. Just FYI for anybody else - it adds an icon to status bar (if that&#x27;s what it&#x27;s called? - top right icons). I thought it was going to open an app window and didn&#x27;t notice the icon, so thought it wasn&#x27;t working. (In hindsight, it&#x27;s clear from screenshots in app store).
评论 #41071037 未加载
whartung10 months ago
And then, there was an anecdote!<p>I bought my friends daughter a watch for her birthday. She was, like, 12 or something. I got it at Target.<p>And it had a moon phase dial! How about that?!<p>So I was determined to set it accurately for her.<p>To wit I started dredging through the internet. And to be completely honest, I&#x27;m not quite sure how I was doing that, having nothing but a Netcom.com shell account available to me. Perhaps I was using Lynx, or using some mail gateway, or who knows what. It wasn&#x27;t no 10 seconds on Google, I&#x27;ll tell you that.<p>In the end, I stumbled upon some code for my HP-48. Aha! This should do nicely!<p>Somehow, I got the code into the calculator. Did I type it in? Did I download it over Kermit? Who knows. Times were moving fast back then. Somehow, someway, however, I got that code in and calculated away.<p>Finally! ACCURATE results. Done with precision and math and engineering. Done Right!<p>To wit, I then proceeded to get the watch set properly.<p>And...it was a fashion watch for a 12 year old girl from Target. This may come as a shock to some, I was obviously taken aback by it, however -- the moon phase did not work. It was just a moon on a dial that moved, though some undetermined mechanism. It could not be set independently.<p>Oh.
评论 #41074814 未加载
gmiller12345610 months ago
I have a site with a lot of these stand alone &quot;snippets&quot; so that you don&#x27;t have to include&#x2F;port an entire astronomy library just to get the Sun rise&#x2F;set times, etc. <a href="https:&#x2F;&#x2F;www.celestialprogramming.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.celestialprogramming.com&#x2F;</a><p>Most of them are written in JavaScript only, but specifically written to be easy to port to other languages.
评论 #41072541 未加载
08234987234987210 months ago
Here&#x27;s a tiny calculator for moon phase that I kludged up ca. 2018 but (at least as of this month) is still tracking the full moon (which makes sense given that the algo came out of a dead tree source from over half a century ago?):<p><pre><code> _=min lphase = lambda y,m,d: _( lkp(ph,p) for lkp in [lambda t,x: _(v for (kl,v),(kh,w) in zip(t,t[1:]) if kl &lt;= x &lt; kh)] for daynum in [lambda y,m,d: daynum(y-1,m+12,d) if m&lt;3 else y*365 + y&#x2F;&#x2F;4 - y&#x2F;&#x2F;100 + y&#x2F;&#x2F;400 + (153*m+3)&#x2F;&#x2F;5 + d] for o in [daynum(y,m,d)-daynum(2000,1,6)] for p in [o%29.53] # mean; varies significantly! for ph in [[(0,&quot;new&quot;),(0.5,&quot;waxing crescent&quot;),(7,&quot;first quarter&quot;), (8,&quot;waxing gibbous&quot;),(14.5,&quot;full&quot;),(15.5,&quot;waning gibbous&quot;), (21.7,&quot;last quarter&quot;),(22.7,&quot;waning crescent&quot;),(29,&quot;new&quot;), (30,None)]] )</code></pre>
评论 #41072371 未加载
评论 #41072543 未加载
kickingvegas10 months ago
Side note, if you use Emacs you can get the phases of the Moon by pressing “M” in either calendar or Org Agenda.
评论 #41073178 未加载
评论 #41074809 未加载