Qore Programming Language  0.8.7
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 2003 - 2013 David Nichols
10 
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU Lesser General Public
13  License as published by the Free Software Foundation; either
14  version 2.1 of the License, or (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  Lesser General Public License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public
22  License along with this library; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25 
26 #ifndef _QORE_DATETIMENODE_H
27 
28 #define _QORE_DATETIMENODE_H
29 
30 #include <qore/AbstractQoreNode.h>
31 #include <qore/DateTime.h>
32 
33 class qore_date_private;
34 
36 class DateTimeNode : public SimpleValueQoreNode, public DateTime {
37 private:
39  DLLLOCAL DateTimeNode(const DateTime *);
40 
42  DLLLOCAL DateTimeNode& operator=(const DateTimeNode &);
43 
45 
49  DLLEXPORT virtual bool getAsBoolImpl() const;
50 
52 
55  DLLEXPORT virtual int getAsIntImpl() const;
56 
58 
61  DLLEXPORT virtual int64 getAsBigIntImpl() const;
62 
64 
67  DLLEXPORT virtual double getAsFloatImpl() const;
68 
70  DLLLOCAL DateTimeNode(qore_date_private *n_priv);
71 
72 protected:
74  DLLEXPORT virtual ~DateTimeNode();
75 
76 public:
78 
81  DLLEXPORT DateTimeNode(bool r = false);
82 
84 
94  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);
95 
97 
100  DLLEXPORT DateTimeNode(int64 seconds);
101 
103 
107  DLLEXPORT DateTimeNode(int64 seconds, int ms);
108 
110 
112  DLLEXPORT DateTimeNode(const char *date);
113 
115 
118  DLLEXPORT DateTimeNode(const AbstractQoreZoneInfo *zone, const char *date);
119 
121 
124  DLLEXPORT DateTimeNode(struct tm *tms);
125 
127  DLLEXPORT DateTimeNode(const DateTimeNode &dt);
128 
130  DLLEXPORT DateTimeNode(const DateTime &dt);
131 
133 
138  DLLEXPORT virtual QoreString *getStringRepresentation(bool &del) const;
139 
141 
144  DLLEXPORT virtual void getStringRepresentation(QoreString &str) const;
145 
147 
151  DLLEXPORT virtual DateTime *getDateTimeRepresentation(bool &del) const;
152 
154 
157  DLLEXPORT virtual void getDateTimeRepresentation(DateTime &dt) const;
158 
160 
169  DLLEXPORT virtual QoreString *getAsString(bool &del, int foff, ExceptionSink *xsink) const;
170 
172 
179  DLLEXPORT virtual int getAsString(QoreString &str, int foff, ExceptionSink *xsink) const;
180 
181  DLLEXPORT virtual class AbstractQoreNode *realCopy() const;
182 
184 
188  DLLEXPORT virtual bool is_equal_soft(const AbstractQoreNode *v, ExceptionSink *xsink) const;
189  DLLEXPORT virtual bool is_equal_hard(const AbstractQoreNode *v, ExceptionSink *xsink) const;
190 
192  DLLEXPORT virtual const char *getTypeName() const;
193 
195 
198  DLLEXPORT DateTimeNode *copy() const;
199 
201 
204  DLLEXPORT DateTimeNode *add(const DateTime *dt) const;
205 
207 
210  DLLEXPORT DateTimeNode *subtractBy(const DateTime *dt) const;
211 
213  DLLEXPORT DateTimeNode *unaryMinus() const;
214 
216  DLLLOCAL static const char *getStaticTypeName() {
217  return "date";
218  }
219 
221  DLLLOCAL static qore_type_t getStaticTypeCode() {
222  return NT_DATE;
223  }
224 
226 
233  DLLEXPORT static DateTimeNode *getDateFromISOWeek(int year, int week, int day, ExceptionSink *xsink);
234 
236  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);
237 
239 
244  DLLEXPORT static DateTimeNode *makeAbsolute(const AbstractQoreZoneInfo *zone, int64 seconds, int us = 0);
245 
247 
252  DLLEXPORT static DateTimeNode *makeAbsoluteLocal(const AbstractQoreZoneInfo *zone, int64 seconds, int us = 0);
253 
255  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);
256 };
257 
258 DLLEXPORT extern DateTimeNode *ZeroDate;
259 
261 
267 private:
268  const DateTime *dt;
269  bool del;
270 
271  DLLLOCAL DateTimeValueHelper(const DateTimeValueHelper&); // not implemented
272  DLLLOCAL DateTimeValueHelper& operator=(const DateTimeValueHelper&); // not implemented
273  DLLLOCAL void *operator new(size_t); // not implemented, make sure it is not new'ed
274 
275 public:
278  // optmization without virtual function call for most common case
279  if (n) {
280  if (n->getType() == NT_DATE) {
281  dt = reinterpret_cast<const DateTimeNode *>(n);
282  del = false;
283  }
284  else
285  dt = n->getDateTimeRepresentation(del);
286  }
287  else {
288  dt = ZeroDate;
289  del = false;
290  }
291  }
292 
294  DLLLOCAL ~DateTimeValueHelper() {
295  if (del)
296  delete const_cast<DateTime *>(dt);
297  }
298  DLLLOCAL const DateTime *operator->() { return dt; }
299  DLLLOCAL const DateTime *operator*() { return dt; }
300 };
301 
303 
307 private:
308  DateTimeNode *dt;
309  bool temp;
310 
311  DLLLOCAL DateTimeNodeValueHelper(const DateTimeNodeValueHelper&); // not implemented
312  DLLLOCAL DateTimeNodeValueHelper& operator=(const DateTimeNodeValueHelper&); // not implemented
313  DLLLOCAL void *operator new(size_t); // not implemented, make sure it is not new'ed
314 
315 public:
318  if (!n) {
319  dt = ZeroDate;
320  temp = false;
321  return;
322  }
323 
324  // optmization without virtual function call for most common case
325  if (n->getType() == NT_DATE) {
326  dt = const_cast<DateTimeNode *>(reinterpret_cast<const DateTimeNode *>(n));
327  temp = false;
328  return;
329  }
330 
331  dt = new DateTimeNode();
333  temp = true;
334  }
335 
338  if (dt && temp)
339  dt->deref();
340  }
341 
342  DLLLOCAL const DateTimeNode *operator->() { return dt; }
343  DLLLOCAL const DateTimeNode *operator*() { return dt; }
344 
346 
351  if (temp)
352  temp = false;
353  else if (dt)
354  dt->ref();
355  return dt;
356  }
357 };
358 
359 #endif