Skip to content

Commit

Permalink
Refactor function names and constants to be consistent with naming co…
Browse files Browse the repository at this point in the history
…nventions
  • Loading branch information
geekfactory committed May 30, 2018
1 parent c1dcc92 commit 138ba8d
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 140 deletions.
152 changes: 76 additions & 76 deletions TimeLib.c
@@ -1,4 +1,4 @@
/* Time management library for embedded devices
/* TimeLib - Time management library for embedded devices
Copyright (C) 2014 Jesus Ruben Santa Anna Zamudio.
This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -26,17 +26,17 @@ bool halt = false;
const unsigned char month_length[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

/* Unix like time counter, keeps track of absolute time */
time_t sys_time = 0;
timelib_t sys_time = 0;

/* The interval in seconds after which the system time variable should be updated
* (synced) with an external (accurate) time base */
time_t sync_interval = 86400;
timelib_t sync_interval = 86400;

/* Unix timestamp when the sync should be done. */
time_t sync_next = 0;
timelib_t sync_next = 0;

/* Cache for current time */
time_t tcache;
timelib_t tcache;
struct tm telements;

/* Variable used to keep track of the last time the "seconds" or sys_time counter
Expand All @@ -47,11 +47,11 @@ unsigned long last_update = 0;
enum time_status tstatus = E_TIME_NOT_SET;

/**
* Stores a pointer to a function that returns a precise unix timestamp to set
* the internal clock to the returned timestamp. This update ocurs at the
* Stores a pointer to a function that returns a precise Unix timestamp to set
* the internal clock to the returned timestamp. This update occurs at the
* interval given when the callback is configured.
*/
time_callback_t time_provider_callback = 0;
timelib_callback_t timelib_provider_callback = 0;

