Newer
Older
Tardis / src / Clock.cpp
/*
SPDX-License-Identifier: MIT
Copyright (c) 2023 John Watts and the LuminaSensum contributors
*/

#include "Clock.h"
#include "mbed.h"

using namespace std::chrono;

// Initializes the clock, run once at boot
void clock_setup(void) {
	// mbed reads the RTC at boot, do nothing for now
}

// Syncs the clock using SNTP, run any time
SNTPError clock_sync(NetworkInterface &net) {
	struct timeval time;
	SNTPError err;
	err = sntp(net, "time.google.com", 123, &time);
	if (err != SNTPSuccess) {
		return err;
	}
	set_time(time.tv_sec);
	return SNTPSuccess;
}

// Gets the current clock time in microseconds since 1970
microseconds clock_now(void) {
	time_t now_seconds = time(NULL);
	microseconds microsecs = seconds(now_seconds);
	return microsecs;
}

// Formats a time in microseconds using strftime
// Returned value is a buffer containing the formatted string
// or "(time overflow)"
// Calling this function overwrites the previous buffer contents
// so if you want to preserve it please copy the string
const char *clock_timestring(microseconds time, const char *format) {
	time_t time_seconds = ceil<seconds>(time).count();
	struct tm *local_time = localtime(&time_seconds);

	static char buffer[64];
	size_t buffer_size = sizeof(buffer);
	size_t buffer_written;
	buffer_written = strftime(buffer, buffer_size, format, local_time);

	if (buffer_written == 0) {
		// It overflowed, so return something useful
		return "(time overflow)";
	} else {
		return buffer;
	}
}

// Formats a time in microseconds according to ISO 8601
// Returned value is a buffer containing the formatted string
// or "(iso8601 overflow)"
// Calling this function overwrites the previous buffer contents
// so if you want to preserve it please copy the string
const char *clock_iso8601string(microseconds time) {
	const char *timestring = clock_timestring(time, "%Y-%m-%dT%H:%M:%S");
	long only_microseconds = time.count() % 1000000;

	static char buffer[128];
	int buffer_size = sizeof(buffer);
	int written;
	written = snprintf(
		buffer, buffer_size, "%s.%06li", timestring, only_microseconds);

	if (written >= buffer_size) {
		// It overflowed, so return something useful
		return "(iso8601 overflow)";
	} else {
		return buffer;
	}
}

// Formats the current time in microseconds according to ISO 8601
// This is a quick wrapper for clock_iso8601string using the current
// clock time. Read its documentation for full usage details
const char *clock_iso8601string_now(void) {
	return clock_iso8601string(clock_now());
}