We build a pet astrology app, and the website around it has a few free calculators. Two of them are for people: a Sun, Moon and Rising calculator and a Moon sign calculator. Both run entirely in the browser: no API, no date library, no time zone library. The engine is about 140 lines on top of the Sun and Moon positions from our earlier post.
There are three problems to solve, and each one has a trap:
- Turn "born 02:30 on this date in this city" into a UTC instant, including daylight saving rules from decades ago.
- Say which sign the Sun and Moon were in, and if the Moon changed sign that day, at which minute.
- Compute the Ascendant (the rising sign) from that instant and the city's coordinates.
Our first test suite passed. A code review then found two real bugs in it. Both are below.
1. Local time to UTC with only Intl
Browsers ship the full IANA time zone database, history included. Intl.DateTimeFormat will tell you what the wall clock read in any zone at any instant, which is enough to get the UTC offset:
function offsetMinutes(tz, ms) {
const p = {};
new Intl.DateTimeFormat('en-US', {
timeZone: tz, hourCycle: 'h23',
year: 'numeric', month: '2-digit', day: '2-digit',
hour: '2-digit', minute: '2-digit', second: '2-digit'
}).formatToParts(new Date(ms)).forEach(x => { p[x.type] = x.value; });
const asUtc = Date.UTC(+p.year, +p.month - 1, +p.day, +p.hour % 24, +p.minute, +p.second);
return Math.round((asUtc - Math.floor(ms / 1000) * 1000) / 60000);
}
Asked about old dates, it returns the offsets that were in force then. In Node 24:
| Zone | Date | Offset returned |
|---|---|---|
| Europe/London | 15 Dec 1968 | +60 min (Britain stayed on summer time that winter) |
| America/New_York | 1 Jul 1950 | −240 min |
| Europe/Warsaw | 1 Jul 1977 | +120 min |
| Europe/Kyiv | 1 Jul 1985 | +240 min |
The inverse direction is the hard one, because a wall-clock time can exist twice or not at all. We follow the rule that Temporal calls "compatible": a time that happened twice resolves to its first occurrence, and a time that was skipped lands just after the jump.
const wallOf = (tz, ms) => ms + offsetMinutes(tz, ms) * 60000;
function localToUtc(y, mo, d, h, mi, tz) {
const wall = Date.UTC(y, mo - 1, d, h, mi);
const before = wall - offsetMinutes(tz, wall - 86400000) * 60000;
const after = wall - offsetMinutes(tz, wall + 86400000) * 60000;
const okBefore = wallOf(tz, before) === wall;
const okAfter = wallOf(tz, after) === wall;
if (okBefore && okAfter) return Math.min(before, after);
if (okBefore) return before;
if (okAfter) return after;
return before; // skipped time
}
The idea is to try the offset from a day earlier and the offset from a day later, and keep whichever candidate maps back to the requested wall time.
- New York, 10 March 2024, 02:30 (skipped): returns 07:30 UTC, the instant just after the jump.
- New York, 3 November 2024, 01:30 (happened twice): returns 05:30 UTC, the first one.
- Round trip on 3,000 pseudo-random date-times across eight zones from 1950 to 2025: 3,000 of 3,000 came back unchanged.
Bug one was here. The first version got the start of the day wrong in zones that change their clocks at midnight, which Chile, Jordan and (until 2019) Brazil have done. On such a night the day does not start at 00:00, or 00:00 happens twice, and the "whole day" window came out shifted. The Moon calculator then looked for a sign change in the wrong window. In Santiago the Moon entered Virgo at 23:59 local time on 11 August 2018, one minute before the clocks jumped from 00:00 to 01:00. It is hard to construct a nastier test date.
One caveat we show on the page: the time zone database only aims to be accurate from 1970 on. For earlier births the offset is usually right, but it deserves a second look.
2. The minute the Moon changes sign
The Moon covers a sign in about two and a quarter days. Counting New York calendar days in 2024 and 2025, it changed sign during the day on 321 of 731 days (43.9%). So for almost half of all birthdays, the date alone gives two candidate Moon signs, and the calculator has to say so instead of picking one.
When the sign at the start of the day differs from the sign at the end, a bisection finds the minute:
function daySpan(y, mo, d, tz, signAt) {
const start = localToUtc(y, mo, d, 0, 0, tz);
const next = new Date(Date.UTC(y, mo - 1, d) + 86400000);
const end = localToUtc(next.getUTCFullYear(), next.getUTCMonth() + 1, next.getUTCDate(), 0, 0, tz) - 1;
const a = signAt(start), b = signAt(end);
if (a === b) return { first: a, last: b, changeMs: null };
let lo = start, hi = end;
while (hi - lo > 30000) {
const mid = (lo + hi) / 2;
if (signAt(mid) === a) lo = mid; else hi = mid;
}
// the shipped version also keeps the rounded minute inside the day's own edges
return { first: a, last: b, changeMs: Math.round(hi / 60000) * 60000 };
}
A day holds at most one change, so there is nothing to search for beyond that. The same function finds the Sun's change of sign, which happened on 24 of those 731 days. For 23 August 2023 it returns 09:01 UTC for the Sun entering Virgo.
One detail matters at this precision. Planetary positions are computed in Terrestrial Time, clocks run on Universal Time, and the difference (ΔT) is about 69 seconds today and was about 24 seconds in 1930. Ignore it and the minute of a sign change can be off by one. We interpolate ΔT from a table of ten-year values, which is plenty for a result rounded to the minute.
3. The Ascendant
The rising sign is the sign of the ecliptic point crossing the eastern horizon. It needs local sidereal time, the obliquity of the ecliptic and the latitude:
const RAD = Math.PI / 180;
const norm = x => ((x % 360) + 360) % 360;
function gmst(jd) { // Greenwich mean sidereal time, degrees
const t = (jd - 2451545.0) / 36525;
return norm(280.46061837 + 360.98564736629 * (jd - 2451545.0)
+ 0.000387933 * t * t - t * t * t / 38710000);
}
function ascendant(jd, lat, lon) { // lon is east-positive
const t = (jd - 2451545.0) / 36525;
const e = (23.439291 - 0.0130042 * t) * RAD;
const ramc = norm(gmst(jd) + lon) * RAD;
const f = lat * RAD;
return norm(Math.atan2(
Math.cos(ramc),
-(Math.sin(ramc) * Math.cos(e) + Math.tan(f) * Math.sin(e))
) / RAD);
}
Sidereal time runs on Universal Time, so this step uses the clock instant directly, without ΔT.
The popular rule is "the rising sign changes every two hours". That is only the average. Counting minutes over one day:
| Place | Shortest | Longest |
|---|---|---|
| London (51.5° N) | Aries, 52 min | Virgo, 171 min |
| Quito (0.2° S) | Virgo, Libra and Pisces, 111 min | Cancer and Sagittarius, 129 min |
In London a birth time that is 30 minutes off can easily move an Aries or Pisces Ascendant into the next sign, while a Virgo Ascendant survives a much larger error. That is why the calculator runs the result again at 30 minutes earlier and later and tells you whether the sign held.
The bug above the Arctic Circle
Bug two. The formula above is the one in most references, and it has a quiet failure mode. Inside the polar circles part of the ecliptic never rises, and for part of each day the atan2 returns the point that is setting in the west, 180° from the answer.
We measured how often, for 1 January 2026, minute by minute:
| Place | Latitude | Minutes per day with the wrong horizon |
|---|---|---|
| London | 51.5° N | 0 |
| Reykjavik | 64.2° N | 0 |
| Murmansk | 69.0° N | 220 |
| Tromsø | 69.7° N | 249 |
| Longyearbyen | 78.2° N | 489 |
In every one of those minutes the sign is the opposite one. Nothing throws and the output looks perfectly plausible.
The fix uses the fact that the rising point always lies east of the meridian. Compute the Midheaven, and if the candidate is on the wrong side of it, flip it:
function ascendantFixed(jd, lat, lon) {
let asc = ascendant(jd, lat, lon);
const t = (jd - 2451545.0) / 36525;
const eDeg = 23.439291 - 0.0130042 * t;
if (Math.abs(lat) > 90 - eDeg) {
const e = eDeg * RAD, ramc = norm(gmst(jd) + lon) * RAD;
const mc = norm(Math.atan2(Math.sin(ramc), Math.cos(ramc) * Math.cos(e)) / RAD);
if (((asc - mc + 540) % 360) - 180 < 0) asc = norm(asc + 180);
}
return asc;
}
The regression test converts the returned point to right ascension, takes its hour angle and asserts that it is in the eastern half of the sky for every minute of a day in seven places, from London to Longyearbyen and Ushuaia. After the fix: zero western minutes in all seven.
Testing without a reference ephemeris
We did not want the tests to depend on someone else's calculator, so they check the engine against things that must be true:
- At sunrise the Ascendant is the Sun. The Sun is on the eastern horizon at that moment, so the two longitudes must match, and at sunset they must be 180° apart. Across 1,343 sunrises and sunsets, at latitudes from 45° S to 60° N and years from 1970 to 2016, the largest difference was 0.014° at sunrise and 0.016° at sunset.
- Around a sign change the sign really changes. For every day with a reported Moon change in two years, the sign one minute before is the old one and one minute after is the new one.
- Round trips. Local to UTC and back returns the input, except for times that never existed.
What those tests did not cover is the lesson. All of our sunrise latitudes stopped at 60°, where the polar bug cannot occur, and none of our zones changed clocks at midnight. The tests passed because they were drawn from places where the code was right. The reviewer found both bugs by asking where the formulas stop being valid, which is a better question than "does it pass".
What it does not do
No houses, no aspects, no geocoding service. The city field uses a static list of 12,467 places from GeoNames (CC BY 4.0) that is fetched only when you focus the field. Without a birth time the calculator leaves the Ascendant empty. It does not fall back to noon, because a rising sign computed from an assumed hour is a guess presented as a result.
You can try both tools here: Sun, Moon and Rising and Moon sign. If you know a zone or a latitude where they get it wrong, we would like to hear about it.
Top comments (1)
The part that stuck with me isn't the atan2 fix — it's that your tests were genuinely clever (sunrise equals the Ascendant, sign flips exactly at the bisection minute, 3,000/3,000 round trips) and still missed it, because every sunrise latitude stopped at 60°. The coverage boundary was the bug's hiding place.
And the failure mode is the worst kind: nothing throws, the output looks perfect, and the sign is simply the opposite one — for 489 minutes a day in Longyearbyen on 1 January. That table (0, 0, 220, 249, 489) is a beautiful way to show a bug scaling with latitude.
Now that the hour-angle assertion is the guard, do you also sweep a polar latitude into the sunrise property test, or is the regression test the permanent net for this one?