00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __ccxx_DateTimeFormat_hxx
00024 #define __ccxx_DateTimeFormat_hxx
00025
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/DateTime.h++>
00028 #include <commonc++/ParseException.h++>
00029
00030 namespace ccxx {
00031
00032 class CStringBuilder;
00033
00095 class COMMONCPP_API DateTimeFormat
00096 {
00097 public:
00098
00105 DateTimeFormat(String format = "%t %T", String dateFormat = "%t",
00106 String timeFormat = "%T");
00107
00109 ~DateTimeFormat() throw();
00110
00112 inline void setFormat(const String &format)
00113 { _parseFormat(format, _dateTimeFormat); }
00114
00116 inline void setDateFormat(const String &dateFormat)
00117 { _parseFormat(dateFormat, _dateFormat); }
00118
00120 inline void setTimeFormat(const String &timeFormat)
00121 { _parseFormat(timeFormat, _timeFormat); }
00122
00128 String format(const Date& value) const;
00129
00137 size_t format(const Date& value, char *buf, size_t bufsz) const;
00138
00144 String format(const Time& value) const;
00145
00153 size_t format(const Time& value, char *buf, size_t bufsz) const;
00154
00160 String format(const DateTime& value) const;
00161
00169 size_t format(const DateTime& value, char *buf, size_t bufsz) const;
00170
00177 void parse(Date &value, const String &text) const throw(ParseException);
00178
00185 void parse(Time &value, const String &text) const throw(ParseException);
00186
00193 void parse(DateTime &value, const String &text) const throw(ParseException);
00194
00195 private:
00196
00197 class TokenList;
00198
00199 static String _monthNames[12];
00200 static String _monthNamesLong[12];
00201 static String _weekdayNames[7];
00202 static String _weekdayNamesLong[7];
00203 static String _meridiemUpper[2];
00204 static String _meridiemLower[2];
00205 static bool _cacheStrings();
00206 static bool _stringsCached;
00207
00208 enum TokenCode { TOK_INVALID = -1, TOK_LITERAL = 0, TOK_HOURS,
00209 TOK_HOURS12, TOK_MINUTES, TOK_SECONDS, TOK_MSEC,
00210 TOK_AMPM_LOWER, TOK_AMPM_UPPER, TOK_MONTH, TOK_DAY,
00211 TOK_DAYNUM, TOK_YEAR, TOK_YEAR_4, TOK_WEEKDAY,
00212 TOK_WEEKNUM, TOK_TZ, TOK_DATE, TOK_TIME };
00213
00214 enum TokenFormat { FMT_INVALID = -1, FMT_NUMERIC, FMT_NUMERIC_0,
00215 FMT_NUMERIC_SPC, FMT_STRING,
00216 FMT_STRING_LONG };
00217
00218 class Token
00219 {
00220 private:
00221
00222 TokenCode _token;
00223 TokenFormat _format;
00224 char *_text;
00225
00226 public:
00227
00228 Token(TokenCode token, TokenFormat format, const char *text = NULL,
00229 int len = 0)
00230 : _token(token), _format(format)
00231 {
00232 if(text != NULL)
00233 {
00234 _text = new char[len + 1];
00235 std::memcpy(_text, text, len);
00236 _text[len] = NUL;
00237 }
00238 else
00239 _text = NULL;
00240 }
00241
00242 ~Token()
00243 {
00244 delete[] _text;
00245 }
00246
00247 inline TokenCode getType() const
00248 { return(_token); }
00249
00250 inline TokenFormat getFormat() const
00251 { return(_format); }
00252
00253 inline const char *getText() const
00254 { return(_text); }
00255
00256 };
00257
00258 void _formatNumeric(String &s, uint_t val, uint_t width,
00259 TokenFormat format) const;
00260 void _parseFormat(const String &format, TokenList *tokens);
00261 String _format(const TokenList *tokens, const DateTime& value) const;
00262 size_t _format(const TokenList *tokens, const DateTime& value,
00263 CStringBuilder &builder) const;
00264 void _parse(const TokenList *tokens, DateTime& value, const char *buf)
00265 const throw(ParseException);
00266
00267 int _parseInt(const char *&p, uint_t &pos, int min, int max) const
00268 throw(ParseException);
00269 int _parseString(const char *&p, uint_t &pos, const String *list,
00270 size_t count) const throw(ParseException);
00271 void _skip(const char *&p, uint_t &pos, uint_t count) const
00272 throw(ParseException);
00273
00274 TokenList *_dateFormat;
00275 TokenList *_timeFormat;
00276 TokenList *_dateTimeFormat;
00277 };
00278
00279 };
00280
00281 #endif // __ccxx_DateTimeFormat_hxx
00282
00283
00284
00285