libcommonc++  0.7
DateTimeFormat.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_DateTimeFormat_hxx
24 #define __ccxx_DateTimeFormat_hxx
25 
26 #include <commonc++/Common.h++>
27 #include <commonc++/DateTime.h++>
29 
30 #include <cstring>
31 
32 namespace ccxx {
33 
34 class CStringBuilder; // fwd decl
35 
98 {
99  public:
100 
108  DateTimeFormat(String format = "%t %T", String dateFormat = "%t",
109  String timeFormat = "%T");
110 
112  ~DateTimeFormat();
113 
115  inline void setFormat(const String& format)
116  { _parseFormat(format, _dateTimeFormat); }
117 
119  inline void setDateFormat(const String& dateFormat)
120  { _parseFormat(dateFormat, _dateFormat); }
121 
123  inline void setTimeFormat(const String& timeFormat)
124  { _parseFormat(timeFormat, _timeFormat); }
125 
132  String format(const Date& value) const;
133 
142  size_t format(const Date& value, char* buf, size_t bufsz) const;
143 
150  String format(const Time& value) const;
151 
160  size_t format(const Time& value, char* buf, size_t bufsz) const;
161 
168  String format(const DateTime& value) const;
169 
178  size_t format(const DateTime& value, char* buf, size_t bufsz) const;
179 
187  void parse(Date& value, const String& text) const;
188 
196  void parse(Time& value, const String& text) const;
197 
205  void parse(DateTime& value, const String& text) const;
206 
207  private:
208 
209  class TokenList; // fwd decl
210 
211  static String _monthNames[12];
212  static String _monthNamesLong[12];
213  static String _weekdayNames[7];
214  static String _weekdayNamesLong[7];
215  static String _meridiemUpper[2];
216  static String _meridiemLower[2];
217  static bool _cacheStrings();
218  static bool _stringsCached;
219 
220  enum TokenCode { TOK_INVALID = -1, TOK_LITERAL = 0, TOK_HOURS,
221  TOK_HOURS12, TOK_MINUTES, TOK_SECONDS, TOK_MSEC,
222  TOK_AMPM_LOWER, TOK_AMPM_UPPER, TOK_MONTH, TOK_DAY,
223  TOK_DAYNUM, TOK_YEAR, TOK_YEAR_4, TOK_WEEKDAY,
224  TOK_WEEKNUM, TOK_TZ, TOK_DATE, TOK_TIME };
225 
226  enum TokenFormat { FMT_INVALID = -1, FMT_NUMERIC, FMT_NUMERIC_0,
227  FMT_NUMERIC_SPC, FMT_STRING, FMT_STRING_LONG };
228 
229  class Token
230  {
231  private:
232 
233  TokenCode _token;
234  TokenFormat _format;
235  char* _text;
236 
237  public:
238 
239  Token(TokenCode token, TokenFormat format, const char* text = NULL,
240  int len = 0)
241  : _token(token), _format(format)
242  {
243  if(text != NULL)
244  {
245  _text = new char[len + 1];
246  std::memcpy(_text, text, len);
247  _text[len] = NUL;
248  }
249  else
250  _text = NULL;
251  }
252 
253  ~Token()
254  {
255  delete[] _text;
256  }
257 
258  inline TokenCode getType() const
259  { return(_token); }
260 
261  inline TokenFormat getFormat() const
262  { return(_format); }
263 
264  inline const char *getText() const
265  { return(_text); }
266 
267  };
268 
269  void _formatNumeric(String& s, uint_t val, uint_t width,
270  TokenFormat format) const;
271  void _parseFormat(const String& format, TokenList* tokens);
272  String _format(const TokenList* tokens, const DateTime& value) const;
273  size_t _format(const TokenList* tokens, const DateTime& value,
274  CStringBuilder& builder) const;
275  void _parse(const TokenList* tokens, DateTime& value, const char* buf) const;
276 
277  int _parseInt(const char*& p, uint_t& pos, int min, int max) const;
278  int _parseString(const char*& p, uint_t& pos, const String* list,
279  size_t count) const;
280  void _skip(const char*& p, uint_t& pos, uint_t count) const;
281 
282  TokenList* _dateFormat;
283  TokenList* _timeFormat;
284  TokenList* _dateTimeFormat;
285 };
286 
287 } // namespace ccxx
288 
289 #endif // __ccxx_DateTimeFormat_hxx
A formatter for dates and times.
Definition: DateTimeFormat.h++:97
void setTimeFormat(const String &timeFormat)
Set the format for times.
Definition: DateTimeFormat.h++:123
A representation of a calendar date.
Definition: Date.h++:35
A utility class for constructing C-style strings.
Definition: CStringBuilder.h++:42
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
void setFormat(const String &format)
Set the format for dates with times.
Definition: DateTimeFormat.h++:115
A representation of a clock time.
Definition: Time.h++:37
void setDateFormat(const String &dateFormat)
Set the format for dates.
Definition: DateTimeFormat.h++:119
#define COMMONCPP_API
Definition: Common.h++:126
A flexible, reference counted, copy-on-write, thread-safe, nullable string.
Definition: String.h++:50
A representation of a calendar date and clock time.
Definition: DateTime.h++:38
#define NUL
The NUL character (ASCII value 0).
Definition: Common.h++:240
Definition: AllocationMap.c++:25