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 Federico Guerra
4 * and regents of the SIGNET lab, University of Padova
5 *
6 * Author: Federico Guerra - federico@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/*
23 * This software has been developed by Federico Guerra and SIGNET lab,
24 * University of Padova, in collaboration with the NATO Centre for
25 * Maritime Research and Experimentation (http://www.cmre.nato.int ;
26 * E-mail: pao@cmre.nato.int), whose support is gratefully acknowledged.
27 */
28
29
40#ifndef WOSS_TIME_DEFINITIONS_H
41#define WOSS_TIME_DEFINITIONS_H
42
43
44#include <iostream>
45#include <ctime>
46#include <cassert>
47#include <cmath>
48#include <climits>
49
50
51namespace woss {
52
53
54 #define TIME_NOT_SET_VALUE (LONG_MIN)
55
56
64
65
66 public:
67
68
69 virtual ~TimeReference() = 0;
70
75 virtual TimeReference* clone() = 0;
76
81 virtual double getTimeReference() const = 0;
82
83
84 };
85
86 inline TimeReference::~TimeReference() { }
87
95 class Time {
96
97
98 public:
99
100
104 Time();
105
110 Time( struct tm* time );
111
121 Time( int day, int month, int year, int hours = 0, int mins = 0, int seconds = 1 );
122
127 Time( const Time& copy );
128
133 ~Time() { }
134
135
141 Time& setMonth( int m ) { assert( m >= 1 && m <= 12 ); timeinfo.tm_mon = m - 1; raw_time = mktime(&timeinfo);
142 return *this; }
143
149 Time& setDay( int d ) { assert( d >= 1 && d <= 31 ); timeinfo.tm_mday = d; raw_time = mktime(&timeinfo); return *this; }
150
156 Time& setYear( int y ) { assert( y >= 1900 ); timeinfo.tm_year = y - 1900; raw_time = mktime(&timeinfo); return *this; }
157
163 Time& setHours( int h ) { assert( h >= 0 && h <= 23 ); timeinfo.tm_hour = h; raw_time = mktime(&timeinfo); return *this; }
164
170 Time& setMinutes( int m ) { assert( m >= 0 && m <= 59 ); timeinfo.tm_min = m; raw_time = mktime(&timeinfo); return *this; }
171
177 Time& setSeconds( int s ) { assert( s >= 0 && s <= 59 ); timeinfo.tm_sec = s; raw_time = mktime(&timeinfo); return *this; }
178
179
184 static void setDebug(bool flag) { debug = flag; }
185
186
191 bool isValid() const { return ( raw_time != TIME_NOT_SET_VALUE ); }
192
193
198 int getMonth() const { return timeinfo.tm_mon; }
199
204 int getDay() const { return timeinfo.tm_mday; }
205
210 int getHours() const { return timeinfo.tm_hour; }
211
216 int getYear() const { return timeinfo.tm_year; }
217
222 int getMinutes() const { return timeinfo.tm_min; }
223
228 int getSeconds() const { return timeinfo.tm_sec; }
229
230
235 operator time_t() const;
236
242 Time& operator=( const Time& copy );
243
244
251 friend const Time operator+( const Time& left, const time_t right );
252
259 friend const Time operator-( const Time& left, const time_t right );
260
267 friend double operator-( const Time& left, const Time& right );
268
269
276 friend Time& operator+=( Time& left, time_t right );
277
284 friend Time& operator-=( Time& left, time_t right );
285
286
293 friend bool operator==( const Time& left, const Time& right );
294
301 friend bool operator!=( const Time& left, const Time& right );
302
303
310 friend bool operator>( const Time& left, const Time& right );
311
318 friend bool operator<( const Time& left, const Time& right );
319
326 friend bool operator<=( const Time& left, const Time& right );
327
334 friend bool operator>=( const Time& left, const Time& right );
335
336
343 friend ::std::ostream& operator<<( ::std::ostream& os, const Time& instance );
344
345
346 protected:
347
348
352 mutable struct tm timeinfo;
353
357 time_t raw_time;
358
359
363 static bool debug;
364
365 };
366
367
373 struct SimTime {
374
375
376 SimTime( Time time1 = Time(), Time time2 = Time() ) : start_time(time1), end_time(time2) { }
377
378
379 friend std::ostream& operator<<( std::ostream& os, const SimTime& instance ) {
380 os << "start time = " << instance.start_time << "; end time = " << instance.end_time;
381 return os;
382 }
383
384
385 Time start_time;
386
387 Time end_time;
388
389
390 };
391
392
393 // non-inline operator declarations
394 double operator-( const Time& left, const Time& right );
395
396
397 //inline functions
399 inline Time::operator time_t() const {
400 return( mktime(&timeinfo) );
401 }
402
403
404 inline bool operator==( const Time& left, const Time& right ) {
405 if ( &left == &right ) return true;
406 return( mktime(const_cast<tm*>(&left.timeinfo)) == mktime(const_cast<tm*>(&right.timeinfo)) );
407 }
408
409
410 inline bool operator!=( const Time& left, const Time& right ) {
411 if ( &left == &right ) return false;
412 return( mktime(const_cast<tm*>(&left.timeinfo)) != mktime(const_cast<tm*>(&right.timeinfo)) );
413 }
414
415
416 inline bool operator>( const Time& left, const Time& right ) {
417 if ( &left == &right ) return false;
418 return( mktime(const_cast<tm*>(&left.timeinfo)) > mktime(const_cast<tm*>(&right.timeinfo)) );
419 }
420
421
422 inline bool operator<( const Time& left, const Time& right ) {
423 if ( &left == &right ) return false;
424 return( mktime(const_cast<tm*>(&left.timeinfo)) < mktime(const_cast<tm*>(&right.timeinfo)) );
425 }
426
427
428 inline bool operator<=( const Time& left, const Time& right ) {
429 return( mktime(const_cast<tm*>(&left.timeinfo)) <= mktime(const_cast<tm*>(&right.timeinfo)) );
430 }
431
432
433 inline bool operator>=( const Time& left, const Time& right ) {
434 return( mktime(const_cast<tm*>(&left.timeinfo)) >= mktime(const_cast<tm*>(&right.timeinfo)) );
435 }
436
437
438 inline std::ostream& operator<<( std::ostream& os, const Time& time ) {
439 os << asctime(const_cast<tm*>(&time.timeinfo)) ;
440 return os;
441 }
442
443
444 inline const Time operator+( const Time& left, const time_t right ) {
445 time_t sum_time = mktime(const_cast<tm*>(&left.timeinfo)) + right;
446 return( Time( localtime( &sum_time ) ) );
447 }
448
449
450 inline const Time operator-( const Time& left, const time_t right ) {
451 time_t diff_time = mktime(const_cast<tm*>(&left.timeinfo)) - right;
452 return( Time( localtime( &diff_time ) ) );
453 }
454
455
456 Time& operator+=( Time& left, time_t right );
457
458
459 Time& operator-=( Time& left, time_t right );
460
461}
462
463
464#endif /* WOSS_TIME_DEFINITIONS_H */
465
466
bool operator!=(const Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.h:846
std::ostream & operator<<(std::ostream &os, const Altimetry &instance)
Definition altimetry-definitions.h:826
bool operator==(const Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.h:840
Altimetry & operator+=(Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.cpp:327
const Altimetry operator+(const Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.cpp:257
Altimetry & operator-=(Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.cpp:335
const Altimetry operator-(const Altimetry &left, const Altimetry &right)
Definition altimetry-definitions.cpp:264
Class for simulation time reference purposes.
Definition time-definitions.h:63
virtual double getTimeReference() const =0
virtual TimeReference * clone()=0
a class for time date manipulation
Definition time-definitions.h:95
friend bool operator!=(const Time &left, const Time &right)
Definition time-definitions.h:410
struct tm timeinfo
Definition time-definitions.h:352
Time & setMonth(int m)
Definition time-definitions.h:141
time_t raw_time
Definition time-definitions.h:357
friend Time & operator-=(Time &left, time_t right)
Time & setHours(int h)
Definition time-definitions.h:163
int getMinutes() const
Definition time-definitions.h:222
int getHours() const
Definition time-definitions.h:210
Time & setDay(int d)
Definition time-definitions.h:149
Time()
Definition time-definitions.cpp:52
Time & operator=(const Time &copy)
Definition time-definitions.cpp:108
friend::std::ostream & operator<<(::std::ostream &os, const Time &instance)
int getSeconds() const
Definition time-definitions.h:228
~Time()
Definition time-definitions.h:133
Time & setMinutes(int m)
Definition time-definitions.h:170
friend const Time operator+(const Time &left, const time_t right)
Definition time-definitions.h:444
int getDay() const
Definition time-definitions.h:204
friend bool operator==(const Time &left, const Time &right)
Definition time-definitions.h:404
static bool debug
Definition time-definitions.h:363
Time & setYear(int y)
Definition time-definitions.h:156
int getYear() const
Definition time-definitions.h:216
static void setDebug(bool flag)
Definition time-definitions.h:184
friend bool operator<(const Time &left, const Time &right)
Definition time-definitions.h:422
Time & setSeconds(int s)
Definition time-definitions.h:177
int getMonth() const
Definition time-definitions.h:198
friend Time & operator+=(Time &left, time_t right)
bool isValid() const
Definition time-definitions.h:191
friend const Time operator-(const Time &left, const time_t right)
Definition time-definitions.h:450
friend double operator-(const Time &left, const Time &right)
friend bool operator>=(const Time &left, const Time &right)
Definition time-definitions.h:433
friend bool operator<=(const Time &left, const Time &right)
Definition time-definitions.h:428
friend bool operator>(const Time &left, const Time &right)
Definition time-definitions.h:416
bool operator<=(const Coord &left, const Coord &right)
Definition coordinates-definitions.h:928
bool operator>=(const Coord &left, const Coord &right)
Definition coordinates-definitions.h:922
bool operator<(const Coord &left, const Coord &right)
Definition coordinates-definitions.h:916
bool operator>(const Coord &left, const Coord &right)
Definition coordinates-definitions.h:910
Struct that stores start and end Time.
Definition time-definitions.h:373