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 00032 // pre-declaration of utUnit pointer 00033 struct utUnit; 00034 00035 namespace MetNoFimex 00036 { 00037 00044 class FimexTime { 00045 public: 00047 unsigned short msecond; 00049 char second; 00051 char minute; 00053 char hour; 00055 char mday; 00057 char month; 00059 unsigned short year; 00061 bool operator==(const FimexTime &rhs) const; 00063 bool operator!=(const FimexTime &rhs) const { return !(*this == rhs); } 00065 bool operator>(const FimexTime &rhs) const { return (toLong() > rhs.toLong()); } 00067 bool operator<(const FimexTime &rhs) const { return (rhs > *this); } 00069 bool operator>=(const FimexTime &rhs) const { return !(rhs > *this); } 00071 bool operator<=(const FimexTime &rhs) const { return !(*this > rhs); } 00072 00073 private: 00075 long long toLong() const { return year*10000000000000LL + month*100000000000LL + mday*1000000000LL + hour*10000000LL + minute*100000 + second*1000 + msecond; } 00076 }; 00077 00078 00079 std::ostream& operator<< (std::ostream& out, const FimexTime& fTime); 00080 FimexTime string2FimexTime(const std::string& str) throw(CDMException); 00081 00092 class TimeUnit 00093 { 00094 Units units; // unit initialization 00095 boost::shared_ptr<utUnit> pUnit; // pointer to unit implementation 00096 double epochOffset; 00097 double epochSlope; 00098 public: 00100 TimeUnit() throw(CDMException); 00101 TimeUnit(const std::string& timeUnitString) throw(CDMException); 00102 virtual ~TimeUnit(); 00104 double unitTime2epochSeconds(double unitTime) const; 00106 double epochSeconds2unitTime(double epochSeconds) const; 00108 FimexTime unitTime2fimexTime(double unitTime) const throw(CDMException); 00110 double fimexTime2unitTime(const FimexTime& fiTime) const throw(CDMException); 00112 double fimexTime2unitTimeX(FimexTime fiTime) const throw(CDMException) { return fimexTime2unitTime(fiTime); } 00113 private: 00114 void init(const std::string& timeUnitString = "seconds since 1970-01-01 00:00:00") throw(CDMException); 00115 }; 00116 00117 } 00118 00119 #endif /* TIMEUNIT_H_ */