Wolframe, 0.0.3

datetime.hpp
Go to the documentation of this file.
1 /************************************************************************
2 Copyright (C) 2011 - 2014 Project Wolframe.
3 All rights reserved.
4 
5 This file is part of Project Wolframe.
6 
7 Commercial Usage
8 Licensees holding valid Project Wolframe Commercial licenses may
9 use this file in accordance with the Project Wolframe
10 Commercial License Agreement provided with the Software or,
11 alternatively, in accordance with the terms contained
12 in a written agreement between the licensee and Project Wolframe.
13 
14 GNU General Public License Usage
15 Alternatively, you can redistribute this file and/or modify it
16 under the terms of the GNU General Public License as published by
17 the Free Software Foundation, either version 3 of the License, or
18 (at your option) any later version.
19 
20 Wolframe is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24 
25 You should have received a copy of the GNU General Public License
26 along with Wolframe. If not, see <http://www.gnu.org/licenses/>.
27 
28 If you have questions regarding the use of this file, please contact
29 Project Wolframe.
30 
31 ************************************************************************/
34 
35 #ifndef _Wolframe_TYPES_DATETIME_HPP_INCLUDED
36 #define _Wolframe_TYPES_DATETIME_HPP_INCLUDED
37 #include <string>
38 #include <cstring>
39 #include <cstdlib>
40 #include <stdexcept>
41 #include <boost/cstdint.hpp>
42 
43 namespace _Wolframe {
44 namespace types {
45 
47 class Variant;
48 
50 typedef boost::uint64_t Timestamp;
51 
55 class DateTime
56 {
57 public:
60  enum SubType
61  {
66  };
68  DateTime( const DateTime& o)
70  {}
71 
81  DateTime( unsigned short YY,
82  unsigned short MM,
83  unsigned short DD,
84  unsigned short hh,
85  unsigned short mm,
86  unsigned short ss,
87  unsigned short ll,
88  unsigned short cc)
89  :m_year(YY),m_month((unsigned char)MM),m_day((unsigned char)DD),m_hour((unsigned char)hh),m_minute((unsigned char)mm),m_second((unsigned char)ss),m_millisecond(ll),m_microsecond(cc),m_subtype(YYYYMMDDhhmmss_lllccc)
90  {
91  check( YY, MM, DD, hh, mm, ss, ll, cc);
92  }
93 
103  DateTime( unsigned short YY,
104  unsigned short MM,
105  unsigned short DD,
106  unsigned short hh,
107  unsigned short mm,
108  unsigned short ss,
109  unsigned short ll)
110  :m_year(YY),m_month((unsigned char)MM),m_day((unsigned char)DD),m_hour((unsigned char)hh),m_minute((unsigned char)mm),m_second((unsigned char)ss),m_millisecond(ll),m_microsecond(0),m_subtype(YYYYMMDDhhmmss_lll)
111  {
112  check( YY, MM, DD, hh, mm, ss, ll, 0);
113  }
114 
122  DateTime( unsigned short YY,
123  unsigned short MM,
124  unsigned short DD,
125  unsigned short hh,
126  unsigned short mm,
127  unsigned short ss)
128  :m_year(YY),m_month((unsigned char)MM),m_day((unsigned char)DD),m_hour((unsigned char)hh),m_minute((unsigned char)mm),m_second((unsigned char)ss),m_millisecond(0),m_microsecond(0),m_subtype(YYYYMMDDhhmmss)
129  {
130  check( YY, MM, DD, hh, mm, ss, 0, 0);
131  }
132 
137  DateTime( unsigned short YY,
138  unsigned short MM,
139  unsigned short DD)
140  :m_year(YY),m_month((unsigned char)MM),m_day((unsigned char)DD),m_hour(0),m_minute(0),m_second(0),m_millisecond(0),m_microsecond(0),m_subtype(YYYYMMDD)
141  {
142  check( YY, MM, DD, 0, 0, 0, 0, 0);
143  }
144 
147  unsigned int year() const {return m_year;}
150  unsigned int month() const {return m_month;}
153  unsigned int day() const {return m_day;}
156  unsigned int hour() const {return m_hour;}
159  unsigned int minute() const {return m_minute;}
162  unsigned int second() const {return m_second;}
165  unsigned int millisecond() const {return m_millisecond;}
168  unsigned int microsecond() const {return m_microsecond;}
171  unsigned int usecond() const {return m_second * 1000 + m_microsecond;}
174  SubType subtype() const {return m_subtype;}
175 
178  DateTime( const Timestamp& t)
179  {
180  init( t);
181  }
184  DateTime( const std::string& s)
185  {
186  init( s);
187  }
188 
192  DateTime( const char* str, std::size_t strsize)
193  {
194  init( str, strsize);
195  }
196 
199  DateTime( const Variant& v);
200 
203  Timestamp timestamp() const;
204 
207  void init( Timestamp timestamp_);
208 
212  void init( const std::string& str);
213 
215  // The date time value gets a subtype depending on what is specified in the string (e.g. if the granularity in the parsed string is seconds then the subtype gets YYYYMMDDhhmmss)
217  // a) ISO date time 20011126T234312,123134
218  // b) 2001-11-26 23:43:12,123134
219  // c) 20011126234312123134
223  void init( const char* str, std::size_t strsize);
224 
228  {
229  enum Id
230  {
234  };
235  };
236 
239  double toMSDNtimestamp() const;
240 
243  static DateTime fromMSDNtimestamp( double tm);
244 
249 
252  bool operator==( const DateTime& o) const {return compare(o)==0;}
255  bool operator!=( const DateTime& o) const {return compare(o)!=0;}
258  bool operator>=( const DateTime& o) const {return compare(o)>=0;}
261  bool operator> ( const DateTime& o) const {return compare(o)> 0;}
264  bool operator<=( const DateTime& o) const {return compare(o)<=0;}
267  bool operator< ( const DateTime& o) const {return compare(o)< 0;}
268 
271  int compare( const DateTime& o) const;
272 
273 private:
276  static int compareValue( unsigned int aa, unsigned int bb)
277  {
278  if (aa == bb) return 0;
279  if (aa < bb) return -1;
280  return +1;
281  }
287  static void setBits( Timestamp& dest, unsigned int val, unsigned int bitidx, unsigned int bitrange)
288  {
289  dest |= ((Timestamp) val << (63-bitidx-bitrange));
290  }
295  static unsigned int getBits( const Timestamp& from, unsigned int bitidx, unsigned int bitrange)
296  {
297  return (unsigned int)(from >> (63-bitidx-bitrange)) & ((1<<bitrange)-1);
298  }
303  static unsigned int getSubstringAsInt( const char* from, unsigned int idx, unsigned int range)
304  {
305  return std::atoi( std::string( from + idx, range).c_str());
306  }
312  static void check_range( unsigned int val, unsigned int from, unsigned int to)
313  {
314  if (val < from || val > to)
315  {
316  throw std::runtime_error("value out of range");
317  }
318  }
323  static void check_range( unsigned int val, unsigned int maximum)
324  {
325  if (val > maximum)
326  {
327  throw std::runtime_error("value out of range");
328  }
329  }
339  static void check( unsigned short YY, unsigned short MM, unsigned short DD, unsigned short hh, unsigned short mm, unsigned short ss, unsigned short ll, unsigned short cc);
340 
345  static void appendValue( std::string& dest, unsigned int value, unsigned int nofDigits);
346 
347 private:
348  unsigned short m_year;
349  unsigned char m_month;
350  unsigned char m_day;
351  unsigned char m_hour;
352  unsigned char m_minute;
353  unsigned char m_second;
354  unsigned short m_millisecond;
355  unsigned short m_microsecond;
357 };
358 
359 }}//namespace
360 #endif
361 
362 
boost::uint64_t Timestamp
Timestamp equivalent to a date time value for variant type.
Definition: datetime.hpp:47
date with time down to milliseconds (1/1'000 seconds)
Definition: datetime.hpp:64
unsigned int hour() const
Get the hour of the datetime.
Definition: datetime.hpp:156
unsigned int millisecond() const
Get the milliseconds part of the datetime.
Definition: datetime.hpp:165
static unsigned int getBits(const Timestamp &from, unsigned int bitidx, unsigned int bitrange)
Get the bits [bitidx..bitidx+bitrange-1] of a value.
Definition: datetime.hpp:295
Timestamp timestamp() const
Get the datetime timestamp value of the datetime.
unsigned int day() const
Get the day of the datetime in the month starting with 1.
Definition: datetime.hpp:153
std::string tostring(StringFormat::Id sf=StringFormat::YYYYMMDDhhmmssxxxxxx) const
Get the string value of the datetime.
bool operator>(const DateTime &o) const
Test if this datetime is bigger than argument.
Definition: datetime.hpp:261
DateTime(unsigned short YY, unsigned short MM, unsigned short DD, unsigned short hh, unsigned short mm, unsigned short ss)
Constructor of datetime format YYYYMMDDhhmmss.
Definition: datetime.hpp:122
double toMSDNtimestamp() const
Get the MSDN DATE type from the date.
static void setBits(Timestamp &dest, unsigned int val, unsigned int bitidx, unsigned int bitrange)
Initialize the bits [bitidx..bitidx+bitrange-1] of a value.
Definition: datetime.hpp:287
unsigned int year() const
Get the year AD of the datetime.
Definition: datetime.hpp:147
static unsigned int getSubstringAsInt(const char *from, unsigned int idx, unsigned int range)
Get the characters [idx..idx+bitrange-1] of a value as unsigned int.
Definition: datetime.hpp:303
SubType
Describing the format of the DateTime value.
Definition: datetime.hpp:60
date
Definition: datetime.hpp:62
unsigned short m_microsecond
[49..59] micorseconds part
Definition: datetime.hpp:355
DateTime(const Timestamp &t)
Constructor.
Definition: datetime.hpp:178
unsigned int month() const
Get the month of the datetime in the year starting with 1.
Definition: datetime.hpp:150
DateTime(unsigned short YY, unsigned short MM, unsigned short DD, unsigned short hh, unsigned short mm, unsigned short ss, unsigned short ll, unsigned short cc)
Constructor of datetime format YYYYMMDDhhmmss_lllccc.
Definition: datetime.hpp:81
bool operator<(const DateTime &o) const
Test if this datetime is smaller than argument.
Definition: datetime.hpp:267
Data type for normalized date time (absolute time without time zone info)
Definition: datetime.hpp:55
static int compareValue(unsigned int aa, unsigned int bb)
Compare two integer values.
Definition: datetime.hpp:276
SubType m_subtype
[59..60] subtype (format) of the datetime
Definition: datetime.hpp:356
bool operator!=(const DateTime &o) const
Test argument datetime for inequality.
Definition: datetime.hpp:255
Forward declaration.
Definition: variant.hpp:65
unsigned int microsecond() const
Get the microseconds part of the datetime.
Definition: datetime.hpp:168
static DateTime fromMSDNtimestamp(double tm)
Get the date and time from an MSDN DATE type as YYYYMMDDhhmmss.
static void appendValue(std::string &dest, unsigned int value, unsigned int nofDigits)
Append value formatted (filling zero's)
void init(Timestamp timestamp_)
Initialize the datetime with its timestamp value.
Id
Definition: datetime.hpp:229
DateTime(const DateTime &o)
Copy constructor.
Definition: datetime.hpp:68
DateTime(unsigned short YY, unsigned short MM, unsigned short DD)
Constructor of datetime format YYYYMMDD.
Definition: datetime.hpp:137
Format for parsing/printing date time.
Definition: datetime.hpp:227
DateTime(const char *str, std::size_t strsize)
Constructor.
Definition: datetime.hpp:192
unsigned char m_month
[13..16] 2 digits month in year starting with 1
Definition: datetime.hpp:349
DateTime(const std::string &s)
Constructor.
Definition: datetime.hpp:184
static void check_range(unsigned int val, unsigned int maximum)
Check the value to be within the range of [0,to].
Definition: datetime.hpp:323
bool operator>=(const DateTime &o) const
Test if this datetime is bigger or equal than argument.
Definition: datetime.hpp:258
unsigned short m_millisecond
[39..49] milliseconds part
Definition: datetime.hpp:354
unsigned char m_minute
[27..32] minutes part
Definition: datetime.hpp:352
DateTime(unsigned short YY, unsigned short MM, unsigned short DD, unsigned short hh, unsigned short mm, unsigned short ss, unsigned short ll)
Constructor of datetime format YYYYMMDDhhmmss_lll.
Definition: datetime.hpp:103
date with time down to microseconds (1/1'000'000 seconds)
Definition: datetime.hpp:65
unsigned char m_day
[17..21] 2 digits day in month starting with 1
Definition: datetime.hpp:350
date with time down to seconds
Definition: datetime.hpp:63
bool operator<=(const DateTime &o) const
Test if this datetime is smaller or equal than argument.
Definition: datetime.hpp:264
unsigned int usecond() const
Get the format (subtype) of the datetime.
Definition: datetime.hpp:171
unsigned char m_hour
[22..26] hour of day [0..23]
Definition: datetime.hpp:351
unsigned char m_second
[33..39] seconds part
Definition: datetime.hpp:353
bool operator==(const DateTime &o) const
Test argument datetime for equality.
Definition: datetime.hpp:252
static void check_range(unsigned int val, unsigned int from, unsigned int to)
Check the value to be within the range of [from,to].
Definition: datetime.hpp:312
unsigned int second() const
Get the seconds part of the datetime.
Definition: datetime.hpp:162
unsigned int minute() const
Get the minutes part of the datetime.
Definition: datetime.hpp:159
static void check(unsigned short YY, unsigned short MM, unsigned short DD, unsigned short hh, unsigned short mm, unsigned short ss, unsigned short ll, unsigned short cc)
Verify the date element without calendar validity checks.
int compare(const DateTime &o) const
Compare with argument datetime.
SubType subtype() const
Get the format (subtype) of the datetime.
Definition: datetime.hpp:174
unsigned short m_year
[ 0..12] 4 digits year AD
Definition: datetime.hpp:348