]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
Makefile.am:
authorHarlan Stenn <stenn@ntp.org>
Sun, 18 Feb 2024 09:31:23 +0000 (01:31 -0800)
committerHarlan Stenn <stenn@ntp.org>
Sun, 18 Feb 2024 09:31:23 +0000 (01:31 -0800)
  distribute lsf-times.c in util/Makefile.am
lsf-times.c:
  new file
run-refnumtoa.c, run-bug-2803.c:
  Check in regenerated files

bk: 65d1ce6bUjVIefFY6QDimHXgRnYGMg

tests/bug-2803/run-bug-2803.c
tests/libntp/run-refnumtoa.c
util/Makefile.am
util/lsf-times.c [new file with mode: 0644]

index 0cc2e5f68429806ebda23a9c353fb042307b0e45..41c976a5d2564e3f1ef9e7b937d0bcc7f51e9654 100644 (file)
@@ -60,7 +60,7 @@ int main(int argc, char *argv[])
   progname = argv[0];
   suite_setup();
   UnityBegin("bug-2803.c");
-  RUN_TEST(test_main, 18);
+  RUN_TEST(test_main, 17);
 
   return (UnityEnd());
 }
index e56c9a1158e1be81794008f1c68f0d75f892345e..1b48534ecd1f1cbc4d223c8042bd06a953033735 100644 (file)
@@ -59,8 +59,8 @@ int main(int argc, char *argv[])
   progname = argv[0];
   suite_setup();
   UnityBegin("refnumtoa.c");
-  RUN_TEST(test_LocalClock, 13);
-  RUN_TEST(test_UnknownId, 14);
+  RUN_TEST(test_LocalClock, 10);
+  RUN_TEST(test_UnknownId, 11);
 
   return (UnityEnd());
 }
