date.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_DATE_H
30 #define TNTDB_DATE_H
31 
32 #include <string>
33 
34 namespace tntdb
35 {
37  class Date
38  {
39  private:
40  unsigned short _year;
41  unsigned short _month;
42  unsigned short _day;
43 
44  public:
46  Date()
47  : _year(0), _month(0), _day(0)
48  { }
49 
54  Date(unsigned short year,
55  unsigned short month,
56  unsigned short day)
57  : _year(year),
58  _month(month),
59  _day(day)
60  { }
61 
62  static Date localtime();
63  static Date gmtime();
64 
66  unsigned short getYear() const { return _year; }
68  unsigned short getMonth() const { return _month; }
70  unsigned short getDay() const { return _day; }
72  unsigned short getWDay() const;
73 
74  bool isNull() const { return _month == 0; }
75 
77  void set(unsigned short year,
78  unsigned short month,
79  unsigned short day)
80  {
81  _year = year;
82  _month = month;
83  _day = day;
84  }
85 
87  std::string getIso() const;
88 
93  static Date fromIso(const std::string& s);
94 
95  bool operator== (const Date& dt) const
96  {
97  return _year == dt._year
98  && _month == dt._month
99  && _day == dt._day;
100  }
101 
102  bool operator!= (const Date& dt) const
103  { return !operator==(dt); }
104 
105  bool operator< (const Date& dt) const
106  {
107  return _year < dt._year ? true
108  : _year > dt._year ? false
109  : _month < dt._month ? true
110  : _month > dt._month ? false
111  : _day < dt._day;
112  }
113 
114  bool operator> (const Date& dt) const
115  { return dt < *this; }
116 
117  bool operator<= (const Date& dt) const
118  { return !(*this > dt); }
119 
120  bool operator>= (const Date& dt) const
121  { return !(*this < dt); }
122  };
123 }
124 
125 #endif // TNTDB_DATE_H
126