Skip to content
Snippets Groups Projects
Commit 78df6a80 authored by Adam Wujek's avatar Adam Wujek
Browse files

userspace/tools/wr_irig: use mktime for day in month calculations


Signed-off-by: default avatarAdam Wujek <dev_public@wujek.eu>
parent 267965be
Branches
Tags
No related merge requests found
...@@ -125,47 +125,45 @@ static int irig_get_date(struct irig_slave *irig, struct irig_time *t){ ...@@ -125,47 +125,45 @@ static int irig_get_date(struct irig_slave *irig, struct irig_time *t){
uint32_t yr_raw = (date & IRIG_SLAVE_DATE_YEARS_MASK) >> IRIG_SLAVE_DATE_YEARS_SHIFT; uint32_t yr_raw = (date & IRIG_SLAVE_DATE_YEARS_MASK) >> IRIG_SLAVE_DATE_YEARS_SHIFT;
uint32_t valid = (date & IRIG_SLAVE_DATE_VALID); uint32_t valid = (date & IRIG_SLAVE_DATE_VALID);
uint32_t days = (day_raw & 0x0f); uint32_t ydays = (day_raw & 0x0f);
uint32_t yrs = (yr_raw & 0x0f); uint32_t yrs = (yr_raw & 0x0f);
uint32_t mon = 0;
const int m_to_d[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, struct tm t0;
334};
int ret;
int i = 0; int i = 0;
for (i = 0; i < 6; i++) { for (i = 0; i < 6; i++) {
if ((day_raw >> 5) & 1 << i) { if ((day_raw >> 5) & 1 << i) {
if (i >= 4) { if (i >= 4) {
days += 100 * (1 << (i - 4)); ydays += 100 * (1 << (i - 4));
} else { } else {
days += 10 * (1 << i); ydays += 10 * (1 << i);
} }
} }
} }
//fixme
days -= 1;
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
if ((yr_raw >> 5) & (1 << i)){ if ((yr_raw >> 5) & (1 << i)){
yrs += 10 * (1 << i); yrs += 10 * (1 << i);
} }
} }
yrs += 2000; memset(&t0, 0, sizeof(struct tm));
t0.tm_isdst = -1;
for (i = 0; i < 11; i++){ /* mktime counts year from 1900, irigb counts from 2000 */
if (days >= m_to_d[i] && days < m_to_d[i + 1]) { t0.tm_year = yrs + 100;
mon = i+1; /* Give days in a year as days in a month (tm_mday) to mktime to give
days -= m_to_d[i]; * month/day */
} t0.tm_mday = ydays;
} ret = mktime(&t0);
if (valid){ if (valid && ret > 0){
t->day = days; t->day = t0.tm_mday;
t->mon = mon; t->mon = t0.tm_mon + 1; /* t0.tm_mon is 0..11 */
t->year = yrs; t->year = t0.tm_year + 1900;
return 0; return 0;
} }
return -1; return -1;
} }
...@@ -194,35 +192,18 @@ static int irig_get_sbs(struct irig_slave *irig, struct irig_time *t) ...@@ -194,35 +192,18 @@ static int irig_get_sbs(struct irig_slave *irig, struct irig_time *t)
static int64_t irig_time_to_seconds(struct irig_time *t) static int64_t irig_time_to_seconds(struct irig_time *t)
{ {
short month, year;
int64_t result; int64_t result;
const int m_to_d[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, struct tm t0;
334 };
memset(&t0, 0, sizeof(struct tm));
month = t->mon; t0.tm_isdst = -1;
year = t->year + month / 12 + 1900; t0.tm_year = t->year;
month %= 12; t0.tm_mon = t->mon;
if (month < 0) { t0.tm_mday = t->day;
year -= 1; t0.tm_hour = t->hour;
month += 12; t0.tm_min = t->min;
} t0.tm_sec = t->sec;
result = (year - 1970) * 365 + (year - 1969) / 4 + m_to_d[month]; result = mktime(&t0);
result = (year - 1970) * 365 + m_to_d[month];
if (month <= 1)
year -= 1;
result += (year - 1968) / 4;
result -= (year - 1900) / 100;
result += (year - 1600) / 400;
result += t->day;
result -= 1;
result *= 24;
result += t->hour;
result *= 60;
result += t->min;
result *= 60;
result += t->sec;
return result; return result;
} }
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment