C++ main module for gpm Package  1.0
CORE_Time.h
Go to the documentation of this file.
1 
2 
3 #ifndef CORE_TIME_H
4 #define CORE_TIME_H
5 
6 #include "CORE_Object.h"
7 #include "CORE_Pointers.h"
8 #include "CORE_ListPointers.h"
9 #include "types.h"
10 
59 
60 class CORE_Time : public CORE_Object {
61  SP_OBJECT(CORE_Time);
62 
63  // attributes
64  public:
67  static const tTime MAX_YEAR;
68  private:
69  static const tFlag YEAR;
70  static const tFlag MONTH;
71  static const tFlag WEEK;
72  static const tFlag DAY;
73  static const tFlag WEEK_DAY;
74  static const tFlag HOUR;
75  static const tFlag MINUTE;
76  static const tFlag SECUND;
77  static const tFlag MILLI_SECUNDS;
78 
79  // year of the date [1900,...[
80  int mYear;
81  // month of the date [0,11]
82  int mMonth;
83  // week of the day in year [1,53]
84  int mWeek;
85  // week of the day in week [0,6] 0:Sunday
86  int mWeekDay;
87  // day number [1,31]
88  int mDay;
89  // hours [0,23]
90  int mHours;
91  // minutes [0,59]
92  int mMinutes;
93  // seconds [0,59]
94  int mSeconds;
95  // milli seconds [0,999]
96  int mMilliSeconds;
97  // cpu time
98  long int mCPUTime;
99  // secunds since 1900
100  tTime mSecondsSince1900;
101 
102  public:
105  static const tString DAYS[];
108  static const tString JOURS[];
109  // month names
112  static const tString MONTHS[];
115  static const tString MOIS[];
116 
117 
118  // asociations
119  // methods
120 
121  protected:
122  // builders
125  CORE_Time();
126 
127 
130  virtual ~CORE_Time();
131 
132 
133  public:
136  inline static SP::CORE_Time New(){
137  SP::CORE_Time p(new CORE_Time(),CORE_Time::Delete());
138  return p;
139  };
142  inline static SP::CORE_Time New(const CORE_Time& time){
143  SP::CORE_Time p=New();
144  p->setDate(time);
145  return p;
146  };
149  inline static SP::CORE_Time New(const CORE_Time* time){
150  SP::CORE_Time p=New();
151  if (time!=null) p->setDate(*time);
152  return p;
153  };
156  inline static SP::CORE_Time New(const tTime& time){
157  SP::CORE_Time p=New();
158  p->setDate(time);
159  return p;
160  };
163  inline static SP::CORE_Time New(const SP::CORE_Time& time){
164  SP::CORE_Time p=New();
165  p->setDate(time.get());
166  return p;
167  };
168 
171  inline static SP::CORE_Time New(const int& year,
172  const int& month,
173  const int& day){
174  SP::CORE_Time t(new CORE_Time(),CORE_Time::Delete());
175  t->setDate(year,month,day,0,0,1,0);
176  return t;
177  };
178 
179 
183  SP::CORE_Time time=CORE_Time::New();
184  tTime secs=time->getSecondsSince1900();
185  return secs;
186  }
190  SP::CORE_Time time=CORE_Time::New();
191  tTime secs=time->getSecondsSince1900()*1000+time->getMilliSeconds();
192  return secs;
193  }
194 
197  static long int getTime(){
198  return getDateInMilliSeconds();
199  }
202  inline long int getCPUTime() const{
203  return mCPUTime;
204  };
205 
210  int getWeekYear(int& year) const{
211  year=mYear;
212  if ((mMonth==0) && (mWeek>25)) year--;
213  if ((mMonth==11) && (mWeek<25)) year++;
214  return mWeek;
215  };
216 
220  inline int getYear() const{
221  return mYear;
222  };
227  inline int getMonth() const{
228  return mMonth+1;
229  };
232  inline int getDaysNumberInMonth() const {
233  SP::CORE_Time t;
234  switch(mMonth) {
235  case 0:
236  case 2:
237  case 4:
238  case 6:
239  case 7:
240  case 9:
241  case 11:
242  return 31;
243  case 1:
244  t=CORE_Time::New();
245  t->setDate(mYear,mMonth+1,29,0,0,0,0);
246  if (t->getMonth()==2) return 29;
247  return 28;
248  case 3:
249  case 5:
250  case 8:
251  case 10:
252  return 30;
253  }
254  return 30;
255  }
260  inline int getDay() const{
261  return mDay;
262  };
266  static tString getWeekIntervalString(const int& year,const int& week);
270  return getWeekIntervalString(mYear,mWeek);
271  }
276  void setToFirstWeekDay(const int& year,const int& week);
277 
283  void setToWeekDay(const int& year,const int& week,const int& day);
284 
285 
290  inline int getWeekDay() const{
291  return mWeekDay;
292  };
297  inline int getMinutes() const{
298  return mMinutes;
299  };
304  inline int getSeconds() const{
305  return mSeconds;
306  };
311  inline int getHours() const{
312  return mHours;
313  };
316  inline int getMilliSeconds() const{
317  return mMilliSeconds;
318  };
321  inline tTime getSecondsSince1900() const {
322  return mSecondsSince1900;
323  };
324 
327  void setDate(const tTime& seconds);
328 
329 
335  void setDate(const int& year,const int& month,const int& day,
336  const int& hour,const int& minutes,const int& seconds,const int& ms);
342  inline void setDate(const int& year,const int& month,const int& day,
343  const int& hour,const int& minutes,const int& seconds) {
344  setDate(year,month,day,hour,minutes,seconds,0);
345  };
350  inline void setDate(int year,
351  int month,
352  int day){
353  setDate(year,month,day,0,0,0);
354  };
355 
358  void setDate(const tString& date);
361  inline void setDate(const CORE_Time& time) {
362  setDate(time.getYear(),
363  time.getMonth(),
364  time.getDay(),
365  time.getHours(),
366  time.getMinutes(),
367  time.getSeconds());
368 
369  };
372  inline void setDate(SP::CORE_Time time) {
373  setDate(time.get());
374  };
377  inline void setDate(const CORE_Time* time) {
378  if (time==null) return;
379  setDate(time->getYear(),
380  time->getMonth(),
381  time->getDay(),
382  time->getHours(),
383  time->getMinutes(),
384  time->getSeconds());
385 
386  };
387 
400  void setDate(const tString& date,const tString& format);
401 
404  void setHour(const tString& h);
407  void setTime(const int& hour,const int& minutes,const int& seconds);
410  void setTime(const tString& date);
411 
414  static tRelativeInteger subTimes(const CORE_Time& time1, const CORE_Time& time2);
417  static tRelativeInteger subTimes(const SP::CORE_Time& time1,
418  const SP::CORE_Time& time2);
421  tRelativeInteger sub(const CORE_Time& time2) const;
424  tRelativeInteger sub(const SP::CORE_Time& time2) const {
425  return sub(time2.get());
426  }
429  tRelativeInteger sub(const CORE_Time* time2) const {
430  if (time2!=null) return sub(*time2);
431  return getSecondsSince1900();
432  }
433 
436  static tRelativeInteger subTimesInMilliSeconds(const CORE_Time& time1, const CORE_Time& time2);
439  static tRelativeInteger subTimesInMilliSeconds(const SP::CORE_Time& time1,
440  const SP::CORE_Time& time2);
443  tRelativeInteger subInMilliSeconds(const CORE_Time& time2) const;
446  tRelativeInteger subInMilliSeconds(const SP::CORE_Time& time2) const {
447  return sub(time2.get());
448  }
452  if (time2!=null) return sub(*time2);
453  return getSecondsSince1900()*1000+mMilliSeconds;
454  }
455 
458  inline void setToNextDay() {
459  setDate(mSecondsSince1900+24*3600);
460  };
463  inline void setToPreviousDay() {
464  setDate(mSecondsSince1900-24*3600);
465  };
468  inline void setToPreviousDays(const int& nDays) {
469  setDate(mSecondsSince1900-nDays*24*3600);
470  };
473  inline void setToNextMonth() {
474  int mo=mMonth+1;
475  int y=mYear;
476  if (mo==12) {
477  mo=0;
478  y++;
479  }
480  setDate(y,mo+1,mDay,mHours,mMinutes,mSeconds,0);
481  };
484  inline void setToPreviousMonth() {
485  int mo=mMonth-1;
486  int y=mYear;
487  if (mo==-1) {
488  mo=11;
489  y--;
490  }
491  setDate(y,mo+1,mDay,mHours,mMinutes,mSeconds,0);
492  };
495  inline void setToNextWeek() {
496  setDate(mSecondsSince1900+7*24*3600);
497  };
500  inline void setToPreviousWeek() {
501  setDate(mSecondsSince1900-7*24*3600);
502  };
505  inline void setToLastDay(const int& year,const int& month) {
506  int mo=month+1;
507  int ye=year;
508  if (mo==13) {
509  mo=1;
510  ye++;
511  }
512  setDate(ye,mo,1,0,0,0,0);
513  setDate(mSecondsSince1900-23*3600);
514  };
515 
518  inline tBoolean isEqual(const CORE_Time* t) const {
519  if (t==null) return false;
520  return (mSecondsSince1900==t->getSecondsSince1900());
521  };
524  inline tBoolean isEqual(const CORE_Time& t) const {
525  return (mSecondsSince1900==t.getSecondsSince1900());
526  };
529  inline tBoolean isBefore(const CORE_Time& t) const {
530  return (mSecondsSince1900<t.getSecondsSince1900());
531  };
534  inline tBoolean isBefore(const CORE_Time* t) const {
535  if (t==null) return true;
536  return (mSecondsSince1900<t->getSecondsSince1900());
537  };
540  inline tBoolean isAfter(const CORE_Time& t) const {
541  return (mSecondsSince1900>t.getSecondsSince1900());
542  };
545  inline tBoolean isAfter(const CORE_Time* t) const {
546  if (t!=null)
547  return (mSecondsSince1900>t->getSecondsSince1900());
548  return false;
549  };
552  inline tBoolean isBeforeOrEqual(const CORE_Time& t) const {
553  return (mSecondsSince1900<=t.getSecondsSince1900());
554  };
557  inline tBoolean isAfterOrEqual(const CORE_Time& t) const {
558  return (mSecondsSince1900>=t.getSecondsSince1900());
559  };
562  inline tBoolean isEqual(const SP::CORE_Time& t) const {
563  return (mSecondsSince1900==t->getSecondsSince1900());
564  };
567  inline tBoolean isBefore(const SP::CORE_Time& t) const {
568  return (mSecondsSince1900<t->getSecondsSince1900());
569  };
572  inline tBoolean isAfter(const SP::CORE_Time& t) const {
573  return (mSecondsSince1900>t->getSecondsSince1900());
574  };
577  inline tBoolean isBeforeOrEqual(const SP::CORE_Time& t) const {
578  return (mSecondsSince1900<=t->getSecondsSince1900());
579  };
582  inline tBoolean isAfterOrEqual(const SP::CORE_Time& t) const {
583  return (mSecondsSince1900>=t->getSecondsSince1900());
584  };
585 
588  static tString getDuration(const long int& duration,
589  int& days,
590  int& hours,
591  int& minutes,
592  int& seconds,
593  int& ms);
596  inline static tString getDuration(const long int& duration) {
597 
598  int days;
599  int hours;
600  int minutes;
601  int seconds;
602  int ms;
603  return getDuration(duration,days,hours,minutes,seconds,ms);
604  }
605 
606 
609  virtual tString toString() const;
621  tString toString(const tString& format) const;
622 
623 
624  private:
625  static char *_fmt(const struct tm *t, char *pt, const char *ptlim);
626  static char *_conv(const int n, const char *format, char *pt, const char *ptlim);
627  static char *_add(const char *str, char *pt, const char *ptlim);
628 
629  static int getWeek(const struct tm *t);
630 
631 };
632 
633 #endif // end of ifndef
tBoolean isBeforeOrEqual(const SP::CORE_Time &t) const
return true if this <= t
Definition: CORE_Time.h:577
static const tTime MAX_YEAR
MAX number of years.
Definition: CORE_Time.h:67
tTime getSecondsSince1900() const
return the number of seconds since 1900
Definition: CORE_Time.h:321
static SP::CORE_Time New()
creates a time object
Definition: CORE_Time.h:136
void setDate(const tTime &seconds)
set the date from 1900 in seconds
Definition: CORE_Time.cpp:202
int getYear() const
return the year
Definition: CORE_Time.h:220
void setToNextWeek()
set to next week
Definition: CORE_Time.h:495
void setToPreviousWeek()
set to previoust week
Definition: CORE_Time.h:500
void setDate(const CORE_Time *time)
set the date from a string year-month-day
Definition: CORE_Time.h:377
this class describes a time class
Definition: CORE_Time.h:60
tBoolean isAfter(const SP::CORE_Time &t) const
return true if this > t
Definition: CORE_Time.h:572
static tTime getDateInMilliSeconds()
return the time in 1/60 seconds
Definition: CORE_Time.h:189
int getMinutes() const
return the minutes of the time
Definition: CORE_Time.h:297
void setToPreviousMonth()
set to previous month
Definition: CORE_Time.h:484
long int getCPUTime() const
return the CPU time in 1/60 seconds
Definition: CORE_Time.h:202
tString getWeekIntervalString()
get the week interval monday –> Sunday of the current time
Definition: CORE_Time.h:269
#define tRelativeInteger
Definition: types.h:33
static tString getDuration(const long int &duration, int &days, int &hours, int &minutes, int &seconds, int &ms)
get duration
Definition: CORE_Time.cpp:574
void setToWeekDay(const int &year, const int &week, const int &day)
set the date to the week day week must be in [1..53] day in [0,6] 0:MONDAY....6:SUNDAY the first day ...
Definition: CORE_Time.cpp:451
#define tBoolean
Definition: types.h:35
tBoolean isAfterOrEqual(const CORE_Time &t) const
return true if this >= t
Definition: CORE_Time.h:557
tBoolean isBefore(const CORE_Time *t) const
return true if this < t
Definition: CORE_Time.h:534
static SP::CORE_Time New(const SP::CORE_Time &time)
creates a time object
Definition: CORE_Time.h:163
#define tTime
Definition: types.h:41
#define null
Definition: types.h:13
tBoolean isEqual(const CORE_Time *t) const
return true if the 2 dates are equals
Definition: CORE_Time.h:518
static const tString JOURS[]
days names in french
Definition: CORE_Time.h:108
static SP::CORE_Time New(const tTime &time)
creates a time object
Definition: CORE_Time.h:156
int getDaysNumberInMonth() const
return the number of days in month
Definition: CORE_Time.h:232
int getSeconds() const
return the seconds of the time
Definition: CORE_Time.h:304
void setDate(const CORE_Time &time)
set the date from a string year-month-day
Definition: CORE_Time.h:361
static long int getTime()
return the time in 1/60 seconds
Definition: CORE_Time.h:197
tRelativeInteger subInMilliSeconds(const CORE_Time *time2) const
get the difference with time2 this -time2 in secunds
Definition: CORE_Time.h:451
CORE_Time()
create a general time object
Definition: CORE_Time.cpp:89
void setToLastDay(const int &year, const int &month)
set to last day of month
Definition: CORE_Time.h:505
tBoolean isAfter(const CORE_Time &t) const
return true if this > t
Definition: CORE_Time.h:540
void setToPreviousDays(const int &nDays)
set to days ago
Definition: CORE_Time.h:468
int getMilliSeconds() const
return the milliseconds seconds of the time
Definition: CORE_Time.h:316
tBoolean isAfter(const CORE_Time *t) const
return true if this > t
Definition: CORE_Time.h:545
void setToNextDay()
set to next day
Definition: CORE_Time.h:458
void setTime(const int &hour, const int &minutes, const int &seconds)
set the time
Definition: CORE_Time.cpp:216
void setHour(const tString &h)
set hour xxhyy
Definition: CORE_Time.cpp:485
static tRelativeInteger subTimesInMilliSeconds(const CORE_Time &time1, const CORE_Time &time2)
get the difference betwen two dates in milli-secund time1-time2
Definition: CORE_Time.cpp:244
void setToNextMonth()
set to next month
Definition: CORE_Time.h:473
static const tString MOIS[]
month names in french
Definition: CORE_Time.h:115
static tRelativeInteger subTimes(const CORE_Time &time1, const CORE_Time &time2)
get the difference betwen two dates in secund time1-time2
Definition: CORE_Time.cpp:241
static tTime getDateInSeconds()
return the time in 1/60 seconds
Definition: CORE_Time.h:182
DEFINE_SPTR(CORE_Time)
int getWeekYear(int &year) const
get the week of the year
Definition: CORE_Time.h:210
void setDate(int year, int month, int day)
set the date year>=0 month in [1,12]
Definition: CORE_Time.h:350
tRelativeInteger sub(const CORE_Time *time2) const
get the difference with time2 this -time2 in secunds
Definition: CORE_Time.h:429
abstract base class for most classes.
Definition: CORE_Object.h:30
static const tString DAYS[]
days names in english
Definition: CORE_Time.h:105
static SP::CORE_Time New(const CORE_Time *time)
creates a time object
Definition: CORE_Time.h:149
int getWeekDay() const
return the day of the week 0:Sunday [0..6]
Definition: CORE_Time.h:290
#define tString
Definition: types.h:36
tRelativeInteger subInMilliSeconds(const CORE_Time &time2) const
get the difference with time2 this -time2 in secunds
Definition: CORE_Time.cpp:261
static tString getDuration(const long int &duration)
get duration
Definition: CORE_Time.h:596
tBoolean isBeforeOrEqual(const CORE_Time &t) const
return true if this <= t
Definition: CORE_Time.h:552
tBoolean isEqual(const CORE_Time &t) const
return true if the 2 dates are equals
Definition: CORE_Time.h:524
static SP::CORE_Time New(const CORE_Time &time)
creates a time object
Definition: CORE_Time.h:142
void setToPreviousDay()
set to previous day
Definition: CORE_Time.h:463
static SP::CORE_Time New(const int &year, const int &month, const int &day)
creates a time object year > 1900 month [1,12] day [1,31]
Definition: CORE_Time.h:171
void setToFirstWeekDay(const int &year, const int &week)
set the date to the first week day week must be in [1..53] the first day is a monday ...
Definition: CORE_Time.cpp:424
tBoolean isBefore(const SP::CORE_Time &t) const
return true if this < t
Definition: CORE_Time.h:567
void setDate(SP::CORE_Time time)
set the date from a string year-month-day
Definition: CORE_Time.h:372
tRelativeInteger sub(const SP::CORE_Time &time2) const
get the difference with time2 this -time2 in secunds
Definition: CORE_Time.h:424
virtual tString toString() const
return the time into a string
Definition: CORE_Time.cpp:320
virtual ~CORE_Time()
remove
Definition: CORE_Time.cpp:148
tRelativeInteger subInMilliSeconds(const SP::CORE_Time &time2) const
get the difference with time2 this -time2 in secunds
Definition: CORE_Time.h:446
int getMonth() const
return the month
Definition: CORE_Time.h:227
tBoolean isAfterOrEqual(const SP::CORE_Time &t) const
return true if this >= t
Definition: CORE_Time.h:582
int getDay() const
return the day
Definition: CORE_Time.h:260
int getHours() const
return the hours of the time
Definition: CORE_Time.h:311
tRelativeInteger sub(const CORE_Time &time2) const
get the difference with time2 this -time2 in secunds
Definition: CORE_Time.cpp:253
DEFINE_SVPTR(CORE_Time)
static const tString MONTHS[]
month names in english
Definition: CORE_Time.h:112
tBoolean isBefore(const CORE_Time &t) const
return true if this < t
Definition: CORE_Time.h:529
void setDate(const int &year, const int &month, const int &day, const int &hour, const int &minutes, const int &seconds)
set the date year>=0 month in [1,12] day in [1,31]
Definition: CORE_Time.h:342
tBoolean isEqual(const SP::CORE_Time &t) const
return true if the 2 dates are equals
Definition: CORE_Time.h:562
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:106
#define tFlag
Definition: types.h:14