E-MailRelay

gtime.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 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
27 G::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
34 G::Time::Time( const BrokenDownTime & tm ) :
35 Time(tm.hour(),tm.min(),tm.sec())
36{
37}
38
39#ifndef G_LIB_SMALL
40 G::Time::Time() :
41 Time(SystemTime::now().utc())
42{
43}
44#endif
45
46#ifndef G_LIB_SMALL
47 G::Time::Time( SystemTime t ) :
48 Time(t.utc())
49{
50}
51#endif
52
53#ifndef G_LIB_SMALL
54 G::Time::Time( const LocalTime & ) :
55 Time(SystemTime::now().local())
56{
57}
58#endif
59
60#ifndef G_LIB_SMALL
61 G::Time::Time( SystemTime t , const LocalTime & ) :
62 Time(t.local())
63{
64}
65#endif
66
67#ifndef G_LIB_SMALL
68 int G::Time::hours() const
69{
70 return m_hh ;
71}
72#endif
73
74#ifndef G_LIB_SMALL
75 int G::Time::minutes() const
76{
77 return m_mm ;
78}
79#endif
80
81#ifndef G_LIB_SMALL
82 int G::Time::seconds() const
83{
84 return m_ss ;
85}
86#endif
87
88std::string G::Time::xx( int n )
89{
90 return std::string(1U,char('0'+n/10)).append(1U,char('0'+n%10)) ;
91}
92
93std::string G::Time::hhmmss( const char * sep ) const
94{
95 if( sep == nullptr ) sep = "" ;
96 return std::string(xx(m_hh)).append(sep).append(xx(m_mm)).append(sep).append(xx(m_ss)) ;
97}
98
99#ifndef G_LIB_SMALL
100std::string G::Time::hhmm( const char * sep ) const
101{
102 if( sep == nullptr ) sep = "" ;
103 return std::string(xx(m_hh)).append(sep).append(xx(m_mm)) ;
104}
105#endif
106
107#ifndef G_LIB_SMALL
108std::string G::Time::ss() const
109{
110 return xx( m_ss ) ;
111}
112#endif
113
114#ifndef G_LIB_SMALL
115 unsigned int G::Time::value() const
116{
117 return
118 static_cast<unsigned int>(std::max(0,std::min(23,m_hh))) * 3600U +
119 static_cast<unsigned int>(std::max(0,std::min(59,m_mm))) * 60U +
120 static_cast<unsigned int>(std::max(0,std::min(59,m_ss))) ; // ignore leap seconds here
121}
122#endif
123
124#ifndef G_LIB_SMALL
125 G::Time G::Time::at( unsigned int s )
126{
127 unsigned int hh = s / 3600U ;
128 unsigned int mm_ss = s - (hh*3600U) ;
129 return {
130 std::max(0,std::min(23,static_cast<int>(hh))) ,
131 std::max(0,std::min(59,static_cast<int>(mm_ss/60U))) ,
132 std::max(0,std::min(59,static_cast<int>(mm_ss%60U))) } ;
133}
134#endif
135
136 bool G::Time::operator==( const Time & other ) const
137{
138 return m_hh == other.m_hh && m_mm == other.m_mm && m_ss == other.m_ss ;
139}
140
141#ifndef G_LIB_SMALL
142 bool G::Time::operator!=( const Time & other ) const
143{
144 return !(*this==other) ;
145}
146#endif
147
G::BrokenDownTime
An encapsulation of 'struct std::tm'.
Definition: gdatetime.h:45
G::SystemTime
Represents a unix-epoch time with microsecond resolution.
Definition: gdatetime.h:140
G::Time::LocalTime
An overload discriminator class for Time constructors.
Definition: gtime.h:42
G::Time
A simple time-of-day (hh/mm/ss) class.
Definition: gtime.h:39
G::Time::operator==
bool operator==(const Time &) const
Comparison operator.
Definition: gtime.cpp:136
G::Time::value
unsigned int value() const
Returns the time as the number of seconds since midnight (ignoring leap seconds).
Definition: gtime.cpp:115
G::Time::minutes
int minutes() const
Returns the minutes (0 <= m < 60).
Definition: gtime.cpp:75
G::Time::at
static Time at(unsigned int)
Factory function for a time that is the given number of seconds since midnight (see value()).
Definition: gtime.cpp:125
G::Time::hhmm
std::string hhmm(const char *sep=nullptr) const
Returns the hhmm string.
Definition: gtime.cpp:100
G::Time::hours
int hours() const
Returns the hours (0 <= h < 24).
Definition: gtime.cpp:68
G::Time::Time
Time()
Constructor for the current time, using UTC.
Definition: gtime.cpp:40
G::Time::ss
std::string ss() const
Returns the seconds as a two-digit decimal string.
Definition: gtime.cpp:108
G::Time::seconds
int seconds() const
Returns the seconds (0 <= s <= 61).
Definition: gtime.cpp:82
G::Time::operator!=
bool operator!=(const Time &) const
Comparison operator.
Definition: gtime.cpp:142
G::Time::hhmmss
std::string hhmmss(const char *sep=nullptr) const
Returns the hhmmss string.
Definition: gtime.cpp:93
gdatetime.h
gtime.h
std
STL namespace.