diff --git a/userspace/tools/wr_irig.c b/userspace/tools/wr_irig.c index 97817b8ebff9bc53d537a481c4880ef53316d3b6..b340bcfb6d95aa63baeeb3c3334a9d22bf3160ed 100644 --- a/userspace/tools/wr_irig.c +++ b/userspace/tools/wr_irig.c @@ -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 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 mon = 0; - const int m_to_d[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, - 334}; + struct tm t0; + + int ret; int i = 0; for (i = 0; i < 6; i++) { if ((day_raw >> 5) & 1 << i) { if (i >= 4) { - days += 100 * (1 << (i - 4)); + ydays += 100 * (1 << (i - 4)); } else { - days += 10 * (1 << i); + ydays += 10 * (1 << i); } } } - //fixme - days -= 1; - for (i = 0; i < 4; i++) { if ((yr_raw >> 5) & (1 << i)){ yrs += 10 * (1 << i); } } - yrs += 2000; - - for (i = 0; i < 11; i++){ - if (days >= m_to_d[i] && days < m_to_d[i + 1]) { - mon = i+1; - days -= m_to_d[i]; - } - } - - if (valid){ - t->day = days; - t->mon = mon; - t->year = yrs; + memset(&t0, 0, sizeof(struct tm)); + t0.tm_isdst = -1; + /* mktime counts year from 1900, irigb counts from 2000 */ + t0.tm_year = yrs + 100; + /* Give days in a year as days in a month (tm_mday) to mktime to give + * month/day */ + t0.tm_mday = ydays; + ret = mktime(&t0); + + if (valid && ret > 0){ + t->day = t0.tm_mday; + t->mon = t0.tm_mon + 1; /* t0.tm_mon is 0..11 */ + t->year = t0.tm_year + 1900; return 0; } + return -1; } @@ -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) { - short month, year; int64_t result; - const int m_to_d[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, - 334 }; - - month = t->mon; - year = t->year + month / 12 + 1900; - month %= 12; - if (month < 0) { - year -= 1; - month += 12; - } - result = (year - 1970) * 365 + (year - 1969) / 4 + m_to_d[month]; - 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; + struct tm t0; + + memset(&t0, 0, sizeof(struct tm)); + t0.tm_isdst = -1; + t0.tm_year = t->year; + t0.tm_mon = t->mon; + t0.tm_mday = t->day; + t0.tm_hour = t->hour; + t0.tm_min = t->min; + t0.tm_sec = t->sec; + result = mktime(&t0); return result; }