time.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_TIME_H
30 #define TNTDB_TIME_H
31 
32 #include <string>
33 
34 namespace tntdb
35 {
37  class Time
38  {
39  private:
40  short _hour;
41  unsigned short _minute;
42  unsigned short _second;
43  unsigned short _millis;
44 
45  public:
47  Time()
48  : _hour(-1),
49  _minute(0),
50  _second(0),
51  _millis(0)
52  { }
53 
58  Time(unsigned short hour,
59  unsigned short minute,
60  unsigned short second,
61  unsigned short millis = 0)
62  : _hour(hour),
63  _minute(minute),
64  _second(second),
65  _millis(millis)
66  { }
67 
68  static Time localtime();
69  static Time gmtime();
70 
72  unsigned short getHour() const { return static_cast<unsigned short>(_hour); }
74  unsigned short getMinute() const { return _minute; }
76  unsigned short getSecond() const { return _second; }
78  unsigned short getMillis() const { return _millis; }
79 
80  bool isNull() const { return _hour < 0; }
81 
86  void set(unsigned short hour,
87  unsigned short minute,
88  unsigned short second,
89  unsigned short millis = 0)
90  {
91  _hour = static_cast<short>(hour);
92  _minute = minute;
93  _second = second;
94  _millis = millis;
95  }
96 
98  std::string getIso() const;
99 
104  static Time fromIso(const std::string& s);
105 
106  bool operator== (const Time& dt) const
107  {
108  return _hour == dt._hour
109  && _minute == dt._minute
110  && _second == dt._second
111  && _millis == dt._millis;
112  }
113 
114  bool operator!= (const Time& dt) const
115  { return !operator==(dt); }
116 
117  bool operator< (const Time& dt) const
118  {
119  return _hour < dt._hour ? true
120  : _hour > dt._hour ? false
121  : _minute < dt._minute ? true
122  : _minute > dt._minute ? false
123  : _second < dt._second ? true
124  : _second > dt._second ? false
125  : _millis < dt._millis;
126  }
127 
128  bool operator> (const Time& dt) const
129  { return dt < *this; }
130 
131  bool operator<= (const Time& dt) const
132  { return !(*this > dt); }
133 
134  bool operator>= (const Time& dt) const
135  { return !(*this < dt); }
136  };
137 }
138 
139 #endif // TNTDB_TIME_H
140