From b4b4a88a4e0809c1c09b25129cb896846656b46e Mon Sep 17 00:00:00 2001 From: Mark Pizzolato Date: Wed, 13 Apr 2016 12:12:29 -0700 Subject: [PATCH] MicroVAX2: Fix watch chip implementation - In VMS mode, the day and month have to behave correctly to map the current day of year to the equivalent day of year in 1982 - The month maintained and returned by the watch chip has January as 1 while the tm_mon field in the 'struct tm' has 0 for January. --- VAX/vax_watch.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/VAX/vax_watch.c b/VAX/vax_watch.c index 994026a2..e69ca610 100644 --- a/VAX/vax_watch.c +++ b/VAX/vax_watch.c @@ -162,6 +162,7 @@ int32 wtc_rd (int32 pa) int32 rg = (pa >> 1) & 0xF; int32 val = 0; time_t curr; +static int mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; struct tm *ctm = NULL; if (rg < 10) { /* time reg? */ @@ -171,6 +172,22 @@ if (rg < 10) { /* time reg? */ ctm = localtime (&curr); /* decompose */ if (ctm == NULL) /* error? */ return 0; + if ((wtc_mode == WTC_MODE_VMS) && + ((ctm->tm_year % 4) == 0)) { /* Leap Year? */ + if (ctm->tm_mon > 1) { /* Past February? */ + ++ctm->tm_mday; /* Adjust for Leap Day */ + if (ctm->tm_mday > mdays[ctm->tm_mon]) { /* wrap to last day of prior month */ + ++ctm->tm_mon; + ctm->tm_mday = 1; + } + } + else + if ((ctm->tm_mon == 1) && /* February 29th? */ + (ctm->tm_mday == 29)) { + ctm->tm_mon = 2; /* Is March 1 in 1982 */ + ctm->tm_mday = 1; + } + } } switch(rg) { @@ -196,7 +213,7 @@ switch(rg) { break; case 8: /* month */ - val = ctm->tm_mon; + val = ctm->tm_mon + 1; break; case 9: /* year */