pebble / lib / src / datetime.c

/** \file
 * Date/time calculations
 */

#include <stdint.h>
#include "pebble_os.h"
#include "pebble_th.h"


uint32_t
unix_epoch_time(
	const PblTm * const curr_time,
	const int gmt_offset
)
{
	long now = 0;

	now += (curr_time->tm_year-70)*31536000;
	now += ((curr_time->tm_year-69)/4)*86400;
	now -= ((curr_time->tm_year-1)/100)*86400;
	now += ((curr_time->tm_year+299)/400)*86400;
	now += curr_time->tm_yday*86400;
	now += curr_time->tm_hour*3600;
	now += curr_time->tm_min*60;
	now += curr_time->tm_sec;
	now -= gmt_offset;

	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;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.