RTC time to UNIX time and EEPROM

Status
Not open for further replies.

bianchi77

Guys,
Do you have any ideas on how to convert RTC time into unix time and save it into eeprom ?
Thanks

betwixt

Super Moderator
Staff member
This is the code I use, it is NOT the same as unixtime but it does produce a unique value for every time and date. The real unixtime calculation is quite complicated:
Code:
//*******************************************************************************
// calculate unique seconds number based on 0 being 00:00 01-01-2010
unsigned long GetSecs(unsigned char Year, unsigned char Month, unsigned char Day, unsigned char Hour, unsigned char Min, unsigned char Sec)
{
unsigned long USecs = 0;

USecs = ((ulong)Year - 10) * 29462400;	//seconds in 1 year (12 months of 31 days)
USecs += ((ulong)Month - 1) * 2678400;	//seconds in one month (31 days)
USecs += ((ulong)Day - 1) * 86400;	//seconds in one day
USecs += (ulong)Hour * 3600;		//seconds in one hour
USecs += (ulong)Min * 60);			//seconds in one minute
return (USecs + (ulong)Sec);			//+ seconds
}

For simplicity, it assumes that every month has 31 days, this removes the need to work out the days in each month and whether it's a leap year. It means there will be gaps in the numbers produced sometimes but it's good for 90 years or so. Simply reverse the calculation to recover the original date and time.

Brian.

Status
Not open for further replies.