datetime.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef TNTDB_DATETIME_H
30 #define TNTDB_DATETIME_H
31 
32 #include <string>
33 #include <tntdb/date.h>
34 #include <tntdb/time.h>
35 
36 namespace tntdb
37 {
39  class Datetime
40  {
41  private:
42  unsigned short _year;
43  unsigned short _month;
44  unsigned short _day;
45  unsigned short _hour;
46  unsigned short _minute;
47  unsigned short _second;
48  unsigned short _millis;
49 
50  public:
53  : _year(0),
54  _month(0),
55  _day(0),
56  _hour(0),
57  _minute(0),
58  _second(0),
59  _millis(0)
60  { }
61 
63  Datetime(const Date& date, const Time& time)
64  : _year(date.getYear()),
65  _month(date.getMonth()),
66  _day(date.getDay()),
67  _hour(time.getHour()),
68  _minute(time.getMinute()),
69  _second(time.getSecond()),
70  _millis(time.getMillis())
71  { }
72 
77  Datetime(unsigned short year,
78  unsigned short month,
79  unsigned short day,
80  unsigned short hour,
81  unsigned short minute,
82  unsigned short second,
83  unsigned short millis = 0)
84  : _year(year),
85  _month(month),
86  _day(day),
87  _hour(hour),
88  _minute(minute),
89  _second(second),
90  _millis(millis)
91  { }
92 
93  static Datetime localtime();
94  static Datetime gmtime();
95 
97  unsigned short getYear() const { return _year; }
99  unsigned short getMonth() const { return _month; }
101  unsigned short getDay() const { return _day; }
103  unsigned short getHour() const { return _hour; }
105  unsigned short getMinute() const { return _minute; }
107  unsigned short getSecond() const { return _second; }
109  unsigned short getMillis() const { return _millis; }
110 
111  Date getDate() const { return Date(_year, _month, _day); }
112  Time getTime() const { return Time(_hour, _minute, _second, _millis); }
113 
114  bool isNull() const { return _month == 0; }
115 
117  std::string getIso() const;
118 
123  static Datetime fromIso(const std::string& s);
124 
129  void set(unsigned short year,
130  unsigned short month,
131  unsigned short day,
132  unsigned short hour,
133  unsigned short minute,
134  unsigned short second,
135  unsigned short millis = 0)
136  {
137  _year = year;
138  _month = month;
139  _day = day;
140  _hour = hour;
141  _minute = minute;
142  _second = second;
143  _millis = millis;
144  }
145 
146  bool operator== (const Datetime& dt) const
147  {
148  return _year == dt._year
149  && _month == dt._month
150  && _day == dt._day
151  && _hour == dt._hour
152  && _minute == dt._minute
153  && _second == dt._second
154  && _millis == dt._millis;
155  }
156 
157  bool operator!= (const Datetime& dt) const
158  { return !operator==(dt); }
159 
160  bool operator< (const Datetime& dt) const
161  {
162  return _year < dt._year ? true
163  : _year > dt._year ? false
164  : _month < dt._month ? true
165  : _month > dt._month ? false
166  : _day < dt._day ? true
167  : _day > dt._day ? false
168  : _hour < dt._hour ? true
169  : _hour > dt._hour ? false
170  : _minute < dt._minute ? true
171  : _minute > dt._minute ? false
172  : _second < dt._second ? true
173  : _second > dt._second ? false
174  : _millis < dt._millis;
175  }
176 
177  bool operator> (const Datetime& dt) const
178  { return dt < *this; }
179 
180  bool operator<= (const Datetime& dt) const
181  { return !(*this > dt); }
182 
183  bool operator>= (const Datetime& dt) const
184  { return !(*this < dt); }
185  };
186 }
187 
188 #endif // TNTDB_DATETIME_H
189