summaryrefslogtreecommitdiff
path: root/openssl.c
diff options
context:
space:
mode:
authorWilliam Ahern <william@server.local>2012-10-03 18:36:54 -0700
committerWilliam Ahern <william@server.local>2012-10-03 18:36:54 -0700
commit6a069b6d93892bfce6be1ca368887835cd4ccbfa (patch)
treef7fea530a59bde4f9cc85c576b2e381727056460 /openssl.c
parentf0f38200ba99a767ebe0db5d171c98f25d4dbe54 (diff)
downloadluaossl-6a069b6d93892bfce6be1ca368887835cd4ccbfa.tar.gz
luaossl-6a069b6d93892bfce6be1ca368887835cd4ccbfa.tar.bz2
luaossl-6a069b6d93892bfce6be1ca368887835cd4ccbfa.zip
-n
refactor time routines a little
Diffstat (limited to 'openssl.c')
-rw-r--r--openssl.c33
1 files changed, 23 insertions, 10 deletions
diff --git a/openssl.c b/openssl.c
index 3ae9a9f..7ec8b73 100644
--- a/openssl.c
+++ b/openssl.c
@@ -828,10 +828,15 @@ static int yday(int year, int mon, int mday) {
828 static const int past[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; 828 static const int past[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
829 int yday = past[CLAMP(mon, 0, 11)] + CLAMP(mday, 1, 31) - 1; 829 int yday = past[CLAMP(mon, 0, 11)] + CLAMP(mday, 1, 31) - 1;
830 830
831 return yday + (isleap(year) && mon > 1); 831 return yday + (mon > 1 && isleap(year));
832} /* yday() */ 832} /* yday() */
833 833
834 834
835static int tm_yday(const struct tm *tm) {
836 return (tm->tm_yday)? tm->tm_yday : yday(1900 + tm->tm_year, tm->tm_mon, tm->tm_mday);
837} /* tm_yday() */
838
839
835static int leaps(int year) { 840static int leaps(int year) {
836 if (year >= 0) 841 if (year >= 0)
837 return (year / 400) + (year / 4) - (year / 100); 842 return (year / 400) + (year / 4) - (year / 100);
@@ -840,6 +845,22 @@ static int leaps(int year) {
840} /* leaps() */ 845} /* leaps() */
841 846
842 847
848static double tm2unix(const struct tm *tm, int gmtoff) {
849 int year = tm->tm_year + 1900;
850 double ts;
851
852 ts = 86400.0 * 365.0 * (year - 1970);
853 ts += 86400.0 * (leaps(year - 1) - leaps(1969));
854 ts += 86400 * tm_yday(tm);
855 ts += 3600 * tm->tm_hour;
856 ts += 60 * tm->tm_min;
857 ts += CLAMP(tm->tm_sec, 0, 59);
858 ts += (year < 1970)? gmtoff : -gmtoff;
859
860 return ts;
861} /* tm2unix() */
862
863
843static _Bool scan(int *i, char **cp, int n, int signok) { 864static _Bool scan(int *i, char **cp, int n, int signok) {
844 int sign = 1; 865 int sign = 1;
845 866
@@ -932,15 +953,7 @@ static double timeutc(ASN1_TIME *time) {
932 gmtoff *= sign; 953 gmtoff *= sign;
933 } 954 }
934 955
935 ts = 86400.0 * 365.0 * (year - 1970); 956 return tm2unix(&tm, gmtoff);
936 ts += 86400.0 * (leaps(year - 1) - leaps(1969));
937 ts += 86400 * tm.tm_yday;
938 ts += 3600 * tm.tm_hour;
939 ts += 60 * tm.tm_min;
940 ts += tm.tm_sec;
941 ts += (year < 1970)? gmtoff : -gmtoff;
942
943 return ts;
944badfmt: 957badfmt:
945 return INFINITY; 958 return INFINITY;
946} /* timeutc() */ 959} /* timeutc() */