/**
* @brief Computes if the given year is a leap year
Expand All @@ -60,7 +60,7 @@ time_callback_t time_provider_callback = 0;
*
* @return Returns true if "year" is leap, false otherwise
*/
static int time_is_leap(unsigned int year)
static int timelib_is_leap(unsigned int year)
{
// Must be divisible by 4 but not by 100, unless it is divisible by 400
return((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
Expand All @@ -71,43 +71,43 @@ static int time_is_leap(unsigned int year)
*
* @param time The timestamp now.
*/
static void time_update(time_t time)
static void timelib_update(timelib_t time)
{
if (tcache != time) {
tcache = time;
time_break(time, &telements);
timelib_break(time, &telements);
}
}

/*-------------------------------------------------------------*/
/* Public API, check TimeLib.h for documentation */
/*-------------------------------------------------------------*/
void time_set(time_t now)
/*-------------------------------------------------------------*
* Public API, check TimeLib.h for documentation *
*-------------------------------------------------------------*/
void timelib_set(timelib_t now)
{
sys_time = now;
sync_next = now + sync_interval;
tstatus = E_TIME_OK;
last_update = tick_get();
}

time_t time_get()
timelib_t timelib_get()
{
time_t now = 0;
timelib_t now = 0;

// Clock halted, return always the same value (no update)
if(halt==true)
if (halt == true)
return sys_time;

// Check if time needs sync to timebase
if (sync_next <= sys_time) {
// Null pointer check
if (time_provider_callback != 0) {
if (timelib_provider_callback != 0) {
// Invoke callback function
now = time_provider_callback();
now = timelib_provider_callback();
// Got time from callback?
if (now != 0)
time_set(now);
else {
if (now != 0) {
timelib_set(now);
} else {
sync_next = sys_time + sync_interval;
tstatus = (tstatus == E_TIME_NOT_SET) ? E_TIME_NOT_SET : E_TIME_NEEDS_SYNC;
}
Expand All @@ -125,130 +125,130 @@ time_t time_get()
return sys_time;
}

void time_halt_clock()
void timelib_halt_clock()
{
halt = true;
}

void time_resume_clock()
void timelib_resume_clock()
{
halt = false;
}

uint8_t time_get_status()
uint8_t timelib_get_status()
{
time_get();
timelib_get();
return tstatus;
}

uint8_t time_second_t(time_t time)
uint8_t timelib_second_t(timelib_t time)
{
time_update(time);
timelib_update(time);
return telements.tm_sec;
}

uint8_t time_minute_t(time_t time)
uint8_t timelib_minute_t(timelib_t time)
{
time_update(time);
timelib_update(time);
return telements.tm_min;
}

uint8_t time_hour_t(time_t time)
uint8_t timelib_hour_t(timelib_t time)
{
time_update(time);
timelib_update(time);
return telements.tm_hour;
}

uint8_t time_wday_t(time_t time)
uint8_t timelib_wday_t(timelib_t time)
{
time_update(time);
timelib_update(time);
return telements.tm_wday;
}

uint8_t time_day_t(time_t time)
uint8_t timelib_day_t(timelib_t time)
{
time_update(time);
timelib_update(time);
return telements.tm_mday;
}

uint8_t time_month_t(time_t time)
uint8_t timelib_month_t(timelib_t time)
{
time_update(time);
timelib_update(time);
return telements.tm_mon;
}

uint16_t time_year_t(time_t time)
uint16_t timelib_year_t(timelib_t time)
{
time_update(time);
timelib_update(time);
return telements.tm_mon;
}

uint8_t time_second()
uint8_t timelib_second()
{
return time_second_t(time_get());
return timelib_second_t(timelib_get());
}

uint8_t time_minute()
uint8_t timelib_minute()
{
return time_minute_t(time_get());
return timelib_minute_t(timelib_get());
}

uint8_t time_hour()
uint8_t timelib_hour()
{
return time_hour_t(time_get());
return timelib_hour_t(timelib_get());
}

uint8_t time_wday()
uint8_t timelib_wday()
{
return time_wday_t(time_get());
return timelib_wday_t(timelib_get());
}

uint8_t time_day()
uint8_t timelib_day()
{
return time_day_t(time_get());
return timelib_day_t(timelib_get());
}

uint8_t time_month()
uint8_t timelib_month()
{
return time_month_t(time_get());
return timelib_month_t(timelib_get());
}

uint16_t time_year()
uint16_t timelib_year()
{
return time_year_t(time_get());
return timelib_year_t(timelib_get());
}

time_t time_make(struct tm * timeinfo)
timelib_t timelib_make(struct tm * timeinfo)
{
int i;
uint32_t tstamp;
timelib_t tstamp;

// Compute the number of seconds since the year 1970 to the begining of
// the given year on the structure, add to the output value
tstamp = timeinfo->tm_year * (TIME_SECS_PER_DAY * 365);
tstamp = timeinfo->tm_year * (TIMELIB_SECS_PER_DAY * 365);
// Add the seconds corresponding to leap years (add extra days)
for (i = 0; i < timeinfo->tm_year; i++) {
if (time_is_leap(i + 1970))
tstamp += TIME_SECS_PER_DAY;
if (timelib_is_leap(i + 1970))
tstamp += (timelib_t) TIMELIB_SECS_PER_DAY;
}
// Add secconds for the monts elapsed
// Add seconds for the months elapsed
for (i = 1; i < timeinfo->tm_mon; i++) {
if (i == 2 && time_is_leap(timeinfo->tm_year + 1970))
tstamp += TIME_SECS_PER_DAY * 29;
if (i == 2 && timelib_is_leap(timeinfo->tm_year + 1970))
tstamp += (timelib_t) TIMELIB_SECS_PER_DAY * 29;
else
tstamp += TIME_SECS_PER_DAY * month_length[i - 1];
tstamp += (timelib_t) TIMELIB_SECS_PER_DAY * month_length[i - 1];
}
// Add seconds for past days
tstamp += (uint32_t) (timeinfo->tm_mday - 1) * (uint32_t) TIME_SECS_PER_DAY;
tstamp += (timelib_t) (timeinfo->tm_mday - 1) * (timelib_t) TIMELIB_SECS_PER_DAY;
// Add seconds on this day
tstamp += (uint32_t) timeinfo->tm_hour * (uint32_t) TIME_SECS_PER_HOUR;
tstamp += (uint32_t) timeinfo->tm_min * (uint32_t) TIME_SECS_PER_MINUTE;
tstamp += (uint32_t) timeinfo->tm_sec;
tstamp += (timelib_t) timeinfo->tm_hour * (timelib_t) TIMELIB_SECS_PER_HOUR;
tstamp += (timelib_t) timeinfo->tm_min * (timelib_t) TIMELIB_SECS_PER_MINUTE;
tstamp += (timelib_t) timeinfo->tm_sec;

return tstamp;
}

void time_break(time_t timeinput, struct tm * timeinfo)
void timelib_break(timelib_t timeinput, struct tm * timeinfo)
{
uint8_t year;
uint8_t month, monthLength;
Expand All @@ -269,12 +269,12 @@ void time_break(time_t timeinput, struct tm * timeinfo)

year = 0;
days = 0;
while ((unsigned) (days += (time_is_leap(1970 + year) ? 366 : 365)) <= time)
while ((unsigned) (days += (timelib_is_leap(1970 + year) ? 366 : 365)) <= time)
year++;

timeinfo->tm_year = year; // year is offset from 1970

days -= time_is_leap(1970 + year) ? 366 : 365;
days -= timelib_is_leap(1970 + year) ? 366 : 365;
time -= days; // now it is days in this year, starting at 0

days = 0;
Expand All @@ -283,7 +283,7 @@ void time_break(time_t timeinput, struct tm * timeinfo)
for (month = 0; month < 12; month++) {
// Check
if (month == 1) {
if (time_is_leap(1970 + year)) {
if (timelib_is_leap(1970 + year)) {
monthLength = 29;
} else {
monthLength = 28;
Expand All @@ -302,17 +302,17 @@ void time_break(time_t timeinput, struct tm * timeinfo)
timeinfo->tm_mday = time + 1; // day of month
}

void time_set_provider(time_callback_t callback, time_t timespan)
void timelib_set_provider(timelib_callback_t callback, timelib_t timespan)
{
// Check null pointer
if (callback == 0)
return;
// Set new callback
time_provider_callback = callback;
timelib_provider_callback = callback;
// Enforce sync interval restrictions
sync_interval = (timespan == 0) ? TIME_SECS_PER_DAY : timespan;
sync_interval = (timespan == 0) ? TIMELIB_SECS_PER_DAY : timespan;
//Set next sync time to actual time
sync_next = sys_time;
// Force time sync
time_get();
timelib_get();
}

0 comments on commit 138ba8d

Please sign in to comment.