2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 * tpTime.c - cert related time functions
22 * Written 10/10/2000 by Doug Mitchell.
33 * Given a string containing either a UTC-style or "generalized time"
34 * time string, convert to a CFDateRef. Returns nonzero on
37 int timeStringToCfDate(
43 bool isUtc
= false; // 2-digit year
44 bool isLocal
= false; // trailing timezone offset
45 bool isCssmTime
= false; // no trailing 'Z'
46 bool noSeconds
= false;
51 CFTimeZoneRef timeZone
;
52 CFTimeInterval gmtOff
= 0;
54 if((str
== NULL
) || (len
== 0) || (cfDate
== NULL
)) {
58 /* tolerate NULL terminated or not */
59 if(str
[len
- 1] == '\0') {
63 case UTC_TIME_NOSEC_LEN
: // 2-digit year, no seconds, not y2K compliant
67 case UTC_TIME_STRLEN
: // 2-digit year, not Y2K compliant
70 case CSSM_TIME_STRLEN
:
73 case GENERALIZED_TIME_STRLEN
: // 4-digit year
75 case LOCALIZED_UTC_TIME_STRLEN
: // "YYMMDDhhmmssThhmm" (where T=[+,-])
77 // deliberate fallthrough
78 case LOCALIZED_TIME_STRLEN
: // "YYYYMMDDhhmmssThhmm" (where T=[+,-])
81 default: // unknown format
87 /* check that all characters except last (or timezone indicator, if localized) are digits */
88 for(i
=0; i
<(len
- 1); i
++) {
89 if ( !(isdigit(cp
[i
])) )
90 if ( !isLocal
|| !(cp
[i
]=='+' || cp
[i
]=='-') )
94 /* check last character is a 'Z' or digit as appropriate */
95 if(isCssmTime
|| isLocal
) {
96 if(!isdigit(cp
[len
- 1])) {
101 if(cp
[len
- 1] != 'Z' ) {
110 /* two more digits */
122 * 0 <= year < 50 : assume century 21
123 * 50 <= year < 70 : illegal per PKIX
124 * ...though we allow this as of 10/10/02...dmitch
125 * 70 < year <= 99 : assume century 20
147 /* in the string, months are from 1 to 12 */
148 if((x
> 12) || (x
<= 0)) {
158 /* 1..31 in both formats */
159 if((x
> 31) || (x
<= 0)) {
169 if((x
> 23) || (x
< 0)) {
179 if((x
> 59) || (x
< 0)) {
193 if((x
> 59) || (x
< 0)) {
215 x
= atoi( szTemp
) * 60 * 60;
221 x
= atoi( szTemp
) * 60;
229 timeZone
= CFTimeZoneCreateWithTimeIntervalFromGMT(NULL
, gmtOff
);
233 *cfDate
= CFDateCreate(NULL
, CFGregorianDateGetAbsoluteTime(gd
, timeZone
));
239 * Compare two times. Assumes they're both in GMT. Returns:
248 switch(CFDateCompare(t1
, t2
, NULL
)) {
249 case kCFCompareLessThan
:
251 case kCFCompareEqualTo
:
253 case kCFCompareGreaterThan
:
262 * Create a time string, in either UTC (2-digit) or or Generalized (4-digit)
263 * year format. Caller mallocs the output string whose length is at least
264 * (UTC_TIME_STRLEN+1), (GENERALIZED_TIME_STRLEN+1), or (CSSM_TIME_STRLEN+1)
265 * respectively. Caller must hold tpTimeLock.
267 void timeAtNowPlus(unsigned secFromNow
,
274 baseTime
= time(NULL
);
275 baseTime
+= (time_t)secFromNow
;
276 utc
= *gmtime(&baseTime
);
280 /* UTC - 2 year digits - code which parses this assumes that
281 * (2-digit) years between 0 and 49 are in century 21 */
282 if(utc
.tm_year
>= 100) {
285 sprintf(outStr
, "%02d%02d%02d%02d%02d%02dZ",
286 utc
.tm_year
/* + 1900 */, utc
.tm_mon
+ 1,
287 utc
.tm_mday
, utc
.tm_hour
, utc
.tm_min
, utc
.tm_sec
);
290 sprintf(outStr
, "%04d%02d%02d%02d%02d%02dZ",
291 /* note year is relative to 1900, hopefully it'll have
292 * four valid digits! */
293 utc
.tm_year
+ 1900, utc
.tm_mon
+ 1,
294 utc
.tm_mday
, utc
.tm_hour
, utc
.tm_min
, utc
.tm_sec
);
297 sprintf(outStr
, "%04d%02d%02d%02d%02d%02d",
298 /* note year is relative to 1900, hopefully it'll have
299 * four valid digits! */
300 utc
.tm_year
+ 1900, utc
.tm_mon
+ 1,
301 utc
.tm_mday
, utc
.tm_hour
, utc
.tm_min
, utc
.tm_sec
);
307 * Convert a time string, which can be in any of three forms (UTC,
308 * generalized, or CSSM_TIMESTRING) into a CSSM_TIMESTRING. Caller
309 * mallocs the result, which must be at least (CSSM_TIME_STRLEN+1) bytes.
310 * Returns nonzero if incoming time string is badly formed.
312 int tpTimeToCssmTimestring(
313 const char *inStr
, // not necessarily NULL terminated
314 unsigned inStrLen
, // not including possible NULL
317 if((inStrLen
== 0) || (inStr
== NULL
)) {
322 case UTC_TIME_STRLEN
:
324 /* infer century and prepend to output */
333 * 0 <= year < 50 : assume century 21
334 * 50 <= year < 70 : illegal per PKIX
335 * 70 < year <= 99 : assume century 20
339 strcpy(outTime
, "20");
346 strcpy(outTime
, "19");
348 memmove(outTime
+ 2, inStr
, inStrLen
- 1); // don't copy the Z
351 case CSSM_TIME_STRLEN
:
352 memmove(outTime
, inStr
, inStrLen
); // trivial case
354 case GENERALIZED_TIME_STRLEN
:
355 memmove(outTime
, inStr
, inStrLen
- 1); // don't copy the Z
361 outTime
[CSSM_TIME_STRLEN
] = '\0';