Bug 460926 A11y hierachy is broken on Ubuntu 8.10 (GNOME 2.24), r=Evan.Yan sr=roc
[wine-gecko.git] / security / nss / lib / util / sectime.c
blobcf4c526fbe62f2256414679a701accde11ff0f46
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #include "prlong.h"
38 #include "prtime.h"
39 #include "secder.h"
40 #include "secitem.h"
41 #include "secerr.h"
43 static const PRTime January1st2050 = LL_INIT(0x0008f81e, 0x1b098000);
45 static char *DecodeUTCTime2FormattedAscii (SECItem *utcTimeDER, char *format);
46 static char *DecodeGeneralizedTime2FormattedAscii (SECItem *generalizedTimeDER, char *format);
48 /* convert DER utc time to ascii time string */
49 char *
50 DER_UTCTimeToAscii(SECItem *utcTime)
52 return (DecodeUTCTime2FormattedAscii (utcTime, "%a %b %d %H:%M:%S %Y"));
55 /* convert DER utc time to ascii time string, only include day, not time */
56 char *
57 DER_UTCDayToAscii(SECItem *utctime)
59 return (DecodeUTCTime2FormattedAscii (utctime, "%a %b %d, %Y"));
62 /* convert DER generalized time to ascii time string, only include day,
63 not time */
64 char *
65 DER_GeneralizedDayToAscii(SECItem *gentime)
67 return (DecodeGeneralizedTime2FormattedAscii (gentime, "%a %b %d, %Y"));
70 /* convert DER generalized or UTC time to ascii time string, only include
71 day, not time */
72 char *
73 DER_TimeChoiceDayToAscii(SECItem *timechoice)
75 switch (timechoice->type) {
77 case siUTCTime:
78 return DER_UTCDayToAscii(timechoice);
80 case siGeneralizedTime:
81 return DER_GeneralizedDayToAscii(timechoice);
83 default:
84 PORT_Assert(0);
85 PORT_SetError(SEC_ERROR_INVALID_ARGS);
86 return NULL;
90 char *
91 CERT_UTCTime2FormattedAscii (int64 utcTime, char *format)
93 PRExplodedTime printableTime;
94 char *timeString;
96 /* Converse time to local time and decompose it into components */
97 PR_ExplodeTime(utcTime, PR_LocalTimeParameters, &printableTime);
99 timeString = (char *)PORT_Alloc(256);
101 if ( timeString ) {
102 if ( ! PR_FormatTime( timeString, 256, format, &printableTime )) {
103 PORT_Free(timeString);
104 timeString = NULL;
108 return (timeString);
111 char *CERT_GenTime2FormattedAscii (int64 genTime, char *format)
113 PRExplodedTime printableTime;
114 char *timeString;
116 /* Decompose time into components */
117 PR_ExplodeTime(genTime, PR_GMTParameters, &printableTime);
119 timeString = (char *)PORT_Alloc(256);
121 if ( timeString ) {
122 if ( ! PR_FormatTime( timeString, 256, format, &printableTime )) {
123 PORT_Free(timeString);
124 timeString = NULL;
125 PORT_SetError(SEC_ERROR_OUTPUT_LEN);
129 return (timeString);
133 /* convert DER utc time to ascii time string, The format of the time string
134 depends on the input "format"
136 static char *
137 DecodeUTCTime2FormattedAscii (SECItem *utcTimeDER, char *format)
139 int64 utcTime;
140 int rv;
142 rv = DER_UTCTimeToTime(&utcTime, utcTimeDER);
143 if (rv) {
144 return(NULL);
146 return (CERT_UTCTime2FormattedAscii (utcTime, format));
149 /* convert DER utc time to ascii time string, The format of the time string
150 depends on the input "format"
152 static char *
153 DecodeGeneralizedTime2FormattedAscii (SECItem *generalizedTimeDER, char *format)
155 PRTime generalizedTime;
156 int rv;
158 rv = DER_GeneralizedTimeToTime(&generalizedTime, generalizedTimeDER);
159 if (rv) {
160 return(NULL);
162 return (CERT_GeneralizedTime2FormattedAscii (generalizedTime, format));
165 /* decode a SECItem containing either a SEC_ASN1_GENERALIZED_TIME
166 or a SEC_ASN1_UTC_TIME */
168 SECStatus DER_DecodeTimeChoice(PRTime* output, const SECItem* input)
170 switch (input->type) {
171 case siGeneralizedTime:
172 return DER_GeneralizedTimeToTime(output, input);
174 case siUTCTime:
175 return DER_UTCTimeToTime(output, input);
177 default:
178 PORT_SetError(SEC_ERROR_INVALID_ARGS);
179 PORT_Assert(0);
180 return SECFailure;
184 /* encode a PRTime to an ASN.1 DER SECItem containing either a
185 SEC_ASN1_GENERALIZED_TIME or a SEC_ASN1_UTC_TIME */
187 SECStatus DER_EncodeTimeChoice(PRArenaPool* arena, SECItem* output, PRTime input)
189 if (LL_CMP(input, >, January1st2050)) {
190 return DER_TimeToGeneralizedTimeArena(arena, output, input);
191 } else {
192 return DER_TimeToUTCTimeArena(arena, output, input);