]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/cssmdates.h
Security-54.1.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / cssmdates.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 //
20 // Manage the Tower of Babel of CSSM dates and times.
21 //
22 #ifndef _H_CSSMDATES
23 #define _H_CSSMDATES
24
25 #include <Security/utilities.h>
26 #include <Security/cssmdata.h>
27 #include <CoreFoundation/CFDate.h>
28
29
30 namespace Security {
31
32
33 //
34 // A PodWrapper for CSSM_DATE
35 //
36 class CssmDate : public PodWrapper<CssmDate, CSSM_DATE> {
37 public:
38 CssmDate() { }
39 CssmDate(const char *y, const char *m, const char *d);
40 CssmDate(int y, int m, int d);
41
42 const char *years() const { return reinterpret_cast<const char *>(Year); }
43 const char *months() const { return reinterpret_cast<const char *>(Month); }
44 const char *days() const { return reinterpret_cast<const char *>(Day); }
45 char *years() { return reinterpret_cast<char *>(Year); }
46 char *months() { return reinterpret_cast<char *>(Month); }
47 char *days() { return reinterpret_cast<char *>(Day); }
48
49 int year() const;
50 int month() const;
51 int day() const;
52
53 private:
54 static void assign(char *dest, int width, const char *src);
55 };
56
57 inline bool operator == (const CSSM_DATE &d1, const CSSM_DATE &d2)
58 { return !memcmp(&d1, &d2, sizeof(d1)); }
59
60 inline bool operator != (const CSSM_DATE &d1, const CSSM_DATE &d2)
61 { return !memcmp(&d1, &d2, sizeof(d1)); }
62
63
64 //
65 // Yet another CSSM date/time format is CSSM_TIMESTRING. This is
66 // defined as "char *", just so you can't use the type system
67 // to keep things sane, so we can't really PodWrap it the usual way.
68 // What *were* they thinking?
69 // The format is allegedly "yyyymmddhhmmss", and the standard says
70 // nothing about trailing null characters.
71 //
72
73
74 //
75 // A unified date-and-time object.
76 // This is based on CFDate objects and converts to various CSSM
77 // inspired formats.
78 //
79 class CssmUniformDate {
80 public:
81 CssmUniformDate() { }
82
83 // convert to/from CFDateRef
84 CssmUniformDate(CFDateRef ref);
85 operator CFDateRef() const;
86
87 // convert to/from CSSM_DATE
88 CssmUniformDate(const CssmDate &src);
89 CssmUniformDate(const CSSM_DATE &src);
90 operator CssmDate () const;
91
92 // convert to/from DATA format (1999-06-30_15:05:39 form)
93 CssmUniformDate(const CSSM_DATA &src);
94 void convertTo(CssmOwnedData &data) const;
95
96 // convert to/from CSSM_TIMESTRING format (19990630150539)
97 CssmUniformDate(const char *src);
98 void convertTo(char *dest, size_t length) const;
99
100 // native comparisons
101 bool operator < (const CssmUniformDate &other) const { return mTime < other.mTime; }
102 bool operator == (const CssmUniformDate &other) const { return mTime == other.mTime; }
103 bool operator > (const CssmUniformDate &other) const { return mTime > other.mTime; }
104 bool operator <= (const CssmUniformDate &other) const { return mTime <= other.mTime; }
105 bool operator >= (const CssmUniformDate &other) const { return mTime >= other.mTime; }
106 bool operator != (const CssmUniformDate &other) const { return mTime != other.mTime; }
107
108 private:
109 void setFromString(const char *src, const char *format, size_t fieldLength);
110
111 private:
112 CFAbsoluteTime mTime;
113 };
114
115
116 } // end namespace Security
117
118 #endif //_H_CSSMDATES