• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

/home/pascal/depot/filedata/src/date.h

00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Nicolas PASCAL                                  *
00003  *   nicolas.pascal@icare.univ-lille1.fr                                   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 /**************************************************************************
00022 *  History :                                                              *
00023 *  3/08/05 : start                                                        *
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     /* Table of the leap seconds between TAI and UTC from the 1st of January 1972 at 00:00:00
00141      each line is :
00142      ( <start of range, included>, <end of range, excluded>, TAI - UTC leap seconds  )
00143      source : ftp://hpiers.obspm.fr/iers/bul/bulc/UTC-TAI.history */
00144     static vector < Epoch2Leap > utc_2_tai_leap_sec;
00145 public:
00146     // constant number of leap seconds between the TAI93 start ( 1st of January 1993 at 00:00:00 ) and the UTC start ()
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

Generated on Thu Feb 14 2013 17:59:03 for filedata.kdevelop by  doxygen 1.7.1