July 25th, 2024

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

Tiny Moon is a Swift library for calculating moon phases offline on iOS and macOS. It offers functions for moon phase calculations and includes demo applications for user guidance.

Read original articleLink Icon
AppreciationInterestSkepticism
Show HN: Tiny Moon – Swift library to calculate the moon phase

Tiny Moon is a Swift library designed for calculating the moon phase for any date, functioning offline and compatible with iOS and macOS. Users can quickly obtain the current moon phase using the function `TinyMoon.calculateMoonPhase()`. To install the library, developers should open their Xcode project, navigate to the Package Dependencies tab, and add the Tiny Moon Package using the provided GitHub URL, specifying version rules as "Up to Next Major Version."

A usage example in SwiftUI demonstrates how to display the moon phase, including details such as the date, emoji representation, name, illumination fraction, and the time until the next full or new moon. The library offers two main functions: `calculateMoonPhase`, which returns the moon phase for a given date and time zone, and `calculateExactMoonPhase`, which provides a more precise calculation.

The `Moon` object includes several properties, such as `moonPhase`, `name`, `emoji`, `daysTillFullMoon`, and `daysTillNewMoon`. Additionally, there are demo applications available for both macOS and iOS that showcase the functionality of the Tiny Moon package. The library draws inspiration from various resources, including the suncalc library and contributions from John Walker's Moontool. For further details, users can visit the Tiny Moon GitHub repository.

Related

NodeSwift: Bridge Node.js and Swift

NodeSwift: Bridge Node.js and Swift

NodeSwift facilitates Swift and Node.js communication, leveraging macOS APIs, SwiftPM, NPM, and Swift for enhanced performance. It emphasizes safety, simplicity, and cross-platform compatibility, simplifying memory management and offering seamless integration.

Tachyonfx: A library for creating shader-like effects in terminal UIs

Tachyonfx: A library for creating shader-like effects in terminal UIs

The "tachyonfx" library on GitHub enables shader-like effects in terminal UIs with color transformations, animations, and complex combinations. For more information or assistance, reach out for support.

A Relativistic Framework to Establish Coordinate Time on the Moon and Beyond

A Relativistic Framework to Establish Coordinate Time on the Moon and Beyond

The paper discusses establishing a coordinate time framework for celestial bodies like the Moon based on Einstein's theory of relativity. It highlights the importance of synchronized clocks for accurate communication and navigation.

TinyML: Why the Future of Machine Learning Is Tiny and Bright

TinyML: Why the Future of Machine Learning Is Tiny and Bright

TinyML is a field merging machine learning with embedded systems, enabling AI on small devices for low-latency decisions, energy efficiency, and privacy. It impacts sectors like smart assistants and disease detection.

TinyPod – Apple Watch case with scroll wheel

TinyPod – Apple Watch case with scroll wheel

The tinyPod is a standalone device with various features like calls, music, messaging, and more. It operates independently from a phone, powered by an Apple Watch, offering tactile navigation and Apple Pay support. Available in standard and lite versions, priced at $79.99 and $29.99 respectively, with summer shipping.

AI: What people are saying
The comments on the Tiny Moon library reflect a mix of appreciation, suggestions, and critiques regarding its functionality and implementation.
  • Many users praise the library for its offline capabilities, documentation, and well-structured code.
  • Some commenters suggest alternative methods for calculating moon phases, including using the Islamic calendar or simpler algorithms.
  • There is interest in potential expansions of the library to other platforms like Linux and Windows.
  • Users express practical applications for the library, particularly in activities like fishing that are influenced by moon phases.
  • A few comments share personal anecdotes related to moon phase calculations and tools.
Link Icon 13 comments
By @adityapurwa - 6 months
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.

I used this approach because most platforms supports islamic calendar.

I really like the offline first approach, and would definitely use the library when there’s a need.

Thanks for sharing!

By @rubiquity - 6 months
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.
By @ChrisMarshallNY - 6 months
I don'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.
By @shainvs - 6 months
Is there a plan to add this for linux / windows? Either way, super cool project
By @jheriko - 6 months
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...

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):

function approximateMoonPhase(julianDay)

{

    // from Meeus p.319

    // JDE = 2451 550.09765 + 29.530 588 853 k

    // + 0.000 1337 T2

    // - 0.000 000 150 T3

    // + 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 / 29.530588853;
}

enjoy. its from the same source as your work appears to use...

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.

By @jes5199 - 6 months
nice! I have needed this before. I think you're using the Meeus algorithm from Astronomical Algorithms? it's a classic, great choice
By @8mobile - 6 months
Hi,

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.

Thanks Otto

By @ks2048 - 6 months
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).
By @whartung - 6 months
And then, there was an anecdote!

I bought my friends daughter a watch for her birthday. She was, like, 12 or something. I got it at Target.

And it had a moon phase dial! How about that?!

So I was determined to set it accurately for her.

To wit I started dredging through the internet. And to be completely honest, I'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't no 10 seconds on Google, I'll tell you that.

In the end, I stumbled upon some code for my HP-48. Aha! This should do nicely!

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.

Finally! ACCURATE results. Done with precision and math and engineering. Done Right!

To wit, I then proceeded to get the watch set properly.

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.

Oh.

By @gmiller123456 - 6 months
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.

By @082349872349872 - 6 months
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 <= x < kh)]
        for daynum  in [lambda y,m,d: daynum(y-1,m+12,d) if m<3 else
                         y*365 + y//4 - y//100 + y//400 + (153*m+3)//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,"new"),(0.5,"waxing crescent"),(7,"first quarter"),
                (8,"waxing gibbous"),(14.5,"full"),(15.5,"waning gibbous"),
                (21.7,"last quarter"),(22.7,"waning crescent"),(29,"new"),
                (30,None)]] )
By @kickingvegas - 6 months
Side note, if you use Emacs you can get the phases of the Moon by pressing “M” in either calendar or Org Agenda.