1/*
2 * Copyright (c) 2000-2004,2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25//
26// Manage the Tower of Babel of CSSM dates and times.
27//
28#ifndef _H_CSSMDATES
29#define _H_CSSMDATES
30
31#include <security_utilities/utilities.h>
32#include <security_cdsa_utilities/cssmdata.h>
33#include <CoreFoundation/CFDate.h>
34
35
36namespace Security {
37
38
39//
40// A PodWrapper for CSSM_DATE
41//
42class CssmDate : public PodWrapper<CssmDate, CSSM_DATE> {
43public:
44    CssmDate() { }
45    CssmDate(const char *y, const char *m, const char *d);
46    CssmDate(int y, int m, int d);
47
48    const char *years() const	{ return reinterpret_cast<const char *>(Year); }
49    const char *months() const	{ return reinterpret_cast<const char *>(Month); }
50    const char *days() const	{ return reinterpret_cast<const char *>(Day); }
51    char *years()				{ return reinterpret_cast<char *>(Year); }
52    char *months()				{ return reinterpret_cast<char *>(Month); }
53    char *days()				{ return reinterpret_cast<char *>(Day); }
54
55    int year() const;
56    int month() const;
57    int day() const;
58
59private:
60    static void assign(char *dest, int width, const char *src);
61};
62
63inline bool operator == (const CSSM_DATE &d1, const CSSM_DATE &d2)
64{ return !memcmp(&d1, &d2, sizeof(d1)); }
65
66inline bool operator != (const CSSM_DATE &d1, const CSSM_DATE &d2)
67{ return !memcmp(&d1, &d2, sizeof(d1)); }
68
69
70//
71// Yet another CSSM date/time format is CSSM_TIMESTRING. This is
72// defined as "char *", just so you can't use the type system
73// to keep things sane, so we can't really PodWrap it the usual way.
74// What *were* they thinking?
75// The format is allegedly "yyyymmddhhmmss", and the standard says
76// nothing about trailing null characters.
77//
78
79
80//
81// A unified date-and-time object.
82// This is based on CFDate objects and converts to various CSSM
83// inspired formats.
84//
85class CssmUniformDate {
86public:
87    CssmUniformDate()	{ }
88
89    // convert to/from CFDateRef
90    CssmUniformDate(CFDateRef ref);
91    operator CFDateRef() const;
92
93	// convert to/from CFAbsoluteTime
94	CssmUniformDate(CFAbsoluteTime ct) : mTime(ct) { }
95	operator CFAbsoluteTime() const { return mTime; }
96
97    // convert to/from CSSM_DATE
98    CssmUniformDate(const CssmDate &src);
99    operator CssmDate () const;
100
101    // convert to/from DATA format (1999-06-30_15:05:39 form)
102    CssmUniformDate(const CSSM_DATA &src);
103    void convertTo(CssmOwnedData &data) const;
104
105    // convert to/from CSSM_TIMESTRING format (19990630150539)
106    CssmUniformDate(const char *src);
107    void convertTo(char *dest, size_t length) const;
108
109    // native comparisons
110    bool operator < (const CssmUniformDate &other) const	{ return mTime < other.mTime; }
111    bool operator == (const CssmUniformDate &other) const	{ return mTime == other.mTime; }
112    bool operator > (const CssmUniformDate &other) const	{ return mTime > other.mTime; }
113    bool operator <= (const CssmUniformDate &other) const	{ return mTime <= other.mTime; }
114    bool operator >= (const CssmUniformDate &other) const	{ return mTime >= other.mTime; }
115    bool operator != (const CssmUniformDate &other) const	{ return mTime != other.mTime; }
116
117private:
118    void setFromString(const char *src, const char *format, size_t fieldLength);
119
120private:
121    CFAbsoluteTime mTime;
122};
123
124
125} // end namespace Security
126
127#endif //_H_CSSMDATES
128