Convert timestamp to unix timestamp to update Time

I have a particle photon ina suctom board. In that board I have a ds3231 too. I know I could use the internal RTC with a coin battery but as our board already had a ds3231 I want to use it.

My device could be connected to the internet or not (some of them will never be connected to). So I can’t assume it will correct the timestamp from time to time. But in case it has connectivity, I could update ds3231 time to fix any deviation.

How can I get a timestamp and convert to unix format? I don’t know if there is any function in the API or I should do my own. (This way I can get the time from my RTC and update the photon’s rtc value)

You can use the standard C functions like mktime()

But are you sure you need translation?
How would you set the DS3231 time?

You could use this statement (see below), or not?

  ds3232.setDateTime(Time.year(), Time.month(), Time.day(), Time.hour(), Time.minute(), Time.second());

And this is what’s done in an Arduino library for the DS1337 & DS3231 to do the UNIX epech time translation into the DS format and back


/**
 * Convert from unix timestamp
 */
void DS1337::getTime(unsigned long timestamp, int &year, int &month, int &day, int &hour, int &minute, int &second) {
	timestamp-= T2000UTC;
	second = (int)(timestamp % 60);
	timestamp = timestamp / 60;
	minute = (int)(timestamp % 60);
	timestamp = timestamp / 60;
	hour = (int)(timestamp % 24);
	timestamp = timestamp / 24;
	year = 0;
	int dYear = 0;
	if (year % 4 == 0)
		dYear = 366;
	else
		dYear = 365;
	while (timestamp >= dYear) {
		year++;
		timestamp-=dYear;
		if (year % 4 == 0)
			dYear = 366;
		else
			dYear = 365;
	}
	day = (int) timestamp;
	int dayMonth[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
	month = 0;
	while (day > dayMonth[month+1])
		month++;
	day-=dayMonth[month];
	month++;
	day++;
}

/**
 * Get unix timestamp
 */
unsigned long DS1337::getTimestamp(int year, int month, int day, int hour, int minute, int second) {
  unsigned long secMinute = 60;
  unsigned long secHour = 60 * secMinute;
  unsigned long secDay = 24 * secHour;
  unsigned long secYear = 365 * secDay;
  int dayMonth[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};

  // leap years
  int leapDays = (year >> 2) + 1;
  if (((year & 3) == 0) && month < 3)
    leapDays--;

  // calculate
  return year * secYear + (dayMonth[month-1] + (day-1) + leapDays) * secDay + hour * secHour + minute * secMinute + second + T2000UTC;
}

I have no problems setting the time on the DS3231. What I want is to set the time in ds3231 to the particle photon rtc. In case the device starts without internet connectivity or the current time in the internal rtc deviates too much, I want to be able to reset that time.

Checking the documentation I have seen that I can reset the time with a timestamp in unix fomat. But I don’t know how I can convert the time I get from the ds3231 to unix format. I get, hour, minute, seconds, day, month and year…

In this case you can use the function I quoted above (using that linked library or checking whether the lib you are already using - which would be good to disclose initially - supports it too might be worth considering)

(these code snippets are there to be read, not just for my own pleasure)

or one of the relatives of mktime() linked in the cplusplus link I also posted above.

Thank you, I will check the code for that function and try to include in my library. My library has been developed by me. It just contains functions to set the time and read it. It is not as complete as the one in particle libs or the arduino one.

1 Like