File: TimeStamp.h

package info (click to toggle)
qtop 2.3.4-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 3,980 kB
  • sloc: cpp: 40,477; makefile: 7
file content (248 lines) | stat: -rw-r--r-- 5,846 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#ifndef TimeStamp_h
#define TimeStamp_h

/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* Any WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program.  If not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/

#include "Counter.h"

#include <QDataStream>
#include <QDateTime>
#include <time.h>

//* time manipulation object
class TimeStamp:private Base::Counter<TimeStamp>
{

    public:

    //* default string for invalid timestamps
    static const QString TimeStampUnknown;

    //* empty creator
    explicit TimeStamp():
        Counter( "TimeStamp" )
    {}

    //* time_t creator
    explicit TimeStamp( time_t time ):
        Counter( "TimeStamp" )
    { setTime( time ); }

    //* destructor
    virtual ~TimeStamp() = default;

    //* set time
    bool setTime( time_t time )
    {
        valid_ = time >= 0;
        time_ = time;
        if( valid_ ) tm_ = *localtime( &time );
        return valid_;
    }

    //* convert tm_ into time_
    bool makeTime()
    {
        valid_ = (time_ = mktime( &tm_ ) ) >= 0;
        return valid_;
    }

    //*@name accessors
    //@{

    //* checks if timestamp is valid or not
    bool isValid() const
    { return valid_; }

    //* timestamp format enumeration
    enum class Format
    {

        //* DD/MM/YYYY
        Date,

        //* YYYY/MM/DD
        DateUS,

        //* HH:MM
        Time,

        //* HH:MM:SS
        TimeLong,

        //* DD MONTH_string YYYY HH:MM:SS (week_day)
        Long,

        //* DD/MM/YYYY HH:MM
        Short,

        //* YYYY/MM/DD HH:MM
        ShortUS,

        //* DD_MM_YY
        DateTag,

        //* MONTH_string DD HH:MM
        JobTag

    };

    //* if timestamp is valid returns formated string
    QString toString( Format = Format::Short ) const;

    //* if timestamp is valid, returns formated string
    QString toString( Qt::DateFormat format ) const
    { return valid_ ? QDateTime::fromTime_t( time_ ).toString( format ) : TimeStampUnknown; }

    //* if timestamp is valid, returns formated string
    QString toString( const QString& format ) const
    { return valid_ ? QDateTime::fromTime_t( time_ ).toString( format ) : TimeStampUnknown; }

    //* returns time in second
    time_t unixTime() const
    { return time_; }

    //* returns TimeStamp corresponding to _now_
    static TimeStamp now();

    //* retrieve seconds (between 0 and 59)
    int seconds() const
    { return (valid_) ? tm_.tm_sec:0; }

    //* retrieve minutes (between 0 and 59)
    int minutes() const
    { return (valid_) ? tm_.tm_min:0; }

    //* retrieve hour (between 0 and 23)
    int hours() const
    { return (valid_) ? tm_.tm_hour:0; }

    //* retrieves day (between 1 and 31)
    int day() const
    { return (valid_) ? tm_.tm_mday:0; }

    //* retrieves month (between 1 and 12)
    int month() const
    { return (valid_) ? tm_.tm_mon+1:0; }

    //* retrieves year (between 1 and 12)
    int year() const
    { return (valid_) ? tm_.tm_year+1900:0; }

    //* true if same day
    bool isSameDay( const TimeStamp& other ) const
    {
        return
            tm_.tm_mday == other.tm_.tm_mday &&
            tm_.tm_mon == other.tm_.tm_mon &&
            tm_.tm_year == other.tm_.tm_year;
    }

    //* true if same day
    bool isSameYear( const TimeStamp& other ) const
    { return tm_.tm_year == other.tm_.tm_year; }

    //@}

    //*@name modifiers
    //@{

    //* seconds (between 0 and 59)
    TimeStamp& setSeconds( int value )
    {
        tm_.tm_sec = value;
        return *this;
    }

    //* minutes (between 0 and 59)
    TimeStamp& setMinutes( int value )
    {
        tm_.tm_min = value;
        return *this;
    }

    //* hour (between 0 and 23)
    TimeStamp& setHours( int value )
    {
        tm_.tm_hour = value;
        return *this;
    }

    //* retrieves day (between 1 and 31)
    TimeStamp& setDay( int value )
    {
        tm_.tm_mday = value;
        return *this;
    }

    //* retrieves month (between 1 and 12)
    TimeStamp& setMonth( int value )
    {
        tm_.tm_mon = value - 1;
        return *this;
    }

    //* retrieves month (from string)
    TimeStamp& setMonth( const QString& );

    //* retrieves year (between 1 and 12)
    TimeStamp& setYear( int value )
    {
        tm_.tm_year = value - 1900;
        return *this;
    }

    //* tells if timestamp is valid or not
    TimeStamp& setValid( bool value )
    {
        valid_ = value;
        return *this;
    }

    //@}

    private:

    //* true if timestamp is properly set
    bool valid_ = false;

    //* unix time in second
    time_t time_ = -1;

    //* time structure from localtime()
    struct tm tm_;

    //*@name serializer
    //@{
    friend QDataStream& operator << ( QDataStream&, const TimeStamp& );
    friend QDataStream& operator >> ( QDataStream&, TimeStamp& );
    //@}

    //* less than operator
    friend bool operator < (const TimeStamp& first, const TimeStamp& second)
    { return( first.time_ < second.time_ ); }

    //* equal to operator
    friend bool operator == (const TimeStamp& first, const TimeStamp& second)
    { return( first.time_ == second.time_ ); }

};

#endif