Live data from Hacker News

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

github.com

1–10 of 41 posts

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

#1
Tiny Moon is a tiny Swift library to calculate the moon phase for any given date, works super fast, and works completely offline.

All of this started when I realized that we only have 12, sometimes 13, full moon's in a year. That doesn’t seem like that many.

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.

The MacOS app I ended up creating can be found at https://apps.apple.com/us/app/tiny-moon/id6502374344 along with the source code [0], all powered by the Tiny Moon library.

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.

The app tries to be as minimal as possible, does what it does very fast, and works completely offline.

[0] https://github.com/mannylopez/TinyMoonApp

[1] https://github.com/mourner/suncalc

[2] https://www.fourmilab.ch/moontoolw/

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

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

#3
Nice work. Just FYI for anybody else - it adds an icon to status bar (if that's what it's called? - top right icons). I thought it was going to open an app window and didn't notice the icon, so thought it wasn't working. (In hindsight, it's clear from screenshots in app store).

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

#4
post #3

Nice work. Just FYI for anybody else - it adds an icon to status bar (if that's what it's called? - top right icons). I thought it was going to open an app window and didn't notice the icon, so thought it wasn't working. (In hindsight, it's clear from screenshots in app store).

I think it's called the menu bar, and apps that don't open windows (with an icon in the menu bar) are known as "menu bar apps". Until recently, these were a PITA to implement with Apple's SDK. Thankfully they provided API support sometime I think starting with macOS 13... before that it was pretty kludgy.

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

#6
Here'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?):

  _=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 

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

#7

Is there a plan to add this for linux / windows? Either way, super cool project

Good news! According to Swift Package Index [0], Tiny Moon is already compatible with Linux environments.

0. https://swiftpackageindex.com/mannylopez/TinyMoon/

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

#8
post #2

nice! I have needed this before. I think you're using the Meeus algorithm from Astronomical Algorithms? it's a classic, great choice

Yes, pretty much every good library I came across referenced Meeus' Astronomical Algorithms [0] book and formulas. Those were a bit dense for me to fully parse, but Dr. Louis Strous` page [1] on finding the position of the moon helped simplify it for me (although that implementation is less accurate, I think).

The algorithm in Moontool for Windows [2], which Tiny Moon is based off of, is based off of Meeus' algorithm.

0. https://www.agopax.it/Libri_astronomia/pdf/Astronomical%20Al...

1. https://aa.quae.nl/en/reken/hemelpositie.html#4

2. https://www.fourmilab.ch/moontoolw/

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

#9

Here'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?): _=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

So this is just saying that the phase of the moon is the day mod 29.53, starting from a new moon on 2000-01-06. That's about a minute off the actual mean length of a lunation - I guess the error from that approximation is less than the approximation in your "varies significantly"?

(For what it's worth - 29.53 * 300 = 8859 exactly, and 2000-01-06 + 8859 days = 2024-04-08, and I am quite sure there was a new moon on the latter date because of the solar eclipse.)

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

#10
I have a site with a lot of these stand alone "snippets" so that you don't have to include/port an entire astronomy library just to get the Sun rise/set times, etc. https://www.celestialprogramming.com/

Most of them are written in JavaScript only, but specifically written to be easy to port to other languages.

Post reply on HN