I wish to create a Unix time stamp. I have a micro-controller with a ble connection that I send the present time to via a GATT connection. I receive the data several integers.
These are Year, Month, Day, Hour, Minute and Second. e.g:
- Year = 2020, month = 3, day = 9, hour = 10, minute = 10, second = 10.
I want to convert this to Unix time so I can then increment the value every second via a interrupt based timer. Therefore, what is the correct way to conjoin this data together to form the Unix time. My presumption is as follows:
Create a integer value large enough to store the value called unix time and add the equivalent seconds from the respective time components(year,month etc).
These are the value in seconds
- Year_in_seconds = 31,556,952
- Month_in_seconds = 2,630,000
- Day_in_seconds = 86,400
- Hour_in_seconds = 3,600
- Minute_in_seconds = 60
Therefore, Unix time =
- (nYears * Year_in_seconds)+(nMonths * Month_in_seconds)+(nDays * Days_in_seconds)+(nHours * Hour_in_seconds)+(nMinutes * Minute_in_seconds)+(nSeconds * 1)
Is there anything wrong about my presumptions here?
I don't know how unix time deals with the different number of days in a month as different months have different lengths. Also leap years. So do I need another lookup table or something to make this accurate?
Or what is the correct method for getting the unix time from my set of data?