Main Page   Class Hierarchy   Compound List   File List   Compound Members  

mdatetime.h

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

Generated at Tue Dec 4 19:53:25 2001 for MagiC++ by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001