From: Sergey Matveev Date: Wed, 20 Nov 2024 14:02:25 +0000 (+0300) Subject: Revised and corrected work with TAI64 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=7292351228d72829349e161ec0d073644c56e3c930691c5fdcef142faae510db;p=keks.git Revised and corrected work with TAI64 --- diff --git a/cyac/lib/dectai.c b/cyac/lib/dectai.c index e0d0b9d..1768c95 100644 --- a/cyac/lib/dectai.c +++ b/cyac/lib/dectai.c @@ -48,11 +48,16 @@ enum YACErr YACTimespecToUTC(struct timespec *ts) { int64_t v = (int64_t)(ts->tv_sec); + enum YACErr err = YACErrNo; { - int64_t diff = 0; + int64_t diff = 10; for (int64_t i = 0; i < YACLeapsecsN; i++) { - if (v > (YACLeapsecs[i] + YACLeapsecsN - i)) { - diff = 10 + YACLeapsecsN - i; + if (v < YACLeapsecs[i]) { + break; + } + diff++; + if (v == YACLeapsecs[i]) { + err = YACErrTAI64IsLeap; break; } } @@ -62,5 +67,5 @@ YACTimespecToUTC(struct timespec *ts) return YACErrTAI64InPast; } ts->tv_sec = (time_t)v; - return YACErrNo; + return err; } diff --git a/cyac/lib/enctai.c b/cyac/lib/enctai.c index f9956f8..e175e30 100644 --- a/cyac/lib/enctai.c +++ b/cyac/lib/enctai.c @@ -39,14 +39,16 @@ bool YACTimespecToTAI(struct timespec *ts) { int64_t v = (int64_t)(ts->tv_sec); - int64_t diff = 10; - for (int64_t i = 0; i < YACLeapsecsN; i++) { - if (v > YACLeapsecs[i]) { - diff += YACLeapsecsN - i; + int64_t i = 0; + for (; i < YACLeapsecsN; i++) { + if (v < YACLeapsecs[i]) { break; } } - v += diff; + v += (int64_t)10 + i; + if (v == YACLeapsecs[i]) { + v++; + } if (((uint64_t)1 << ((sizeof(time_t) * 8) - 1)) < (uint64_t)v) { return false; } diff --git a/cyac/lib/err.c b/cyac/lib/err.c index 8e199c5..c951942 100644 --- a/cyac/lib/err.c +++ b/cyac/lib/err.c @@ -32,6 +32,8 @@ YACErr2Str(const enum YACErr err) return "TAI64BadAsec"; case YACErrTAI64InPast: return "TAI64InPast"; + case YACErrTAI64IsLeap: + return "TAI64IsLeap"; case YACErrMapBadKey: return "MapBadKey"; case YACErrMapNoVal: diff --git a/cyac/lib/err.h b/cyac/lib/err.h index f3b8c28..00726a6 100644 --- a/cyac/lib/err.h +++ b/cyac/lib/err.h @@ -56,6 +56,7 @@ enum YACErr { YACErrTAI64BadNsec, YACErrTAI64BadAsec, YACErrTAI64InPast, + YACErrTAI64IsLeap, YACErrMapBadKey, YACErrMapNoVal, YACErrMapUnordered, diff --git a/cyac/lib/leapsecs.c b/cyac/lib/leapsecs.c index 7f703d4..ec7fef8 100644 --- a/cyac/lib/leapsecs.c +++ b/cyac/lib/leapsecs.c @@ -4,31 +4,31 @@ const int64_t YACLeapsecsN = 27; const int64_t YACLeapsecs[] = { - 1483228800, // 2017-01 - 1435708800, // 2015-07 - 1341100800, // 2012-07 - 1230768000, // 2009-01 - 1136073600, // 2006-01 - 915148800, // 1999-01 - 867715200, // 1997-07 - 820454400, // 1996-01 - 773020800, // 1994-07 - 741484800, // 1993-07 - 709948800, // 1992-07 - 662688000, // 1991-01 - 631152000, // 1990-01 - 567993600, // 1988-01 - 489024000, // 1985-07 - 425865600, // 1983-07 - 394329600, // 1982-07 - 362793600, // 1981-07 - 315532800, // 1980-01 - 283996800, // 1979-01 - 252460800, // 1978-01 - 220924800, // 1977-01 - 189302400, // 1976-01 - 157766400, // 1975-01 - 126230400, // 1974-01 - 94694400, // 1973-01 - 78796800 // 1972-07 + 78796810, // 1972-07 + 94694411, // 1973-01 + 126230412, // 1974-01 + 157766413, // 1975-01 + 189302414, // 1976-01 + 220924815, // 1977-01 + 252460816, // 1978-01 + 283996817, // 1979-01 + 315532818, // 1980-01 + 362793619, // 1981-07 + 394329620, // 1982-07 + 425865621, // 1983-07 + 489024022, // 1985-07 + 567993623, // 1988-01 + 631152024, // 1990-01 + 662688025, // 1991-01 + 709948826, // 1992-07 + 741484827, // 1993-07 + 773020828, // 1994-07 + 820454429, // 1996-01 + 867715230, // 1997-07 + 915148831, // 1999-01 + 1136073632, // 2006-01 + 1230768033, // 2009-01 + 1341100834, // 2012-07 + 1435708835, // 2015-07 + 1483228836, // 2017-01 }; diff --git a/cyac/lib/leapsecs.h b/cyac/lib/leapsecs.h index 71c0efe..726c797 100644 --- a/cyac/lib/leapsecs.h +++ b/cyac/lib/leapsecs.h @@ -1,7 +1,6 @@ #ifndef YAC_LEAPSECS_H #define YAC_LEAPSECS_H -#include #include // TEXINFO: YACLeapsecs