Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

mdatetime.h

Go to the documentation of this file.
00001 
00002 #ifndef __MAGIC_DATETIME_H__
00003 #define __MAGIC_DATETIME_H__
00004 
00005 #include "magic/mobject.h"
00006 
00007 BEGIN_NAMESPACE (MagiC);
00008 
00009 typedef double FloatType;
00010 typedef FloatType TimeHours;
00011 
00015 class Hours {
00016     double  mHours;
00017   public:
00018                         Hours           () : mHours (0.0) {}
00019                         Hours           (double hours) : mHours (hours) {}
00020                         Hours           (int hours, int mins=0, double secs=0.0)
00021                                 : mHours (((hours<0 || mins<0 || secs<0)?-1:1)*(abs(hours)+mins/60.0+secs/3600)) {}
00022     //                      Hours           (const Seconds& secs);
00023     //                      Hours           (const Radians& rads) : mHours (rads.toDouble()*(12/M_PI)) {}
00024     inline double       toDouble        () const {return mHours;}
00025     inline              operator double () const {return mHours;}
00026     inline void         operator +=     (double x) {mHours += x;}
00027     inline void         operator -=     (double x) {mHours -= x;}
00028     inline Hours        operator -      () const {return -mHours;}
00029     inline Hours        operator -      (const Hours& o) const {return mHours-o.mHours;}
00030     inline Hours        operator +      (const Hours& o) const {return mHours+o.mHours;}
00031     String              toString        () const;
00032     int                 hour            () const {return int(mHours);}
00033     int                 min             () const {return int((mHours-int(mHours))*60);}
00034     double              sec             () const {return ((mHours-hour())*60-min())*60;}
00035 };
00036 inline Hours operator * (double x, const Hours& y) {return Hours(x*y.toDouble());}
00037 
00038 // Ensures that the given value is in the given range
00039 FloatType torange (FloatType initial, FloatType rmin, FloatType rmax);
00040 
00041 // Date and time conversions
00042 
00043 class DateTime;
00044 class TimePeriod;
00045 class JulianDay;
00046 
00048 //                                                                           //
00049 //                 ___                   ----- o                             //
00050 //                 |  \   ___   |   ___    |            ___                  //
00051 //                 |   |  ___| -+- /   )   |   | |/|/| /   )                 //
00052 //                 |   | (   |  |  |---    |   | | | | |---                  //
00053 //                 |__/   \__|   \  \__    |   | | | |  \__                  //
00054 //                                                                           //
00056 
00057 typedef DateTime Date;
00058 typedef DateTime Time;
00059 
00060 // This object represents any time or date value
00061 class DateTime : public Object {
00062     decl_dynamic (DateTime);
00063   public:
00064     int year, month, day;   // Päivämäärä
00065     Hours hours;            // Aika tunteina ja sen osina
00066     int timezone;           // Mikä timezone
00067     int utcdiff;            // timezone+dst
00068     int dst;                // Onko kesäaika
00069 
00070                     DateTime    () {make ();}
00071                     DateTime    (const time_t& aika) {make (aika);}
00072                     DateTime    (const struct tm* aika);
00073                     DateTime    (const JulianDay& jd) {make(jd);}
00074                     DateTime    (const DateTime& o) {operator=(o);}
00075                     DateTime    (int d, int m, int y, int h=0, int m=0, FloatType s=0.0);
00076     void            make        ();
00077     void            make        (const time_t& aika);
00078     void            make        (const JulianDay& jd);
00079     void            make        (int d, int m, int y, int h=0, int m=0, FloatType s=0.0);
00080     DateTime&       makecurrent ();
00081 
00082     // Mutatorit
00083     
00084     DateTime&       operator=   (const JulianDay& jd) {make(jd); return *this;}
00085     DateTime&       operator=   (const DateTime& o);
00086 
00087     // Asettaa ajan muotoilun mukaan.<BR>
00088     // Y=vuosi, M=kuukausi, D=päivä, h=tunti, m=minuutti, s=sekuntti.<BR>
00089     // Palauttaa virhetilaneessa !=0
00090     // Esim: "YYYYMMDDhhmmss".<BR>
00091     // Huom! Formaatti saa nykyisellään sisältää vain näitä merkkejä.
00092     int             setstrict   (const String& str, const String& format);
00093 
00094     void            setZone     (int zone) {timezone=zone; utcdiff=zone;}
00095 
00096     // Informatiifiset funktiot
00097     
00098     int             operator<   (const DateTime& o) const {return compare(o)==-1;}
00099     int             operator==  (const DateTime& o) const {return compare(o)==0;}
00100     int             operator>   (const DateTime& o) const {return compare(o)==1;}
00101     int             compare     (const DateTime& o) const;
00102     
00103     double          operator-   (const DateTime& o) const;
00104     int             weekday     () const;
00105     String          weekdayname (int language=0) const;
00106     String          monthname   (int language=0) const;
00107     int             daynumber   () const;
00108     Hours           dectime     () const {return hours/24.0;}
00109     int             hour        () const {return int(hours);}
00110     int             min         () const {return int((double(hours)-int(double(hours)))*60);}
00111     FloatType       sec         () const {return (hours*60-int(hours*60))*60;}
00112 
00113     // Time conversions
00114     DateTime        lt2GMT      () const;
00115     DateTime        GMT2lt      (int timezone) const;
00116 
00117     // Tells if the time period contains this time
00118     int             isduring    (const TimePeriod& period) const;
00119 
00120     // Muunnos c-perusmuotoon. Muista deletoida.
00121     struct tm*      to_tm       () const;
00122     
00123     // Printing functions
00124     
00125     String          text        () const;
00126     String          text_time   () const;
00127     String          text_time   (const String& timeformat) const;
00128 
00129     // Iterator functions
00130     
00131     void            nextday     ();
00132     void            prevday     ();
00133 
00134     void            corrange    ();
00135   private:
00136 };
00137 
00138 
00139 
00141 //                                                                          //
00142 //             ----- o             ----            o          |             //
00143 //               |            ___  |   )  ___                 |             //
00144 //               |   | |/|/| /   ) |---  /   ) |/\ |  __   ---|             //
00145 //               |   | | | | |---  |     |---  |   | /  \ (   |             //
00146 //               |   | | | |  \__  |      \__  |   | \__/  ---|             //
00147 //                                                                          //
00149 
00150 // Stores a time interval
00151 class TimePeriod : public Object {
00152   public:
00153     DateTime    start, end;
00154 
00155                 TimePeriod  () {;}
00156                 TimePeriod  (const DateTime& s, const DateTime& e) {start=s; end=e;}
00157 };
00158 
00159 
00160 
00162 //                                                                           //
00163 //                  --       | o             ___                             //
00164 //                   |       |    ___    _   |  \   ___                      //
00165 //                   | |   | | |  ___| |/ \  |   |  ___| \   |               //
00166 //               |   | |   | | | (   | |   | |   | (   |  \  |               //
00167 //                \_/   \__! | |  \__| |   | |__/   \__|   \_/               //
00168 //                                                        \_/                //
00170 
00171 class JulianDay : public Object {
00172     decl_dynamic (JulianDay);
00173     FloatType   mJD;
00174   public:
00175                     JulianDay           () {mJD=0.0;}
00176                     JulianDay           (const DateTime& dt) {operator=(dt);}
00177                     JulianDay           (const FloatType& fl) {mJD = fl;}
00178     void            make                (const DateTime& dt);
00179     JulianDay&      operator=           (const DateTime& dt) {make (dt); return *this;}
00180                     operator FloatType  () const {return mJD;}
00181 };
00182 
00183 END_NAMESPACE;
00184 
00185 #endif
00186 
00187 
00188 
00189 

Generated on Thu Feb 10 20:06:41 2005 for LibMagiC by doxygen1.2.18