Fix trivial coding style nits in a_time/a_tm files
[openssl.git] / crypto / asn1 / a_time.c
index 6e3fade2ee6bf84bcd332b9491367542a3f73fa4..83c57ce7a660f3d0fc27fcbc9c47646e34b00151 100644 (file)
@@ -31,6 +31,38 @@ static int leap_year(const int year)
     return 0;
 }
 
+/*
+ * Compute the day of the week and the day of the year from the year, month
+ * and day.  The day of the year is straightforward, the day of the week uses
+ * a form of Zeller's congruence.  For this months start with March and are
+ * numbered 4 through 15.
+ */
+static void determine_days(struct tm *tm)
+{
+    static const int ydays[12] = {
+        0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
+    };
+    int y = tm->tm_year + 1900;
+    int m = tm->tm_mon;
+    int d = tm->tm_mday;
+    int c;
+
+    tm->tm_yday = ydays[m] + d - 1;
+    if (m >= 2) {
+        /* March and onwards can be one day further into the year */
+        tm->tm_yday += leap_year(y);
+        m += 2;
+    } else {
+        /* Treat January and February as part of the previous year */
+        m += 14;
+        y--;
+    }
+    c = y / 100;
+    y %= 100;
+    /* Zeller's congruance */
+    tm->tm_wday = (d + (13 * m) / 5 + y + y / 4 + c / 4 + 5 * c + 6) % 7;
+}
+
 int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *d)
 {
     static const int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 };
@@ -127,6 +159,7 @@ int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *d)
             if (n > md)
                 goto err;
             tmp.tm_mday = n;
+            determine_days(&tmp);
             break;
         case 4:
             tmp.tm_hour = n;
@@ -264,8 +297,9 @@ ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(const ASN1_TIME *t,
     if (out == NULL || *out == NULL) {
         if ((ret = ASN1_GENERALIZEDTIME_new()) == NULL)
             goto err;
-    } else
+    } else {
         ret = *out;
+    }
 
     /* If already GeneralizedTime just copy across */
     if (t->type == V_ASN1_GENERALIZEDTIME) {
@@ -316,7 +350,7 @@ int ASN1_TIME_set_string(ASN1_TIME *s, const char *str)
             return 0;
     }
 
-    if (s && !ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t))
+    if (s != NULL && !ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t))
         return 0;
 
     return 1;
@@ -388,7 +422,7 @@ int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm)
 
         time(&now_t);
         memset(tm, 0, sizeof(*tm));
-        if (OPENSSL_gmtime(&now_t, tm))
+        if (OPENSSL_gmtime(&now_t, tm) != NULL)
             return 1;
         return 0;
     }
@@ -415,5 +449,5 @@ int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
     if (tm->type == V_ASN1_GENERALIZEDTIME)
         return ASN1_GENERALIZEDTIME_print(bp, tm);
     BIO_write(bp, "Bad time value", 14);
-    return (0);
+    return 0;
 }