Qore Programming Language  1.7.0
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 - 2022 Qore Technologies, s.r.o.
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 class LocalVar;
43 
45 class DateTimeNode : public SimpleValueQoreNode, public DateTime {
46 private:
48  DLLLOCAL DateTimeNode(const DateTime*) = delete;
49 
51  DLLLOCAL DateTimeNode& operator=(const DateTimeNode&) = delete;
52 
54 
58  DLLEXPORT virtual bool getAsBoolImpl() const;
59 
61 
64  DLLEXPORT virtual int getAsIntImpl() const;
65 
67 
70  DLLEXPORT virtual int64 getAsBigIntImpl() const;
71 
73 
76  DLLEXPORT virtual double getAsFloatImpl() const;
77 
79  DLLLOCAL DateTimeNode(qore_date_private* n_priv);
80 
81 protected:
83  DLLEXPORT virtual ~DateTimeNode();
84 
85 public:
87 
90  DLLEXPORT DateTimeNode(bool r = false);
91 
93 
103  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);
104 
106 
109  DLLEXPORT DateTimeNode(int64 seconds);
110 
112 
116  DLLEXPORT DateTimeNode(int64 seconds, int ms);
117 
119 
125  DLLEXPORT explicit DateTimeNode(const AbstractQoreZoneInfo* zone, const QoreValue v);
126 
128 
131  DLLEXPORT explicit DateTimeNode(const QoreValue v);
132 
134 
136  DLLEXPORT DateTimeNode(const char* date);
137 
139 
144  DLLEXPORT DateTimeNode(const char* date, ExceptionSink* xsink);
145 
147 
151  DLLEXPORT DateTimeNode(const AbstractQoreZoneInfo* zone, const char* date);
152 
154 
157  DLLEXPORT DateTimeNode(struct tm* tms);
158 
160  DLLEXPORT DateTimeNode(const DateTimeNode& dt);
161 
163  DLLEXPORT DateTimeNode(const DateTime& dt);
164 
166 
171  DLLEXPORT virtual QoreString* getStringRepresentation(bool& del) const;
172 
174 
177  DLLEXPORT virtual void getStringRepresentation(QoreString& str) const;
178 
180 
184  DLLEXPORT virtual DateTime* getDateTimeRepresentation(bool& del) const;
185 
187 
190  DLLEXPORT virtual void getDateTimeRepresentation(DateTime& dt) const;
191 
193 
202  DLLEXPORT virtual QoreString* getAsString(bool& del, int foff, ExceptionSink* xsink) const;
203 
205 
212  DLLEXPORT virtual int getAsString(QoreString& str, int foff, ExceptionSink* xsink) const;
213 
214  DLLEXPORT virtual AbstractQoreNode* realCopy() const;
215 
217 
221  DLLEXPORT virtual bool is_equal_soft(const AbstractQoreNode* v, ExceptionSink* xsink) const;
222  DLLEXPORT virtual bool is_equal_hard(const AbstractQoreNode* v, ExceptionSink* xsink) const;
223 
225  DLLEXPORT virtual const char* getTypeName() const;
226 
228 
231  DLLEXPORT DateTimeNode* copy() const;
232 
234 
237  DLLEXPORT DateTimeNode* add(const DateTime* dt) const;
238 
240 
243  DLLEXPORT DateTimeNode* add(const DateTime& dt) const;
244 
246 
249  DLLEXPORT DateTimeNode* subtractBy(const DateTime* dt) const;
250 
252 
255  DLLEXPORT DateTimeNode* subtractBy(const DateTime& dt) const;
256 
258  DLLEXPORT DateTimeNode* unaryMinus() const;
259 
261  DLLEXPORT DateTimeNode* refSelf() const;
262 
264  DLLEXPORT virtual int parseInit(QoreValue& val, QoreParseContext& parse_context);
265 
267  DLLLOCAL static const char* getStaticTypeName() {
268  return "date";
269  }
270 
272  DLLLOCAL static qore_type_t getStaticTypeCode() {
273  return NT_DATE;
274  }
275 
277 
284  DLLEXPORT static DateTimeNode* getDateFromISOWeek(int year, int week, int day, ExceptionSink* xsink);
285 
287  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);
288 
290 
292  DLLEXPORT static DateTimeNode* makeAbsolute(const AbstractQoreZoneInfo* n_zone, int n_year, int n_month, int n_day, int n_hour, int n_minute, int n_second, int n_us, ExceptionSink* xsink);
293 
295 
300  DLLEXPORT static DateTimeNode* makeAbsolute(const AbstractQoreZoneInfo* zone, int64 seconds, int us = 0);
301 
303 
308  DLLEXPORT static DateTimeNode* makeAbsoluteLocal(const AbstractQoreZoneInfo* zone, int64 seconds, int us = 0);
309 
311  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);
312 
314  DLLEXPORT static DateTimeNode* makeRelativeFromSeconds(int64 n_second, int n_us = 0);
315 };
316 
317 DLLEXPORT extern DateTimeNode* ZeroDate;
318 DLLEXPORT extern DateTimeNode* OneDate;
319 
321 
327 private:
328  const DateTime* dt;
329  bool del;
330 
331  DLLLOCAL DateTimeValueHelper(const DateTimeValueHelper&); // not implemented
332  DLLLOCAL DateTimeValueHelper& operator=(const DateTimeValueHelper&); // not implemented
333  DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
334 
335 public:
338 
340  DLLEXPORT DateTimeValueHelper(const QoreValue& n);
341 
343  DLLEXPORT ~DateTimeValueHelper();
344 
345  DLLLOCAL const DateTime* operator->() { return dt; }
346  DLLLOCAL const DateTime* operator*() { return dt; }
347 };
348 
350 
354 private:
355  DateTimeNode* dt;
356  bool del;
357 
358  DLLLOCAL DateTimeNodeValueHelper(const DateTimeNodeValueHelper&); // not implemented
359  DLLLOCAL DateTimeNodeValueHelper& operator=(const DateTimeNodeValueHelper&); // not implemented
360  DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
361 
362 public:
365  if (!n) {
366  dt = ZeroDate;
367  del = false;
368  return;
369  }
370 
371  // optmization without virtual function call for most common case
372  if (n->getType() == NT_DATE) {
373  dt = const_cast<DateTimeNode*>(reinterpret_cast<const DateTimeNode*>(n));
374  del = false;
375  return;
376  }
377 
378  dt = new DateTimeNode;
380  del = true;
381  }
382 
384 
389 
391  DLLEXPORT DateTimeNodeValueHelper(const QoreValue& n);
392 
395  if (dt && del)
396  dt->deref();
397  }
398 
399  DLLLOCAL const DateTimeNode* operator->() { return dt; }
400  DLLLOCAL const DateTimeNode* operator*() { return dt; }
401 
403 
408  if (del)
409  del = false;
410  else if (dt)
411  dt->ref();
412  return dt;
413  }
414 };
415 
416 #endif
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:57
DLLEXPORT void ref() const
increments the reference count
virtual DLLEXPORT class DateTime * getDateTimeRepresentation(bool &del) const
returns the DateTime representation of this type (default implementation: returns ZeroDate,...
DLLLOCAL qore_type_t getType() const
returns the data type
Definition: AbstractQoreNode.h:175
Holds absolute and relative date/time values in Qore with precision to the microsecond.
Definition: DateTime.h:93
Qore's parse tree/value type for date-time values, reference-counted, dynamically-allocated only.
Definition: DateTimeNode.h:45
static DLLLOCAL qore_type_t getStaticTypeCode()
returns the type code (useful in templates)
Definition: DateTimeNode.h:272
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 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...
static DLLEXPORT DateTimeNode * getDateFromISOWeek(int year, int week, int day, ExceptionSink *xsink)
returns a DateTimeNode value as generated from the ISO-8601 week information
DLLEXPORT DateTimeNode * refSelf() const
returns this with an incremented ref count
virtual DLLEXPORT const char * getTypeName() const
returns the type name as a c string
static DLLEXPORT DateTimeNode * makeRelativeFromSeconds(int64 n_second, int n_us=0)
static "constructor" to create a relative time, including microseconds
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,...
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 int parseInit(QoreValue &val, QoreParseContext &parse_context)
returns the type information
virtual DLLEXPORT QoreString * getStringRepresentation(bool &del) const
returns a string in the format YYYYMMDDHHmmSS, del is set to true
virtual DLLEXPORT DateTime * getDateTimeRepresentation(bool &del) const
returns "this" as a DateTime, del is set to false
DLLEXPORT DateTimeNode * copy() const
returns a copy of the DateTimeNode, the caller owns the pointer's reference count
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...
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
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...
static DLLLOCAL const char * getStaticTypeName()
returns the type name (useful in templates)
Definition: DateTimeNode.h:267
virtual DLLEXPORT AbstractQoreNode * realCopy() const
returns a copy of the object; the caller owns the reference count
virtual DLLEXPORT ~DateTimeNode()
protected destructor only called when references = 0, use deref() instead
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
manages calls to AbstractQoreNode::getDateTimeRepresentation() when a DateTimeNode value is required
Definition: DateTimeNode.h:353
DLLLOCAL DateTimeNode * getReferencedValue()
returns a referenced value - the caller will own the reference
Definition: DateTimeNode.h:407
DLLLOCAL ~DateTimeNodeValueHelper()
dereferences the DateTimeNode value if necessary
Definition: DateTimeNode.h:394
manages calls to AbstractQoreNode::getDateTimeRepresentation() when a simple DateTime value is requir...
Definition: DateTimeNode.h:326
DLLEXPORT ~DateTimeValueHelper()
deletes the DateTime value being managed if necessary
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
base class for simple value types
Definition: AbstractQoreNode.h:372
int16_t qore_type_t
used to identify unique Qore data and parse types (descendents of AbstractQoreNode)
Definition: common.h:70
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:260
const qore_type_t NT_DATE
type value for DateTimeNode
Definition: node_types.h:46
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:275