C++ main module for mmsd 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 #include <ctime>
11 
60 
61 class CORE_Time : public CORE_Object {
62  SP_OBJECT(CORE_Time);
63 
64  // attributes
65 public:
68  static const tTime MAX_YEAR;
69 private:
70  static const tFlag YEAR;
71  static const tFlag MONTH;
72  static const tFlag WEEK;
73  static const tFlag DAY;
74  static const tFlag WEEK_DAY;
75  static const tFlag HOUR;
76  static const tFlag MINUTE;
77  static const tFlag SECUND;
78  static const tFlag MILLI_SECUNDS;
79 
80  // year of the date [1900,...[
81  int mYear;
82  // month of the date [0,11]
83  int mMonth;
84  // week of the day in year [1,53]
85  int mWeek;
86  // week of the day in week [0,6] 0:Sunday
87  int mWeekDay;
88  // day number [1,31]
89  int mDay;
90  // hours [0,23]
91  int mHours;
92  // minutes [0,59]
93  int mMinutes;
94  // seconds [0,59]
95  int mSeconds;
96  // milli seconds [0,999]
97  unsigned long long int mMilliSeconds;
98  // cpu time
99  unsigned long long int mCPUTime;
100  // secunds since 1900
101  tTime mSecondsSince1900;
102  std::clock_t mStartClock;
103  tBoolean mIsChronoStarting;
104 
105 public:
108  static const tString DAYS[];
111  static const tString JOURS[];
112  // month names
115  static const tString MONTHS[];
118  static const tString MOIS[];
119 
120 
121  // asociations
122  // methods
123 
124 public:
125  // builders
128  CORE_Time();
129 
130 
133  virtual ~CORE_Time();
134 
135 
136 public:
139  inline static SP::CORE_Time New(){
140  SP::CORE_Time p(new CORE_Time(),CORE_Time::Delete());
141  return p;
142  };
145  inline static SP::CORE_Time New(const CORE_Time& time){
146  SP::CORE_Time p=New();
147  p->setDate(time);
148  return p;
149  };
152  inline static SP::CORE_Time New(const CORE_Time* time){
153  SP::CORE_Time p=New();
154  if (time!=null) p->setDate(*time);
155  return p;
156  };
159  inline static SP::CORE_Time New(const tTime& time){
160  SP::CORE_Time p=New();
161  p->setDate(time);
162  return p;
163  };
166  inline static SP::CORE_Time New(const SP::CORE_Time& time){
167  SP::CORE_Time p=New();
168  p->setDate(time.get());
169  return p;
170  };
171 
174  inline static SP::CORE_Time New(const int& year,
175  const int& month,
176  const int& day){
177  SP::CORE_Time t(new CORE_Time(),CORE_Time::Delete());
178  t->setDate(year,month,day,0,0,1,0);
179  return t;
180  };
181 
182 
186  SP::CORE_Time time=CORE_Time::New();
187  tTime secs=time->getSecondsSince1900();
188  return secs;
189  }
193  SP::CORE_Time time=CORE_Time::New();
194  tTime secs=time->getSecondsSince1900()*1000+time->getMilliSeconds();
195  return secs;
196  }
197 
200  static long int getTime(){
201  return getDateInMilliSeconds();
202  }
205  inline unsigned long long int getCPUTime() const{
206  return mCPUTime;
207  };
208 
209 
214  int getWeekYear(int& year) const{
215  year=mYear;
216  if ((mMonth==0) && (mWeek>25)) year--;
217  if ((mMonth==11) && (mWeek<25)) year++;
218  return mWeek;
219  };
220 
224  inline int getYear() const{
225  return mYear;
226  };
231  inline int getMonth() const{
232  return mMonth+1;
233  };
236  inline int getDaysNumberInMonth() const {
237  SP::CORE_Time t;
238  switch(mMonth) {
239  case 0:
240  case 2:
241  case 4:
242  case 6:
243  case 7:
244  case 9:
245  case 11:
246  return 31;
247  case 1:
248  t=CORE_Time::New();
249  t->setDate(mYear,mMonth+1,29,0,0,0,0);
250  if (t->getMonth()==2) return 29;
251  return 28;
252  case 3:
253  case 5:
254  case 8:
255  case 10:
256  return 30;
257  }
258  return 30;
259  }
264  inline int getDay() const{
265  return mDay;
266  };
270  static tString getWeekIntervalString(const int& year,const int& week);
274  return getWeekIntervalString(mYear,mWeek);
275  }
280  void setToFirstWeekDay(const int& year,const int& week);
281 
287  void setToWeekDay(const int& year,const int& week,const int& day);
288 
289 
294  inline int getWeekDay() const{
295  return mWeekDay;
296  };
301  inline int getMinutes() const{
302  return mMinutes;
303  };
308  inline int getSeconds() const{
309  return mSeconds;
310  };
315  inline int getHours() const{
316  return mHours;
317  };
320  inline unsigned long long int getMilliSeconds() const{
321  return mMilliSeconds;
322  };
325  inline tTime getSecondsSince1900() const {
326  return mSecondsSince1900;
327  };
328 
331  void setDate(const tTime& seconds);
332 
333 
339  void setDate(const int& year,const int& month,const int& day,
340  const int& hour,const int& minutes,const int& seconds,const int& ms);
346  inline void setDate(const int& year,const int& month,const int& day,
347  const int& hour,const int& minutes,const int& seconds) {
348  setDate(year,month,day,hour,minutes,seconds,0);
349  };
354  inline void setDate(int year,
355  int month,
356  int day){
357  setDate(year,month,day,0,0,0);
358  };
359 
362  void setDate(const tString& date);
365  inline void setDate(const CORE_Time& time) {
366  setDate(time.getYear(),
367  time.getMonth(),
368  time.getDay(),
369  time.getHours(),
370  time.getMinutes(),
371  time.getSeconds());
372 
373  };
376  inline void setDate(SP::CORE_Time time) {
377  setDate(time.get());
378  };
381  inline void setDate(const CORE_Time* time) {
382  if (time==null) return;
383  setDate(time->getYear(),
384  time->getMonth(),
385  time->getDay(),
386  time->getHours(),
387  time->getMinutes(),
388  time->getSeconds());
389 
390  };
391 
404  void setDate(const tString& date,const tString& format);
405 
408  void setHour(const tString& h);
411  void setTime(const int& hour,const int& minutes,const int& seconds);
414  void setTime(const tString& date);
415 
418  static tRelativeInteger subTimes(const CORE_Time& time1, const CORE_Time& time2);
421  static tRelativeInteger subTimes(const SP::CORE_Time& time1,
422  const SP::CORE_Time& time2);
425  tRelativeInteger sub(const CORE_Time& time2) const;
428  tRelativeInteger sub(const SP::CORE_Time& time2) const {
429  return sub(time2.get());
430  }
433  tRelativeInteger sub(const CORE_Time* time2) const {
434  if (time2!=null) return sub(*time2);
435  return getSecondsSince1900();
436  }
437 
440  static tRelativeInteger subTimesInMilliSeconds(const CORE_Time& time1, const CORE_Time& time2);
443  static tRelativeInteger subTimesInMilliSeconds(const SP::CORE_Time& time1,
444  const SP::CORE_Time& time2);
447  tRelativeInteger subInMilliSeconds(const CORE_Time& time2) const;
450  tRelativeInteger subInMilliSeconds(const SP::CORE_Time& time2) const {
451  return sub(time2.get());
452  }
456  if (time2!=null) return sub(*time2);
457  return getSecondsSince1900()*1000+mMilliSeconds;
458  }
459 
462  inline void setToNextDay() {
463  setDate(mSecondsSince1900+24*3600);
464  };
467  inline void setToPreviousDay() {
468  setDate(mSecondsSince1900-24*3600);
469  };
472  inline void setToPreviousDays(const int& nDays) {
473  setDate(mSecondsSince1900-nDays*24*3600);
474  };
477  inline void setToNextMonth() {
478  int mo=mMonth+1;
479  int y=mYear;
480  if (mo==12) {
481  mo=0;
482  y++;
483  }
484  setDate(y,mo+1,mDay,mHours,mMinutes,mSeconds,0);
485  };
488  inline void setToPreviousMonth() {
489  int mo=mMonth-1;
490  int y=mYear;
491  if (mo==-1) {
492  mo=11;
493  y--;
494  }
495  setDate(y,mo+1,mDay,mHours,mMinutes,mSeconds,0);
496  };
499  inline void setToNextWeek() {
500  setDate(mSecondsSince1900+7*24*3600);
501  };
504  inline void setToPreviousWeek() {
505  setDate(mSecondsSince1900-7*24*3600);
506  };
509  inline void setToLastDay(const int& year,const int& month) {
510  int mo=month+1;
511  int ye=year;
512  if (mo==13) {
513  mo=1;
514  ye++;
515  }
516  setDate(ye,mo,1,0,0,0,0);
517  setDate(mSecondsSince1900-23*3600);
518  };
519 
522  inline void startChrono() {
523  mIsChronoStarting=true;
524  mStartClock=std::clock();
525  }
528  inline unsigned long long int continueChrono() {
529  if (!mIsChronoStarting) throw CORE_Exception("common/core/",
530  "CORE_Time::stop()",
531  "can not continue a chrono without starting it");
532  std::clock_t t=std::clock();
533  unsigned long long int dt=(unsigned long long int) (1000.0*(t-mStartClock)/CLOCKS_PER_SEC);
534  mStartClock=t;
535  return dt;
536  }
537 
540  inline unsigned long long int stopChrono() {
541  if (!mIsChronoStarting) throw CORE_Exception("common/core/",
542  "CORE_Time::stop()",
543  "can not stop a chrono without starting it");
544  mIsChronoStarting=false;
545  return (unsigned long long int) (1000.0*(std::clock()-mStartClock)/CLOCKS_PER_SEC);
546  }
547 
550  inline tBoolean isEqual(const CORE_Time* t) const {
551  if (t==null) return false;
552  return (mSecondsSince1900==t->getSecondsSince1900());
553  };
556  inline tBoolean isEqual(const CORE_Time& t) const {
557  return (mSecondsSince1900==t.getSecondsSince1900());
558  };
561  inline tBoolean isBefore(const CORE_Time& t) const {
562  return (mSecondsSince1900<t.getSecondsSince1900());
563  };
566  inline tBoolean isBefore(const CORE_Time* t) const {
567  if (t==null) return true;
568  return (mSecondsSince1900<t->getSecondsSince1900());
569  };
572  inline tBoolean isAfter(const CORE_Time& t) const {
573  return (mSecondsSince1900>t.getSecondsSince1900());
574  };
577  inline tBoolean isAfter(const CORE_Time* t) const {
578  if (t!=null)
579  return (mSecondsSince1900>t->getSecondsSince1900());
580  return false;
581  };
584  inline tBoolean isBeforeOrEqual(const CORE_Time& t) const {
585  return (mSecondsSince1900<=t.getSecondsSince1900());
586  };
589  inline tBoolean isAfterOrEqual(const CORE_Time& t) const {
590  return (mSecondsSince1900>=t.getSecondsSince1900());
591  };
594  inline tBoolean isEqual(const SP::CORE_Time& t) const {
595  return (mSecondsSince1900==t->getSecondsSince1900());
596  };
599  inline tBoolean isBefore(const SP::CORE_Time& t) const {
600  return (mSecondsSince1900<t->getSecondsSince1900());
601  };
604  inline tBoolean isAfter(const SP::CORE_Time& t) const {
605  return (mSecondsSince1900>t->getSecondsSince1900());
606  };
609  inline tBoolean isBeforeOrEqual(const SP::CORE_Time& t) const {
610  return (mSecondsSince1900<=t->getSecondsSince1900());
611  };
614  inline tBoolean isAfterOrEqual(const SP::CORE_Time& t) const {
615  return (mSecondsSince1900>=t->getSecondsSince1900());
616  };
617 
620  static tString getDuration(const long int& duration,
621  int& days,
622  int& hours,
623  int& minutes,
624  int& seconds,
625  int& ms);
628  inline static tString getDuration(const long int& duration) {
629 
630  int days;
631  int hours;
632  int minutes;
633  int seconds;
634  int ms;
635  return getDuration(duration,days,hours,minutes,seconds,ms);
636  }
637 
638 
641  virtual tString toString() const;
653  tString toString(const tString& format) const;
654 
655 
656 private:
657  static char *_fmt(const struct tm *t, char *pt, const char *ptlim);
658  static char *_conv(const int n, const char *format, char *pt, const char *ptlim);
659  static char *_add(const char *str, char *pt, const char *ptlim);
660 
661  static int getWeek(const struct tm *t);
662 
663 };
664 
665 #endif // end of ifndef
tBoolean isBeforeOrEqual(const SP::CORE_Time &t) const
return true if this <= t
Definition: CORE_Time.h:609
unsigned long long int getMilliSeconds() const
return the milliseconds seconds of the time
Definition: CORE_Time.h:320
static const tTime MAX_YEAR
MAX number of years.
Definition: CORE_Time.h:68
tTime getSecondsSince1900() const
return the number of seconds since 1900
Definition: CORE_Time.h:325
static SP::CORE_Time New()
creates a time object
Definition: CORE_Time.h:139
void setDate(const tTime &seconds)
set the date from 1900 in seconds
Definition: CORE_Time.cpp:205
int getYear() const
return the year
Definition: CORE_Time.h:224
unsigned long long int getCPUTime() const
return the CPU time in 1/60 seconds
Definition: CORE_Time.h:205
void setToNextWeek()
set to next week
Definition: CORE_Time.h:499
unsigned long long int stopChrono()
end the chrono and return the time in ms since start time
Definition: CORE_Time.h:540
void setToPreviousWeek()
set to previoust week
Definition: CORE_Time.h:504
void setDate(const CORE_Time *time)
set the date from a string year-month-day
Definition: CORE_Time.h:381
this class describes a time class
Definition: CORE_Time.h:61
tBoolean isAfter(const SP::CORE_Time &t) const
return true if this > t
Definition: CORE_Time.h:604
static tTime getDateInMilliSeconds()
return the time in 1/60 seconds
Definition: CORE_Time.h:192
int getMinutes() const
return the minutes of the time
Definition: CORE_Time.h:301
void setToPreviousMonth()
set to previous month
Definition: CORE_Time.h:488
tString getWeekIntervalString()
get the week interval monday –> Sunday of the current time
Definition: CORE_Time.h:273
void startChrono()
start the chrono
Definition: CORE_Time.h:522
#define tRelativeInteger
Definition: types.h:46
static tString getDuration(const long int &duration, int &days, int &hours, int &minutes, int &seconds, int &ms)
get duration
Definition: CORE_Time.cpp:577
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:454
#define tBoolean
Definition: types.h:48
unsigned long long int continueChrono()
contune the chrono and return the time in ms since start time or last continue step ...
Definition: CORE_Time.h:528
tBoolean isAfterOrEqual(const CORE_Time &t) const
return true if this >= t
Definition: CORE_Time.h:589
tBoolean isBefore(const CORE_Time *t) const
return true if this < t
Definition: CORE_Time.h:566
static SP::CORE_Time New(const SP::CORE_Time &time)
creates a time object
Definition: CORE_Time.h:166
#define tTime
Definition: types.h:54
#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:550
static const tString JOURS[]
days names in french
Definition: CORE_Time.h:111
static SP::CORE_Time New(const tTime &time)
creates a time object
Definition: CORE_Time.h:159
int getDaysNumberInMonth() const
return the number of days in month
Definition: CORE_Time.h:236
int getSeconds() const
return the seconds of the time
Definition: CORE_Time.h:308
void setDate(const CORE_Time &time)
set the date from a string year-month-day
Definition: CORE_Time.h:365
static long int getTime()
return the time in 1/60 seconds
Definition: CORE_Time.h:200
tRelativeInteger subInMilliSeconds(const CORE_Time *time2) const
get the difference with time2 this -time2 in secunds
Definition: CORE_Time.h:455
CORE_Time()
create a general time object
Definition: CORE_Time.cpp:90
void setToLastDay(const int &year, const int &month)
set to last day of month
Definition: CORE_Time.h:509
tBoolean isAfter(const CORE_Time &t) const
return true if this > t
Definition: CORE_Time.h:572
void setToPreviousDays(const int &nDays)
set to days ago
Definition: CORE_Time.h:472
this class describes the exceptions raised for CORE package
Definition: CORE_Exception.h:15
tBoolean isAfter(const CORE_Time *t) const
return true if this > t
Definition: CORE_Time.h:577
void setToNextDay()
set to next day
Definition: CORE_Time.h:462
void setTime(const int &hour, const int &minutes, const int &seconds)
set the time
Definition: CORE_Time.cpp:219
void setHour(const tString &h)
set hour xxhyy
Definition: CORE_Time.cpp:488
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:247
void setToNextMonth()
set to next month
Definition: CORE_Time.h:477
static const tString MOIS[]
month names in french
Definition: CORE_Time.h:118
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:244
static tTime getDateInSeconds()
return the time in 1/60 seconds
Definition: CORE_Time.h:185
DEFINE_SPTR(CORE_Time)
int getWeekYear(int &year) const
get the week of the year
Definition: CORE_Time.h:214
void setDate(int year, int month, int day)
set the date year>=0 month in [1,12]
Definition: CORE_Time.h:354
tRelativeInteger sub(const CORE_Time *time2) const
get the difference with time2 this -time2 in secunds
Definition: CORE_Time.h:433
abstract base class for most classes.
Definition: CORE_Object.h:30
static const tString DAYS[]
days names in english
Definition: CORE_Time.h:108
static SP::CORE_Time New(const CORE_Time *time)
creates a time object
Definition: CORE_Time.h:152
int getWeekDay() const
return the day of the week 0:Sunday [0..6]
Definition: CORE_Time.h:294
#define tString
Definition: types.h:49
tRelativeInteger subInMilliSeconds(const CORE_Time &time2) const
get the difference with time2 this -time2 in secunds
Definition: CORE_Time.cpp:264
static tString getDuration(const long int &duration)
get duration
Definition: CORE_Time.h:628
tBoolean isBeforeOrEqual(const CORE_Time &t) const
return true if this <= t
Definition: CORE_Time.h:584
tBoolean isEqual(const CORE_Time &t) const
return true if the 2 dates are equals
Definition: CORE_Time.h:556
static SP::CORE_Time New(const CORE_Time &time)
creates a time object
Definition: CORE_Time.h:145
void setToPreviousDay()
set to previous day
Definition: CORE_Time.h:467
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:174
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:427
tBoolean isBefore(const SP::CORE_Time &t) const
return true if this < t
Definition: CORE_Time.h:599
void setDate(SP::CORE_Time time)
set the date from a string year-month-day
Definition: CORE_Time.h:376
tRelativeInteger sub(const SP::CORE_Time &time2) const
get the difference with time2 this -time2 in secunds
Definition: CORE_Time.h:428
virtual tString toString() const
return the time into a string
Definition: CORE_Time.cpp:323
virtual ~CORE_Time()
remove
Definition: CORE_Time.cpp:151
tRelativeInteger subInMilliSeconds(const SP::CORE_Time &time2) const
get the difference with time2 this -time2 in secunds
Definition: CORE_Time.h:450
int getMonth() const
return the month
Definition: CORE_Time.h:231
tBoolean isAfterOrEqual(const SP::CORE_Time &t) const
return true if this >= t
Definition: CORE_Time.h:614
int getDay() const
return the day
Definition: CORE_Time.h:264
int getHours() const
return the hours of the time
Definition: CORE_Time.h:315
tRelativeInteger sub(const CORE_Time &time2) const
get the difference with time2 this -time2 in secunds
Definition: CORE_Time.cpp:256
DEFINE_SVPTR(CORE_Time)
static const tString MONTHS[]
month names in english
Definition: CORE_Time.h:115
tBoolean isBefore(const CORE_Time &t) const
return true if this < t
Definition: CORE_Time.h:561
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:346
tBoolean isEqual(const SP::CORE_Time &t) const
return true if the 2 dates are equals
Definition: CORE_Time.h:594
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:106
#define tFlag
Definition: types.h:14