MI - Fimex
TimeUnit.h
Go to the documentation of this file.
1 /*
2  * Fimex
3  *
4  * (C) Copyright 2008, met.no
5  *
6  * Project Info: https://wiki.met.no/fimex/start
7  *
8  * This library is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  * License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21  * USA.
22  */
23 
24 #ifndef TIMEUNIT_H_
25 #define TIMEUNIT_H_
26 
27 #include "boost/shared_ptr.hpp"
28 #include "fimex/Units.h"
29 #include "fimex/CDMException.h"
30 #include <iostream>
31 #include <boost/date_time/posix_time/posix_time_types.hpp>
32 
33 namespace MetNoFimex
34 {
35 
45 class FimexTime {
46 private:
48  unsigned short year;
50  char month;
52  char mday;
54  char hour;
56  char minute;
58  char second;
60  unsigned short msecond;
61 
62 public:
66  };
67  FimexTime() {};
68  FimexTime(unsigned short year, char month, char mday, char hour = 0, char minute = 0, char second = 0, unsigned short msecond = 0);
76  bool parseISO8601(const std::string& isoString);
78  void setTime(unsigned short year, char month, char mday, char hour = 0, char minute = 0, char second = 0, unsigned short msecond = 0);
80  unsigned short getYear() const {return year;}
81  void setYear(unsigned short year) {this->year = year;}
83  char getMonth() const {return month;}
84  void setMonth(char month) {this->month = month;}
86  char getMDay() const {return mday;}
87  void setMDay(char mday) {this->mday = mday;}
89  char getHour() const {return hour;}
90  void setHour(char hour) {this->hour = hour;}
92  char getMinute() const {return minute;}
93  void setMinute(char minute) {this->minute = minute;}
95  char getSecond() const {return second;}
96  void setSecond(char second) {this->second = second;}
98  unsigned short getMSecond() const {return msecond;}
99  void setMSecond(unsigned short msecond) {this->msecond = msecond;}
100 
101  boost::posix_time::ptime asPosixTime() const;
102 
104  bool operator==(const FimexTime &rhs) const;
106  bool operator!=(const FimexTime &rhs) const { return !(*this == rhs); }
108  bool operator>(const FimexTime &rhs) const { return (toLong() > rhs.toLong()); }
110  bool operator<(const FimexTime &rhs) const { return (rhs > *this); }
112  bool operator>=(const FimexTime &rhs) const { return !(rhs > *this); }
114  bool operator<=(const FimexTime &rhs) const { return !(*this > rhs); }
115 
116 private:
118  long long toLong() const { return year*10000000000000LL + month*100000000000LL + mday*1000000000LL + hour*10000000LL + minute*100000 + second*1000 + msecond; }
119 };
120 
122 
123 
124 
125 std::ostream& operator<< (std::ostream& out, const FimexTime& fTime);
127 
138 class TimeUnit
139 {
140  Units units; // unit initialization
141  boost::shared_ptr<void> pUnit; // pointer to unit implementation
142  double epochOffset;
143  double epochSlope;
144 public:
146  TimeUnit() throw(CDMException);
147  TimeUnit(const std::string& timeUnitString) throw(CDMException);
148  virtual ~TimeUnit();
150  double unitTime2epochSeconds(double unitTime) const;
152  boost::posix_time::ptime unitTime2posixTime(double unitTime) const;
154  double epochSeconds2unitTime(double epochSeconds) const;
156  FimexTime unitTime2fimexTime(double unitTime) const throw(CDMException);
158  double fimexTime2unitTime(const FimexTime& fiTime) const throw(CDMException);
160  double fimexTime2unitTimeX(FimexTime fiTime) const throw(CDMException) { return fimexTime2unitTime(fiTime); }
162  double posixTime2unitTime(boost::posix_time::ptime poTime) const throw(CDMException);
163 private:
164  void init(const std::string& timeUnitString = "seconds since 1970-01-01 00:00:00") throw(CDMException);
165 };
166 
167 }
168 
169 #endif /* TIMEUNIT_H_ */
Definition: TimeUnit.h:64
basic_string< char > string
char getSecond() const
second (0-59)
Definition: TimeUnit.h:95
char getHour() const
hour (0-23)
Definition: TimeUnit.h:89
bool operator==(const FimexTime &rhs) const
compare two fimexTimes
char getMonth() const
month (1-12)
Definition: TimeUnit.h:83
void setYear(unsigned short year)
Definition: TimeUnit.h:81
basic_ostream< char > ostream
Definition: CDMException.h:36
void setTime(unsigned short year, char month, char mday, char hour=0, char minute=0, char second=0, unsigned short msecond=0)
set all the time-parameters at once
void setMDay(char mday)
Definition: TimeUnit.h:87
void setMonth(char month)
Definition: TimeUnit.h:84
Definition: Units.h:53
Definition: C_CDMReader.h:35
bool operator>=(const FimexTime &rhs) const
compare two fimexTimes
Definition: TimeUnit.h:112
void setSecond(char second)
Definition: TimeUnit.h:96
Definition: TimeUnit.h:138
unsigned short getMSecond() const
millisecond
Definition: TimeUnit.h:98
void setHour(char hour)
Definition: TimeUnit.h:90
boost::posix_time::ptime asPosixTime() const
std::ostream & operator<<(std::ostream &out, CoordinateAxis ca)
bool operator<=(const FimexTime &rhs) const
compare two fimexTimes
Definition: TimeUnit.h:114
FimexTime()
Definition: TimeUnit.h:67
void setMSecond(unsigned short msecond)
Definition: TimeUnit.h:99
char getMDay() const
day of month (1-31)
Definition: TimeUnit.h:86
void setMinute(char minute)
Definition: TimeUnit.h:93
unsigned short getYear() const
year (2008 as of writing)
Definition: TimeUnit.h:80
bool operator<(const FimexTime &rhs) const
compare two fimexTimes
Definition: TimeUnit.h:110
double fimexTime2unitTimeX(FimexTime fiTime) const
same as fimexTime2unitTime but copying fiTime instead of referencing, needed for i.e. bind1st(mem_fun())
Definition: TimeUnit.h:160
bool operator>(const FimexTime &rhs) const
compare two fimexTimes
Definition: TimeUnit.h:108
Definition: TimeUnit.h:65
bool parseISO8601(const std::string &isoString)
char getMinute() const
minute (0-59)
Definition: TimeUnit.h:92
FimexTime string2FimexTime(const std::string &str)
bool operator!=(const FimexTime &rhs) const
compare two fimexTimes
Definition: TimeUnit.h:106
Definition: TimeUnit.h:45
special_values
Definition: TimeUnit.h:63