index 065fdf54738efa7cd8391a072600e9b3a2aec955..db0e8e65d747c6d539bc7b04d41b1e4a66052bae 100644 (file)
@@ -33,6 +33,7 @@ tickadj_LDADD=        ../libntp/libntp.a $(LDADD_LIBNTP) $(LIBM) $(PTHREAD_LIBS) $(LDAD
 EXTRA_DIST=                            \
        invoke-ntp-keygen.menu          \
        invoke-ntp-keygen.texi          \
+       lsf-times.c                     \
        ntp-keygen-opts.def             \
        ntp-keygen.1ntp-keygenman       \
        ntp-keygen.1ntp-keygenmdoc      \
diff --git a/util/lsf-times.c b/util/lsf-times.c
new file mode 100644 (file)
index 0000000..5b719e1
--- /dev/null
@@ -0,0 +1,220 @@
+#include "config.h"
+#include "ntp_calendar.h"
+
+#include <stdlib.h>
+#include <errno.h>
+
+#include "ntp_types.h"
+#include "ntp_fp.h"
+#include "vint64ops.h"
+
+/*
+ * If we're called with 1 arg, it's a u_long timestamp.
+ * If we're called with 3 args, we're expecting YYYY MM DD,
+ *   and MM must be 6 or 12, and DD must be 28,
+ * If we're called with 2 args, we're expecting YYYY MM, and
+ *   MM mst be 6 or 12, and we assume DD is 28.
+ */
+
+char *progname;
+static const char *MONTHS[] =
+                { "January", "February", "March", "April", "May", "June",
+                 "July", "August", "September", "October", "November",
+                 "December" };
+
+void usage(void);
+
+void
+usage(void)
+{
+       printf("Usage:\n");
+       printf(" %s nnnnnn\n", progname);
+       printf(" %s YYYY [6|12]\n", progname);
+       printf(" %s YYYY [6|12] 28\n", progname);
+
+       return;
+}
+
+
+int
+main(
+       int     argc,           /* command line options */
+       char    **argv          /* poiniter to list of tokens */
+       )
+{
+       int err = 0;
+       vint64 expires;
+       unsigned int year = 0;
+       unsigned int mon = 0;
+       unsigned int dom = 0;
+       int scount;
+       char *ep;
+       struct calendar cal = {0};
+
+       progname = argv[0];
+
+       switch(argc) {
+        case 2:        /* 1 arg, must be a string of digits */
+               expires = strtouv64(argv[1], &ep, 10);
+
+               if (0 == *ep) {
+                       ntpcal_ntp64_to_date(&cal, &expires);
+
+                       printf("%02u %s %04u %02u:%02u:%02u\n"
+                               , cal.monthday
+                               , MONTHS[cal.month - 1]
+                               , cal.year
+                               , cal.hour
+                               , cal.minute
+                               , cal.second
+                               );
+
+                       exit(0);
+               } else {
+                       printf("1 arg, but not a string of digits: <%s>\n",
+                               argv[1]);
+                       err = 1;
+               }
+               break;
+               ;;
+        case 3:        /* 2 args, must be YY MM, where MM is 6 or 12 */
+               dom = 28;
+               scount = sscanf(argv[1], "%u", &year);
+               if (1 == scount) {
+                       // printf("2 args: year %u\n", year);
+               } else {
+                       printf("2 args, but #1 is not a string of digits: <%s>\n", argv[1]);
+                       err = 1;
+               }
+
+               scount = sscanf(argv[2], "%u", &mon);
+               if (1 == scount) {
+                       if (6 == mon || 12 == mon) {
+                               // printf("2 args: month %u\n", mon);
+                       } else {
+                               printf("2 arg, but #2 is not 6 or 12: <%d>\n", mon);
+                               err = 1;
+                       }
+               } else {
+                       printf("2 arg, but #2 is not a string of digits: <%s>\n", argv[2]);
+                       err = 1;
+               }
+
+               break;
+               ;;
+        case 4:        /* 3 args, YY MM DD, where MM is 6 or 12, DD is 28 */
+               scount = sscanf(argv[1], "%u", &year);
+               if (1 == scount) {
+                       // printf("3 args: year %u\n", year);
+               } else {
+                       printf("3 args, but #1 is not a string of digits: <%s>\n", argv[1]);
+                       err = 1;
+               }
+
+               scount = sscanf(argv[2], "%u", &mon);
+               if (1 == scount) {
+                       if (6 == mon || 12 == mon) {
+                               // printf("3 args: month %u\n", mon);
+                       } else {
+                               printf("3 arg, but #2 is not 6 or 12: <%d>\n", mon);
+                               err = 1;
+                       }
+               } else {
+                       printf("3 arg, but #2 is not a string of digits: <%s>\n", argv[2]);
+                       err = 1;
+               }
+
+               scount = sscanf(argv[3], "%u", &dom);
+               if (1 == scount) {
+                       if (28 == dom) {
+                               // printf("3 args: dom %u\n", dom);
+                       } else {
+                               printf("3 arg, but #3 is not 28: <%d>\n", dom);
+                               err = 1;
+                       }
+               } else {
+                       printf("3 arg, but #3 is not a string of digits: <%s>\n", argv[2]);
+                       err = 1;
+               }
+
+               break;
+               ;;
+        default:
+               err = 1;
+               break;
+               ;;
+       }
+
+       if (err) {
+               usage();
+               exit(err);
+       }
+
+       cal.year = year;
+       cal.month = mon;
+       cal.monthday = dom;
+       cal.hour = 0;
+       cal.minute = 0;
+       cal.second = 0;
+
+       printf("%u ", ntpcal_date_to_ntp(&cal));
+
+       printf("%02d %s %04d "
+               , cal.monthday
+               , MONTHS[cal.month - 1]
+               , cal.year
+               );
+       printf("\n");
+
+       exit(err);
+}
+
+#if 0
+
+
+void
+test_DateGivenMonthDay(void) {
+       // 2010-06-24 12:50:00
+       struct calendar input = {2010, 0, 6, 24, 12, 50, 0};
+
+       u_long expected = 3486372600UL; // This is the timestamp above.
+
+       TEST_ASSERT_EQUAL_UINT(expected, caltontp(&input));
+}
+
+void
+test_DateGivenYearDay(void) {
+       // 2010-06-24 12:50:00
+       // This is the 175th day of 2010.
+       struct calendar input = {2010, 175, 0, 0, 12, 50, 0};
+
+       u_long expected = 3486372600UL; // This is the timestamp above.
+
+       TEST_ASSERT_EQUAL_UINT(expected, caltontp(&input));
+}
+
+void
+test_DateLeapYear(void) {
+       // 2012-06-24 12:00:00
+       // This is the 176th day of 2012 (since 2012 is a leap year).
+       struct calendar inputYd = {2012, 176, 0, 0, 12, 00, 00};
+       struct calendar inputMd = {2012, 0, 6, 24, 12, 00, 00};
+
+       u_long expected = 3549528000UL;
+
+       TEST_ASSERT_EQUAL_UINT(expected, caltontp(&inputYd));
+       TEST_ASSERT_EQUAL_UINT(expected, caltontp(&inputMd));
+}
+
+void
+test_WraparoundDateIn2036(void) {
+       // 2036-02-07 06:28:16
+       // This is (one) wrapping boundary where we go from ULONG_MAX to 0.
+       struct calendar input = {2036, 0, 2, 7, 6, 28, 16};
+
+       u_long expected = 0UL;
+
+       TEST_ASSERT_EQUAL_UINT(expected, caltontp(&input));
+}
+
+#endif