Commits

Trammell Hudson committed 29ca44c

More calendar math -- julian days and moon phase

  • Participants
  • Parent commits 832c5c9

Comments (0)

Files changed (2)

File lib/src/datetime.c

 
 	return now;
 }
+
+
+static const int julian_1900 = 2415033;
+
+uint32_t
+julian_day(
+	const int year, // since 0
+	const int month, // 1 - 12
+	const int day // 1 - 31
+)
+{
+	int a = (14 - month) / 12;
+	int y = year + 4800 - a;
+	int m = month + 12 * a - 3;
+	return day + (153 * m + 2) / 5 + 365 * y + y / 4 - 32083;
+}
+
+
+uint32_t
+julian_day_pbl(
+	const PblTm * const tm
+)
+{
+	return julian_day(
+		tm->tm_year + 1900,
+		tm->tm_mon + 1,
+		tm->tm_mday
+	);
+}
+
+
+uint32_t
+moonphase(
+	const PblTm * const tm
+)
+{
+	// Compute julian day since 1 jan 1900, which was new moon
+	uint32_t julian = julian_day_pbl(tm);
+	julian -= julian_1900;
+
+	return julian % 2953;
+}

File lib/src/pebble_th.h

 );
 
 
+/** Compute the phase of the moon.
+ * 0 == new moon,
+ * 2953 == almost a full moon.
+ */
+uint32_t
+moonphase(
+	const PblTm * const now
+);
+
+
+uint32_t
+julian_day_pbl(
+	const PblTm * const now
+);
+
+
+uint32_t
+julian_day(
+	const int year, // since 0
+	const int month, // 1 - 12
+	const int day // 1 - 31
+);
+
+
+uint32_t
+days_since_1900(
+	uint32_t year
+);
+
 #endif