World Ocean Simulation System (WOSS) library
time-definitions.h
Go to the documentation of this file.
1/* WOSS - World Ocean Simulation System -
2 *
3 * Copyright (C) 2009 2025 Federico Guerra
4 * and regents of the SIGNET lab, University of Padova
5 *
6 * Author: Federico Guerra - WOSS@guerra-tlc.com
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation;
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22
33#ifndef WOSS_TIME_DEFINITIONS_H
34#define WOSS_TIME_DEFINITIONS_H
35
36
37#include <iostream>
38#include <ctime>
39#include <cassert>
40#include <cmath>
41#include <climits>
42#include <memory>
43
44namespace woss {
45
46 static constexpr long TIME_NOT_SET_VALUE = (LONG_MIN);
47
55
56 public:
57
58 virtual ~TimeReference() = 0;
59
64 virtual std::unique_ptr<TimeReference> clone() const = 0;
65
70 virtual double getTimeReference() const = 0;
71
72 };
73
74 inline TimeReference::~TimeReference() = default;
75
83 class Time {
84
85 public:
86
90 Time();
91
96 Time( struct tm* time );
97
107 Time( int day, int month, int year, int hours = 0, int mins = 0, int seconds = 1 );
108
109 Time( const Time& copy ) = default;
110
111 Time( Time&& tmp ) = default;
112
113 ~Time() = default;
114
120 Time& setMonth( int m ) { assert( m >= 1 && m <= 12 ); timeinfo.tm_mon = m - 1; raw_time = mktime(&timeinfo);
121 return *this; }
122
128 Time& setDay( int d ) { assert( d >= 1 && d <= 31 ); timeinfo.tm_mday = d; raw_time = mktime(&timeinfo); return *this; }
129
135 Time& setYear( int y ) { assert( y >= 1900 ); timeinfo.tm_year = y - 1900; raw_time = mktime(&timeinfo); return *this; }
136
142 Time& setHours( int h ) { assert( h >= 0 && h <= 23 ); timeinfo.tm_hour = h; raw_time = mktime(&timeinfo); return *this; }
143
149 Time& setMinutes( int m ) { assert( m >= 0 && m <= 59 ); timeinfo.tm_min = m; raw_time = mktime(&timeinfo); return *this; }
150
156 Time& setSeconds( int s ) { assert( s >= 0 && s <= 59 ); timeinfo.tm_sec = s; raw_time = mktime(&timeinfo); return *this; }
157
162 static void setDebug(bool flag) { debug = flag; }
163
168 bool isValid() const { return ( raw_time != TIME_NOT_SET_VALUE ); }
169
174 int getMonth() const { return timeinfo.tm_mon; }
175
180 int getDay() const { return timeinfo.tm_mday; }
181
186 int getHours() const { return timeinfo.tm_hour; }
187
192 int getYear() const { return timeinfo.tm_year; }
193
198 int getMinutes() const { return timeinfo.tm_min; }
199
204 int getSeconds() const { return timeinfo.tm_sec; }
205
210 operator time_t() const;
211
212 Time& operator=( const Time& copy ) = default;
213
214 Time& operator=( Time&& tmp ) = default;
215
222 friend const Time operator+( const Time& left, const time_t right );
223
230 friend const Time operator-( const Time& left, const time_t right );
231
238 friend double operator-( const Time& left, const Time& right );
239
246 friend Time& operator+=( Time& left, time_t right );
247
254 friend Time& operator-=( Time& left, time_t right );
255
262 friend bool operator==( const Time& left, const Time& right );
263
270 friend bool operator!=( const Time& left, const Time& right );
271
278 friend bool operator>( const Time& left, const Time& right );
279
286 friend bool operator<( const Time& left, const Time& right );
287
294 friend bool operator<=( const Time& left, const Time& right );
295
302 friend bool operator>=( const Time& left, const Time& right );
303
310 friend std::ostream& operator<<( std::ostream& os, const Time& instance );
311
312 protected:
313
317 mutable struct tm timeinfo;
318
322 time_t raw_time;
323
327 static bool debug;
328
329 };
330
336 struct SimTime {
337
338 SimTime( Time time1 = Time(), Time time2 = Time() ) : start_time(time1), end_time(time2) { }
339
340 friend std::ostream& operator<<( std::ostream& os, const SimTime& instance ) {
341 os << "start time = " << instance.start_time << "; end time = " << instance.end_time;
342 return os;
343 }
344
346
348
349 };
350
351 // non-inline operator declarations
352 double operator-( const Time& left, const Time& right );
353
354 //inline functions
356 inline Time::operator time_t() const {
357 return( mktime(&timeinfo) );
358 }
359
360 inline bool operator==( const Time& left, const Time& right ) {
361 if ( &left == &right ) return true;
362 return( mktime(const_cast<tm*>(&left.timeinfo)) == mktime(const_cast<tm*>(&right.timeinfo)) );
363 }
364
365 inline bool operator!=( const Time& left, const Time& right ) {
366 if ( &left == &right ) return false;
367 return( mktime(const_cast<tm*>(&left.timeinfo)) != mktime(const_cast<tm*>(&right.timeinfo)) );
368 }
369
370 inline bool operator>( const Time& left, const Time& right ) {
371 if ( &left == &right ) return false;
372 return( mktime(const_cast<tm*>(&left.timeinfo)) > mktime(const_cast<tm*>(&right.timeinfo)) );
373 }
374
375 inline bool operator<( const Time& left, const Time& right ) {
376 if ( &left == &right ) return false;
377 return( mktime(const_cast<tm*>(&left.timeinfo)) < mktime(const_cast<tm*>(&right.timeinfo)) );
378 }
379
380 inline bool operator<=( const Time& left, const Time& right ) {
381 return( mktime(const_cast<tm*>(&left.timeinfo)) <= mktime(const_cast<tm*>(&right.timeinfo)) );
382 }
383
384 inline bool operator>=( const Time& left, const Time& right ) {
385 return( mktime(const_cast<tm*>(&left.timeinfo)) >= mktime(const_cast<tm*>(&right.timeinfo)) );
386 }
387
388 inline std::ostream& operator<<( std::ostream& os, const Time& time ) {
389 os << asctime(const_cast<tm*>(&time.timeinfo)) ;
390 return os;
391 }
392
393 inline const Time operator+( const Time& left, const time_t right ) {
394 time_t sum_time = mktime(const_cast<tm*>(&left.timeinfo)) + right;
395 return( Time( localtime( &sum_time ) ) );
396 }
397
398 inline const Time operator-( const Time& left, const time_t right ) {
399 time_t diff_time = mktime(const_cast<tm*>(&left.timeinfo)) - right;
400 return( Time( localtime( &diff_time ) ) );
401 }
402
403 Time& operator+=( Time& left, time_t right );
404
405 Time& operator-=( Time& left, time_t right );
406
407}
408
409#endif /* WOSS_TIME_DEFINITIONS_H */
Class for simulation time reference purposes.
Definition time-definitions.h:54
virtual double getTimeReference() const =0
virtual ~TimeReference()=0
virtual std::unique_ptr< TimeReference > clone() const =0
a class for time date manipulation
Definition time-definitions.h:83
friend bool operator!=(const Time &left, const Time &right)
Definition time-definitions.h:365
struct tm timeinfo
Definition time-definitions.h:317
Time & setMonth(int m)
Definition time-definitions.h:120
time_t raw_time
Definition time-definitions.h:322
~Time()=default
friend Time & operator-=(Time &left, time_t right)
Time & setHours(int h)
Definition time-definitions.h:142
int getMinutes() const
Definition time-definitions.h:198
int getHours() const
Definition time-definitions.h:186
Time & setDay(int d)
Definition time-definitions.h:128
friend std::ostream & operator<<(std::ostream &os, const Time &instance)
Definition time-definitions.h:388
Time()
Definition time-definitions.cpp:45
Time & operator=(const Time &copy)=default
int getSeconds() const
Definition time-definitions.h:204
Time & setMinutes(int m)
Definition time-definitions.h:149
friend const Time operator+(const Time &left, const time_t right)
Definition time-definitions.h:393
int getDay() const
Definition time-definitions.h:180
friend bool operator==(const Time &left, const Time &right)
Definition time-definitions.h:360
Time(Time &&tmp)=default
static bool debug
Definition time-definitions.h:327
Time & setYear(int y)
Definition time-definitions.h:135
int getYear() const
Definition time-definitions.h:192
static void setDebug(bool flag)
Definition time-definitions.h:162
friend bool operator<(const Time &left, const Time &right)
Definition time-definitions.h:375
Time & operator=(Time &&tmp)=default
Time & setSeconds(int s)
Definition time-definitions.h:156
Time(const Time &copy)=default
int getMonth() const
Definition time-definitions.h:174
friend Time & operator+=(Time &left, time_t right)
bool isValid() const
Definition time-definitions.h:168
friend const Time operator-(const Time &left, const time_t right)
Definition time-definitions.h:398
friend double operator-(const Time &left, const Time &right)
friend bool operator>=(const Time &left, const Time &right)
Definition time-definitions.h:384
friend bool operator<=(const Time &left, const Time &right)
Definition time-definitions.h:380
friend bool operator>(const Time &left, const Time &right)
Definition time-definitions.h:370
Definition ac-toolbox-arr-asc-reader.h:44
constexpr bool operator>(const Coord &left, const Coord &right)
Definition coordinates-definitions.h:1241
constexpr bool operator<(const Coord &left, const Coord &right)
Definition coordinates-definitions.h:1248
bool operator!=(const Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.h:833
constexpr bool operator<=(const Coord &left, const Coord &right)
Definition coordinates-definitions.h:1262
std::ostream & operator<<(std::ostream &os, const Altimetry &instance)
Definition altimetry-definitions.h:812
bool operator==(const Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.h:826
Altimetry & operator+=(Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.cpp:306
constexpr bool operator>=(const Coord &left, const Coord &right)
Definition coordinates-definitions.h:1255
const Altimetry operator+(const Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.cpp:236
Altimetry & operator-=(Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.cpp:314
const Altimetry operator-(const Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.cpp:243
Struct that stores start and end Time.
Definition time-definitions.h:336
friend std::ostream & operator<<(std::ostream &os, const SimTime &instance)
Definition time-definitions.h:340
Time start_time
Definition time-definitions.h:345
Time end_time
Definition time-definitions.h:347
SimTime(Time time1=Time(), Time time2=Time())
Definition time-definitions.h:338