Qore Programming Language  0.8.11.1
DateTimeNode.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  DateTimeNode.h
4 
5  DateTimeNode Class Definition
6 
7  Qore Programming Language
8 
9  Copyright (C) 2003 - 2014 David Nichols
10 
11  Permission is hereby granted, free of charge, to any person obtaining a
12  copy of this software and associated documentation files (the "Software"),
13  to deal in the Software without restriction, including without limitation
14  the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  and/or sell copies of the Software, and to permit persons to whom the
16  Software is furnished to do so, subject to the following conditions:
17 
18  The above copyright notice and this permission notice shall be included in
19  all copies or substantial portions of the Software.
20 
21  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  DEALINGS IN THE SOFTWARE.
28 
29  Note that the Qore library is released under a choice of three open-source
30  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
31  information.
32 */
33 
34 #ifndef _QORE_DATETIMENODE_H
35 
36 #define _QORE_DATETIMENODE_H
37 
38 #include <qore/AbstractQoreNode.h>
39 #include <qore/DateTime.h>
40 
41 class qore_date_private;
42 
44 class DateTimeNode : public SimpleValueQoreNode, public DateTime {
45 private:
47  DLLLOCAL DateTimeNode(const DateTime *);
48 
50  DLLLOCAL DateTimeNode& operator=(const DateTimeNode &);
51 
53 
57  DLLEXPORT virtual bool getAsBoolImpl() const;
58 
60 
63  DLLEXPORT virtual int getAsIntImpl() const;
64 
66 
69  DLLEXPORT virtual int64 getAsBigIntImpl() const;
70 
72 
75  DLLEXPORT virtual double getAsFloatImpl() const;
76 
78  DLLLOCAL DateTimeNode(qore_date_private *n_priv);
79 
80 protected:
82  DLLEXPORT virtual ~DateTimeNode();
83 
84 public:
86 
89  DLLEXPORT DateTimeNode(bool r = false);
90 
92 
102  DLLEXPORT DateTimeNode(int n_year, int n_month, int n_day, int n_hour = 0, int n_minute = 0, int n_second = 0, short n_ms = 0, bool n_relative = false);
103 
105 
108  DLLEXPORT DateTimeNode(int64 seconds);
109 
111 
115  DLLEXPORT DateTimeNode(int64 seconds, int ms);
116 
118 
120  DLLEXPORT DateTimeNode(const char *date);
121 
123 
126  DLLEXPORT DateTimeNode(const AbstractQoreZoneInfo *zone, const char *date);
127 
129 
132  DLLEXPORT DateTimeNode(struct tm *tms);
133 
135  DLLEXPORT DateTimeNode(const DateTimeNode &dt);
136 
138  DLLEXPORT DateTimeNode(const DateTime &dt);
139 
141 
146  DLLEXPORT virtual QoreString *getStringRepresentation(bool &del) const;
147 
149 
152  DLLEXPORT virtual void getStringRepresentation(QoreString &str) const;
153 
155 
159  DLLEXPORT virtual DateTime *getDateTimeRepresentation(bool &del) const;
160 
162 
165  DLLEXPORT virtual void getDateTimeRepresentation(DateTime &dt) const;
166 
168 
177  DLLEXPORT virtual QoreString *getAsString(bool &del, int foff, ExceptionSink *xsink) const;
178 
180 
187  DLLEXPORT virtual int getAsString(QoreString &str, int foff, ExceptionSink *xsink) const;
188 
189  DLLEXPORT virtual class AbstractQoreNode *realCopy() const;
190 
192 
196  DLLEXPORT virtual bool is_equal_soft(const AbstractQoreNode *v, ExceptionSink *xsink) const;
197  DLLEXPORT virtual bool is_equal_hard(const AbstractQoreNode *v, ExceptionSink *xsink) const;
198 
200  DLLEXPORT virtual const char *getTypeName() const;
201 
203 
206  DLLEXPORT DateTimeNode *copy() const;
207 
209 
212  DLLEXPORT DateTimeNode *add(const DateTime *dt) const;
213 
215 
218  DLLEXPORT DateTimeNode *subtractBy(const DateTime *dt) const;
219 
221  DLLEXPORT DateTimeNode *unaryMinus() const;
222 
224  DLLLOCAL static const char *getStaticTypeName() {
225  return "date";
226  }
227 
229  DLLLOCAL static qore_type_t getStaticTypeCode() {
230  return NT_DATE;
231  }
232 
234 
241  DLLEXPORT static DateTimeNode *getDateFromISOWeek(int year, int week, int day, ExceptionSink *xsink);
242 
244  DLLEXPORT static DateTimeNode *makeAbsolute(const AbstractQoreZoneInfo *n_zone, int n_year, int n_month, int n_day, int n_hour = 0, int n_minute = 0, int n_second = 0, int n_us = 0);
245 
247 
252  DLLEXPORT static DateTimeNode *makeAbsolute(const AbstractQoreZoneInfo *zone, int64 seconds, int us = 0);
253 
255 
260  DLLEXPORT static DateTimeNode *makeAbsoluteLocal(const AbstractQoreZoneInfo *zone, int64 seconds, int us = 0);
261 
263  DLLEXPORT static DateTimeNode *makeRelative(int n_year, int n_month, int n_day, int n_hour = 0, int n_minute = 0, int n_second = 0, int n_us = 0);
264 };
265 
266 DLLEXPORT extern DateTimeNode *ZeroDate;
267 
269 
275 private:
276  const DateTime *dt;
277  bool del;
278 
279  DLLLOCAL DateTimeValueHelper(const DateTimeValueHelper&); // not implemented
280  DLLLOCAL DateTimeValueHelper& operator=(const DateTimeValueHelper&); // not implemented
281  DLLLOCAL void *operator new(size_t); // not implemented, make sure it is not new'ed
282 
283 public:
286  // optmization without virtual function call for most common case
287  if (n) {
288  if (n->getType() == NT_DATE) {
289  dt = reinterpret_cast<const DateTimeNode *>(n);
290  del = false;
291  }
292  else
293  dt = n->getDateTimeRepresentation(del);
294  }
295  else {
296  dt = ZeroDate;
297  del = false;
298  }
299  }
300 
302  DLLLOCAL ~DateTimeValueHelper() {
303  if (del)
304  delete const_cast<DateTime *>(dt);
305  }
306  DLLLOCAL const DateTime *operator->() { return dt; }
307  DLLLOCAL const DateTime *operator*() { return dt; }
308 };
309 
311 
315 private:
316  DateTimeNode *dt;
317  bool temp;
318 
319  DLLLOCAL DateTimeNodeValueHelper(const DateTimeNodeValueHelper&); // not implemented
320  DLLLOCAL DateTimeNodeValueHelper& operator=(const DateTimeNodeValueHelper&); // not implemented
321  DLLLOCAL void *operator new(size_t); // not implemented, make sure it is not new'ed
322 
323 public:
326  if (!n) {
327  dt = ZeroDate;
328  temp = false;
329  return;
330  }
331 
332  // optmization without virtual function call for most common case
333  if (n->getType() == NT_DATE) {
334  dt = const_cast<DateTimeNode *>(reinterpret_cast<const DateTimeNode *>(n));
335  temp = false;
336  return;
337  }
338 
339  dt = new DateTimeNode();
341  temp = true;
342  }
343 
346  if (dt && temp)
347  dt->deref();
348  }
349 
350  DLLLOCAL const DateTimeNode *operator->() { return dt; }
351  DLLLOCAL const DateTimeNode *operator*() { return dt; }
352 
354 
359  if (temp)
360  temp = false;
361  else if (dt)
362  dt->ref();
363  return dt;
364  }
365 };
366 
367 #endif
DLLLOCAL DateTimeNodeValueHelper(const AbstractQoreNode *n)
gets the DateTimeNode value and sets the temporary flag
Definition: DateTimeNode.h:325
static DLLEXPORT DateTimeNode * makeRelative(int n_year, int n_month, int n_day, int n_hour=0, int n_minute=0, int n_second=0, int n_us=0)
static "constructor" to create a relative time, including microseconds
DLLLOCAL DateTimeValueHelper(const AbstractQoreNode *n)
gets the DateTime value and set the delete flag
Definition: DateTimeNode.h:285
DLLEXPORT DateTimeNode * unaryMinus() const
returns the negative time from the current time
virtual DLLEXPORT bool is_equal_soft(const AbstractQoreNode *v, ExceptionSink *xsink) const
tests for equality with possible type conversion (soft compare)
virtual DLLEXPORT bool is_equal_hard(const AbstractQoreNode *v, ExceptionSink *xsink) const
tests for equality ("deep compare" including all contained values for container types) without type c...
Holds absolute and relative date/time values in Qore with precision to the microsecond.
Definition: DateTime.h:93
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:55
DLLLOCAL ~DateTimeNodeValueHelper()
dereferences the DateTimeNode value if necessary
Definition: DateTimeNode.h:345
const qore_type_t NT_DATE
type value for DateTimeNode
Definition: node_types.h:46
virtual DLLEXPORT class DateTime * getDateTimeRepresentation(bool &del) const
returns the DateTime representation of this type (default implementation: returns ZeroDate...
static DLLLOCAL const char * getStaticTypeName()
returns the type name (useful in templates)
Definition: DateTimeNode.h:224
DLLEXPORT DateTimeNode * add(const DateTime *dt) const
adds a DateTime value to the current value and returns the new value, the caller owns the pointer's r...
virtual DLLEXPORT ~DateTimeNode()
protected destructor only called when references = 0, use deref() instead
DLLEXPORT DateTimeNode * subtractBy(const DateTime *dt) const
subtracts a DateTime value from the current value and returns the new value, the caller owns the poin...
signed short qore_type_t
used to identify unique Qore data and parse types (descendents of AbstractQoreNode) ...
Definition: common.h:67
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:50
static DLLLOCAL qore_type_t getStaticTypeCode()
returns the type code (useful in templates)
Definition: DateTimeNode.h:229
virtual DLLEXPORT class AbstractQoreNode * realCopy() const
returns a copy of the object; the caller owns the reference count
manages calls to AbstractQoreNode::getDateTimeRepresentation() when a simple DateTime value is requir...
Definition: DateTimeNode.h:274
Qore's parse tree/value type for date-time values, reference-counted, dynamically-allocated only...
Definition: DateTimeNode.h:44
virtual DLLEXPORT const char * getTypeName() const
returns the type name as a c string
virtual DLLEXPORT DateTime * getDateTimeRepresentation(bool &del) const
returns "this" as a DateTime, del is set to false
virtual DLLEXPORT QoreString * getAsString(bool &del, int foff, ExceptionSink *xsink) const
returns the date/time value as a formatted string for n and N printf formatting, del is set to true ...
DLLEXPORT void ref() const
increments the reference count
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:43
virtual DLLEXPORT QoreString * getStringRepresentation(bool &del) const
returns a string in the format YYYYMMDDHHmmSS, del is set to true
manages calls to AbstractQoreNode::getDateTimeRepresentation() when a DateTimeNode value is required ...
Definition: DateTimeNode.h:314
DLLLOCAL qore_type_t getType() const
returns the data type
Definition: AbstractQoreNode.h:294
DLLLOCAL DateTimeNode * getReferencedValue()
returns a referenced value - the caller will own the reference
Definition: DateTimeNode.h:358
static DLLEXPORT DateTimeNode * makeAbsoluteLocal(const AbstractQoreZoneInfo *zone, int64 seconds, int us=0)
static "constructor" to create an absolute time as an offset from the given time zone's epoch...
static DLLEXPORT DateTimeNode * getDateFromISOWeek(int year, int week, int day, ExceptionSink *xsink)
returns a DateTimeNode value as generated from the ISO-8601 week information
base class for simple value types
Definition: AbstractQoreNode.h:425
DLLEXPORT DateTimeNode * copy() const
returns a copy of the DateTimeNode, the caller owns the pointer's reference count ...
DLLLOCAL ~DateTimeValueHelper()
deletes the DateTime value being managed if necessary
Definition: DateTimeNode.h:302
static DLLEXPORT DateTimeNode * makeAbsolute(const AbstractQoreZoneInfo *n_zone, int n_year, int n_month, int n_day, int n_hour=0, int n_minute=0, int n_second=0, int n_us=0)
static "constructor" to create an absolute time, including microseconds