Skip to content

Issue with timelib_get_status() #1

Open
@tpcorrea

Description

@tpcorrea

Hi! First of all, thanks for sharing this library. I am using it to convert time from human to epoch format and back together with a local RTC. I got an issue when trying to get the status to know if sending a NTP request was necessary or not.

My basic code is

if (E_TIME_OK == (time_status)timelib_get_status())
  {
    app.setTime(timelib_get());
  }

And I got a wrong status when calling timelib_get_status(). Checking the code, I noticed that the update of sys_time is done AFTER checking if sync_time <= sys_time. In this way, the first time we call timelib_get_status() will always return E_TIME_OK, since the sys_time wasn't updated yet. Hence, I believe the best implementation is to FIRST update sys_time to LATER check if we need a time sync:

timelib_t timelib_get()
{
	timelib_t now = 0;

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

	// Check how many seconds have elapsed (if any) since the last call
	// and update the timestamp counter
	while (tick_get() - last_update >= TICK_SECOND) {
		// Increment timestamp
		sys_time++;
		last_update += TICK_SECOND;
	}
	
	// Check if time needs sync to timebase
	if (sync_next <= sys_time) {
		// Null pointer check
		if (timelib_provider_callback != 0) {
			// Invoke callback function
			now = timelib_provider_callback();
			// Got time from callback?
			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;
			}
		}
	}

	return sys_time;
}

Am I missing something or we can do this modification?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions