00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef DATE_H
00027 #define DATE_H
00028
00029 #define DATE_TEST 0
00030
00031 #include <iostream>
00032 #include <string>
00033 #include <assert.h>
00034 #include <cmath>
00035 #include <ctime>
00036 #include <clocale>
00037 #include <exception>
00038
00039 #include "tools.h"
00040
00041 using namespace std;
00042
00044 static const int MAX_FORMATTED_STRING_SIZE=128;
00045
00049 class bad_date : public exception {
00050 public:
00051 bad_date() {}
00052 ;
00053 ~bad_date() throw() {
00054 ;
00055 };
00056 virtual const char* what() const throw() {
00057 string m="bad_date : Sorry, the date you tried to entered is invalid";
00058 return m.c_str();
00059 };
00060 };
00064 class bad_month : public exception {
00065 int month;
00066 public:
00067 bad_month(const int month) : month(month){};
00068 ~bad_month() throw() {};
00069 virtual const char* what() const throw() {
00070 string m="bad_month : it must be in range [1,12], and you entered "+MyTools::to_string(month);
00071 return m.c_str();
00072 };
00073 };
00074
00075 class parse_date_error : public std::exception {
00076 public:
00077 parse_date_error(const string &date, const string &format):date(date),format(format) {};
00078 ~parse_date_error() throw() {}
00079 ;
00080 virtual const char* what() const throw() {
00081 std::ostringstream oss;
00082 oss<<"parse_date_error : can't parse the date "<<date<<" using the format "<<format<<std::endl;
00083 return oss.str().c_str();
00084 };
00085 private:
00086 string date;
00087 string format;
00088 };
00089
00093 class Epoch2Leap {
00094 double epoch_time;
00095 int leap_sec;
00096 public:
00102 Epoch2Leap ( const double epoch_time = 0., const int leap_sec = 0 ) : epoch_time(epoch_time), leap_sec (leap_sec){};
00103
00107 ~Epoch2Leap () {};
00108
00113 double get_epoch_time() const {
00114 return epoch_time;
00115 }
00120 int get_leap_sec() const {
00121 return leap_sec;
00122 }
00126 void print () const {
00127 cout << epoch_time << " -> " << leap_sec << endl;
00128 }
00129 };
00130
00138 class Date {
00139 time_t epoch_time;
00140
00141
00142
00143
00144 static vector < Epoch2Leap > utc_2_tai_leap_sec;
00145 public:
00146
00147 static const int nsec_tai93_to_utc_start = 27;
00157 Date(int year = 1970, int month = 1, int day = 1, int hour = 0, int min = 0, int sec = 0);
00162 Date(const time_t& tm);
00167 Date( const double & tm );
00171 ~Date() {}
00172 ;
00176 static void init_leap_sec_table ();
00182 static double get_tai93_leap_sec ( const Date &d );
00188 static double get_tai_leap_sec ( const Date &d );
00193 Date &operator= (const Date &d);
00198 Date operator+ (const double &time);
00203 bool operator== (const Date &d) const;
00208 bool operator!= (const Date &d) const;
00213 bool operator<= (const Date &d) const;
00218 bool operator>= (const Date &d) const;
00223 bool operator< (const Date &d) const;
00228 bool operator> (const Date &d) const;
00233 bool is_epoch_start( ) const {return epoch_time==0.;};
00238 const int get_sec() const;
00243 const int get_min() const;
00248 const int get_hour() const;
00253 const int get_day() const;
00258 const int get_month() const;
00263 const int get_year() const;
00268 const int get_yday() const;
00275 static const vector<Date> get_days(const Date &start, const Date &end);
00282 static const int get_nb_day(const int &month,const int &year);
00290 static int get_day_number(const int &year,const int &month, int &day);
00299 static void get_day_range(const int &year,const int &month, int &first, int &last);
00304 static const string get_month_string( const int _month );
00308 const bool is_the_same_day(const Date &d);
00313 void set_julian_time(const double &julian_time);
00318 const double get_julian_time();
00323 const double get_TAI93_time() const ;
00328 void set_TAI93_time(const double &TAI_time);
00333 const double get_ECMWF_time() const ;
00338 void set_ECMWF_time(const double &time);
00339
00349 void set_date(const int &_year = 1970, const int& _month = 1, const int& _day = 1, const int& _hour = 0, const int& _min = 0, const int& _sec = 0);
00355 void set_date_str(const string &s = "", const char * _format = "");
00360 const string get_date_str(const char * _format = "");
00365 void set_epoch_time(const time_t& _epoch_time);
00369 time_t get_epoch_time() const {
00370 return epoch_time;
00371 }
00375 void print() const;
00376 string to_string() const;
00377
00384 static time_t get_epoch_time ( const char* s_date, const char * format );
00385
00386 friend std::ostream &operator<< (std::ostream &stream, const Date &date);
00387 };
00388
00389 #endif