E-MailRelay
gtime.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file gtime.cpp
19///
20
21#include "gdef.h"
22#include "gtime.h"
23#include "gdatetime.h"
24#include <sstream>
25#include <algorithm>
26
27G::Time::Time( int hh , int mm , int ss ) :
28 m_hh(std::min(23,std::max(0,hh))) ,
29 m_mm(std::min(59,std::max(0,mm))) ,
30 m_ss(std::min((hh==23&&mm==59)?60:59,std::max(0,ss)))
31{
32}
33
35 m_hh(tm.hour()) ,
36 m_mm(tm.min()) ,
37 m_ss(tm.sec())
38{
39}
40
42 Time(SystemTime::now().utc())
43{
44}
45
47 Time(t.utc())
48{
49}
50
52 Time(SystemTime::now().local())
53{
54}
55
57 Time(t.local())
58{
59}
60
61int G::Time::hours() const
62{
63 return m_hh ;
64}
65
67{
68 return m_mm ;
69}
70
72{
73 return m_ss ;
74}
75
76std::string G::Time::hhmmss( const char * sep ) const
77{
78 if( sep == nullptr ) sep = "" ;
79 std::ostringstream ss ;
80 ss << (m_hh/10) << (m_hh%10) << sep << (m_mm/10) << (m_mm%10) << sep << (m_ss/10) << (m_ss%10) ;
81 return ss.str() ;
82}
83
84std::string G::Time::hhmm( const char * sep ) const
85{
86 if( sep == nullptr ) sep = "" ;
87 std::ostringstream ss ;
88 ss << (m_hh/10) << (m_hh%10) << sep << (m_mm/10) << (m_mm%10) ;
89 return ss.str() ;
90}
91
92std::string G::Time::ss() const
93{
94 std::ostringstream ss ;
95 ss << (m_ss/10) << (m_ss%10) ;
96 return ss.str() ;
97}
98
99unsigned int G::Time::value() const
100{
101 return
102 static_cast<unsigned int>(std::max(0,std::min(23,m_hh))) * 3600U +
103 static_cast<unsigned int>(std::max(0,std::min(59,m_mm))) * 60U +
104 static_cast<unsigned int>(std::max(0,std::min(59,m_ss))) ; // ignore leap seconds here
105}
106
107G::Time G::Time::at( unsigned int s )
108{
109 unsigned int hh = s / 3600U ;
110 unsigned int mm_ss = s - (hh*3600U) ;
111 return {
112 std::max(0,std::min(23,static_cast<int>(hh))) ,
113 std::max(0,std::min(59,static_cast<int>(mm_ss/60U))) ,
114 std::max(0,std::min(59,static_cast<int>(mm_ss%60U))) } ;
115}
116
117bool G::Time::operator==( const Time & other ) const
118{
119 return m_hh == other.m_hh && m_mm == other.m_mm && m_ss == other.m_ss ;
120}
121
122bool G::Time::operator!=( const Time & other ) const
123{
124 return !(*this==other) ;
125}
126
An encapsulation of 'struct std::tm'.
Definition: gdatetime.h:45
Represents a unix-epoch time with microsecond resolution.
Definition: gdatetime.h:125
An overload discriminator class for Time constructors.
Definition: gtime.h:42
A simple time-of-day (hh/mm/ss) class.
Definition: gtime.h:39
bool operator==(const Time &) const
Comparison operator.
Definition: gtime.cpp:117
unsigned int value() const
Returns the time as the number of seconds since midnight (ignoring leap seconds).
Definition: gtime.cpp:99
int minutes() const
Returns the minutes (0 <= m < 60).
Definition: gtime.cpp:66
static Time at(unsigned int)
Factory function for a time that is the given number of seconds since midnight (see value()).
Definition: gtime.cpp:107
std::string hhmm(const char *sep=nullptr) const
Returns the hhmm string.
Definition: gtime.cpp:84
int hours() const
Returns the hours (0 <= h < 24).
Definition: gtime.cpp:61
Time()
Constructor for the current time, using UTC.
Definition: gtime.cpp:41
std::string ss() const
Returns the seconds as a two-digit decimal string.
Definition: gtime.cpp:92
int seconds() const
Returns the seconds (0 <= s <= 61).
Definition: gtime.cpp:71
bool operator!=(const Time &) const
Comparison operator.
Definition: gtime.cpp:122
std::string hhmmss(const char *sep=nullptr) const
Returns the hhmmss string.
Definition: gtime.cpp:76