# MI - Fimex: include/fimex/TimeUnit.h Source File

- Main Page
- Related Pages
- Namespaces
- Classes
- Files
- Examples

# include/fimex/TimeUnit.h

Go to the documentation of this file.

    00001 /*
    00002  * Fimex
    00003  *
    00004  * (C) Copyright 2008, met.no
    00005  *
    00006  * Project Info:  https://wiki.met.no/fimex/start
    00007  *
    00008  * This library is free software; you can redistribute it and/or modify it
    00009  * under the terms of the GNU Lesser General Public License as published by
    00010  * the Free Software Foundation; either version 2.1 of the License, or
    00011  * (at your option) any later version.
    00012  *
    00013  * This library is distributed in the hope that it will be useful, but
    00014  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    00015  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    00016  * License for more details.
    00017  *
    00018  * You should have received a copy of the GNU Lesser General Public
    00019  * License along with this library; if not, write to the Free Software
    00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    00021  * USA.
    00022  */
    00023
    00024 #ifndef TIMEUNIT_H_
    00025 #define TIMEUNIT_H_
    00026
    00027 #include "boost/shared_ptr.hpp"
    00028 #include "fimex/Units.h"
    00029 #include "fimex/CDMException.h"
    00030 #include <iostream>
    00031 #include <boost/date_time/posix_time/posix_time_types.hpp>
    00032
    00033 // pre-declaration of utUnit pointer
    00034 struct utUnit;
    00035
    00036 namespace MetNoFimex
    00037 {
    00038
    00045 class FimexTime {
    00046 private:
    00048     unsigned short year;
    00050     char month;
    00052     char mday;
    00054     char hour;
    00056     char minute;
    00058     char second;
    00060     unsigned short msecond;
    00061
    00062 public:
    00063     enum special_values {
    00064         min_date_time,
    00065         max_date_time
    00066     };
    00067     FimexTime() {};
    00068     FimexTime(unsigned short year, char month, char mday, char hour = 0, char minute = 0, char second = 0, unsigned short msecond = 0);
    00069     FimexTime(special_values val);
    00076     bool parseISO8601(const std::string& isoString);
    00078     void setTime(unsigned short year, char month, char mday, char hour = 0, char minute = 0, char second = 0, unsigned short msecond = 0);
    00080     unsigned short getYear() const {return year;}
    00081     void setYear(unsigned short year) {this->year = year;}
    00083     char getMonth() const {return month;}
    00084     void setMonth(char month) {this->month = month;}
    00086     char getMDay() const {return mday;}
    00087     void setMDay(char mday) {this->mday = mday;}
    00089     char getHour() const {return hour;}
    00090     void setHour(char hour) {this->hour = hour;}
    00092     char getMinute() const {return minute;}
    00093     void setMinute(char minute) {this->minute = minute;}
    00095     char getSecond() const {return second;}
    00096     void setSecond(char second) {this->second = second;}
    00098     unsigned short getMSecond() const {return msecond;}
    00099     void setMSecond(unsigned short msecond) {this->msecond = msecond;}
    00101         bool operator==(const FimexTime &rhs) const;
    00103         bool operator!=(const FimexTime &rhs) const { return !(*this == rhs); }
    00105         bool operator>(const FimexTime &rhs) const { return (toLong() > rhs.toLong()); }
    00107         bool operator<(const FimexTime &rhs) const { return (rhs > *this); }
    00109         bool operator>=(const FimexTime &rhs) const { return !(rhs > *this); }
    00111         bool operator<=(const FimexTime &rhs) const { return !(*this > rhs); }
    00112
    00113 private:
    00115     long long toLong() const { return year*10000000000000LL + month*100000000000LL + mday*1000000000LL + hour*10000000LL + minute*100000 + second*1000 + msecond; }
    00116 };
    00117
    00119
    00120
    00121
    00122 std::ostream& operator<< (std::ostream& out, const FimexTime& fTime);
    00123 FimexTime string2FimexTime(const std::string& str) throw(CDMException);
    00124
    00135 class TimeUnit
    00136 {
    00137         Units units; // unit initialization
    00138         boost::shared_ptr<utUnit> pUnit; // pointer to unit implementation
    00139         double epochOffset;
    00140         double epochSlope;
    00141 public:
    00143         TimeUnit() throw(CDMException);
    00144         TimeUnit(const std::string& timeUnitString) throw(CDMException);
    00145         virtual ~TimeUnit();
    00147         double unitTime2epochSeconds(double unitTime) const;
    00149         boost::posix_time::ptime unitTime2posixTime(double unitTime) const;
    00151         double epochSeconds2unitTime(double epochSeconds) const;
    00153         FimexTime unitTime2fimexTime(double unitTime) const throw(CDMException);
    00155         double fimexTime2unitTime(const FimexTime& fiTime) const throw(CDMException);
    00157         double fimexTime2unitTimeX(FimexTime fiTime) const throw(CDMException) { return fimexTime2unitTime(fiTime); }
    00159         double posixTime2unitTime(boost::posix_time::ptime poTime) const throw(CDMException);
    00160 private:
    00161         void init(const std::string& timeUnitString = "seconds since 1970-01-01 00:00:00") throw(CDMException);
    00162 };
    00163
    00164 }
    00165
    00166 #endif /* TIMEUNIT_H_ */