]>
git.saurik.com Git - apple/security.git/blob - AppleX509TP/tpTime.c
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.
31 #define UTC_TIME_STRLEN 13
32 #define GENERALIZED_TIME_STRLEN 15
35 * Given a string containing either a UTC-style or "generalized time"
36 * time string, convert to a struct tm (in GMT/UTC). Returns nonzero on
50 if((str
== NULL
) || (len
== 0) || (tmp
== NULL
)) {
54 /* tolerate NULL terminated or not */
55 if(str
[len
- 1] == '\0') {
59 case UTC_TIME_STRLEN
: // 2-digit year, not Y2K compliant
62 case GENERALIZED_TIME_STRLEN
: // 4-digit year
65 default: // unknown format
71 /* check that all characters except last are digits */
72 for(i
=0; i
<(len
- 1); i
++) {
73 if ( !(isdigit(cp
[i
])) ) {
78 /* check last character is a 'Z' */
79 if(cp
[len
- 1] != 'Z' ) {
99 * 0 <= year < 50 : assume century 21
100 * 50 <= year < 70 : illegal per PKIX
101 * 70 < year <= 99 : assume century 20
114 /* by definition - tm_year is year - 1900 */
115 tmp
->tm_year
= x
- 1900;
122 /* in the string, months are from 1 to 12 */
123 if((x
> 12) || (x
<= 0)) {
126 /* in a tm, 0 to 11 */
134 /* 1..31 in both formats */
135 if((x
> 31) || (x
<= 0)) {
145 if((x
> 23) || (x
< 0)) {
155 if((x
> 59) || (x
< 0)) {
165 if((x
> 59) || (x
< 0)) {
172 /* return current GMT time as a struct tm */
176 time_t nowTime
= time(NULL
);
177 *now
= *gmtime(&nowTime
);
181 * Compare two times. Assumes they're both in GMT. Returns:
190 if(t1
->tm_year
> t2
->tm_year
) {
193 else if(t1
->tm_year
< t2
->tm_year
) {
197 else if(t1
->tm_mon
> t2
->tm_mon
) {
200 else if(t1
->tm_mon
< t2
->tm_mon
) {
204 else if(t1
->tm_mday
> t2
->tm_mday
) {
207 else if(t1
->tm_mday
< t2
->tm_mday
) {
210 /* day of month equal */
211 else if(t1
->tm_hour
> t2
->tm_hour
) {
214 else if(t1
->tm_hour
< t2
->tm_hour
) {
218 else if(t1
->tm_min
> t2
->tm_min
) {
221 else if(t1
->tm_min
< t2
->tm_min
) {
225 else if(t1
->tm_sec
> t2
->tm_sec
) {
228 else if(t1
->tm_sec
< t2
->tm_sec
) {