CMSDK  2.0.1
PsyTime.h
1 #if !defined(_PSYTIME_H_)
2 #define _PSYTIME_H_
3 
4 //#include "MemoryManager.h"
5 #include "Types.h"
6 #include "sys/timeb.h"
7 
8 namespace cmlabs {
9 
15 #define USEC_YEAR_0_TO_1970 62167305600000000L
16 #define SECS_PER_YEAR 31536000
17 #define SECS_PER_MONTH 2629744
18 #define SECS_PER_WEEK 604800
19 #define SECS_PER_DAY 86400
20 #define SECS_PER_HOUR 3600
21 #define SECS_PER_MIN 60
22 #define TIME_YEAR_1970 62167305600000000L
23 
24 #define SECOND_MS 1000
25 #define MINUTE_MS SECS_PER_MIN * 1000
26 #define HOUR_MS SECS_PER_HOUR * 1000
27 #define DAY_MS SECS_PER_DAY * 1000
28 #define WEEK_MS SECS_PER_WEEK * 1000
29 #define MONTH_MS (uint64)SECS_PER_MONTH * 1000
30 #define YEAR_MS (uint64)SECS_PER_YEAR * 1000
31 
32 #define SECOND_US (uint64)1000000
33 #define MINUTE_US (uint64)SECS_PER_MIN * 1000000
34 #define HOUR_US (uint64)SECS_PER_HOUR * 1000000
35 #define DAY_US (uint64)SECS_PER_DAY * 1000000
36 #define WEEK_US (uint64)SECS_PER_WEEK * 1000000
37 #define MONTH_US (uint64)SECS_PER_MONTH * 1000000
38 #define YEAR_US (uint64)SECS_PER_YEAR * 1000000
39 
40 #define PSYSECOND SECOND_US
41 #define PSYMINUTE MINUTE_US
42 #define PSYHOUR HOUR_US
43 #define PSYDAY DAY_US
44 #define PSYWEEK WEEK_US
45 #define PSYMONTH MONTH_US
46 #define PSYYEAR YEAR_US
47 
48 static char PsyDays[][5] = {"None", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
49 static char PsyDaysFull[][10] = {"None", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
50 static char PsyMonths[][5] = {"None", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
51 static char PsyMonthsFull[][10] = {"None", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
52 
54  uint32 usec; /* microseconds after the minute - [0,999999] */
55  uint32 msec; /* milliseconds after the minute - [0,999] */
56  uint16 sec; /* seconds after the minute - [0,59] */
57  uint16 min; /* minutes after the hour - [0,59] */
58  uint16 hour; /* hours since midnight - [0,23] */
59  uint16 day; /* day of the month - [1,31] */
60  uint16 mon; /* months since January - [1,12] */
61  uint16 year; /* years since 0000 */
62  uint16 wday; /* day of the week - [1,7] */
63  uint16 yday; /* day of the year - [1,366] */
64  bool negative; /* is the time value negative */
65  bool dst; /* daylight savings in effect */
66  int32 dstsec; /* daylight savings in minutes when in effect */
67  int32 tzsec; /* timezone in sec */
68  char zonename[64]; /* the name of the current time zone */
69 };
70 
71 // Return current time according to the TMC
72 uint64 GetTimeNow();
73 
74 // Calculate the current Time Mapping Constant
75 uint64 SyncToHardwareClock();
76 bool SetCurrentNetSyncDif(int64 netTimeDif);
77 bool SetCurrentTimeSyncData(uint64 tmc, int64 netTimeAdjust);
78 bool GetCurrentTimeSyncData(uint64& tmc, int64& netTimeAdjust);
79 //bool SetLocalSyncAdjustment(int64 adjust);
80 //bool ChangeLocalSyncAdjustment(int64 adjust);
81 
82 #define TMC_MASTER 0
83 #define TMC_SLAVE 1
84 #define TMC_SYNC_INTERVAL 60000000 // 1 minute
85 
86 static uint64 CurrentTMC = 0;
87 static uint8 CurrentTMCMode = TMC_MASTER;
88 static uint64 LastTMCSync = 0;
89 static int64 NetTimeAdjust = 0;
90 
91 // Estimate the next TMC wrap-around
92 uint64 EstNextTMCWrap();
93 
94 // Return age of a timestamp compared to NOW
95 int64 GetTimeAge(uint64 t);
96 int32 GetTimeAgeMS(uint64 t);
97 uint64 GetTimeFromString(const char* str);
98 uint32 GetHTTPTime(uint64 time, char* buffer, uint32 size);
99 uint64 GetTimeFromPsyDateAndTime(struct PsyDateAndTime &tad);
100 
101 uint32 GetTimeOffsetGMT();
102 
103 struct PsyDateAndTime GetDateAndTime(uint64 t, bool local = true);
104 struct PsyDateAndTime GetDateAndTimeUTC(uint64 t);
105 
106 struct PsyDateAndTime GetTimeDifference(uint64 t1, uint64 t2);
107 struct PsyDateAndTime GetTimeDifference(int64 dif);
108 
109 char* PrintTime(uint64 t, bool local = true, bool us = true, bool ms = true);
110 char* PrintTimeOnly(uint64 t, bool local = true, bool us = true, bool ms = true);
111 char* PrintTimeDif(uint64 t, bool us = true, bool ms = true);
112 char* PrintDate(uint64 t, bool local = true);
113 char* PrintDateSortable(uint64 t, bool local = true);
114 char* PrintDateSortableDelimiter(uint64 t, const char* del, bool local = true);
115 
116 char* PrintTimeSortable(uint64 t, bool local);
117 char* PrintTimeSortableMillisec(uint64 t, bool local);
118 char* PrintTimeSortableMicrosec(uint64 t, bool local);
119 
120 std::string PrintTimeNowString(bool local = true, bool us = true, bool ms = true);
121 std::string PrintTimeString(uint64 t, bool local = true, bool us = true, bool ms = true);
122 std::string PrintTimeOnlyString(uint64 t, bool local = true, bool us = true, bool ms = true);
123 std::string PrintTimeDifString(uint64 t, bool us = true, bool ms = true);
124 std::string PrintDateString(uint64 t, bool local = true);
125 std::string PrintDateStringSortable(uint64 t, bool local = true);
126 std::string PrintDateStringSortableDelimiter(uint64 t, const char* del, bool local = true);
127 
128 std::string PrintTimeSortableString(uint64 t, bool local = true);
129 std::string PrintTimeSortableMillisecString(uint64 t, bool local = true);
130 std::string PrintTimeSortableMicrosecString(uint64 t, bool local = true);
131 
132 uint64 FTime2PsyTime(uint64 t);
133 
134 bool PsyTime_UnitTest();
135  // end of time
137 
138 } // namespace cmlabs
139 
140 #endif //_PSYTIME_H_
141 
Definition: PsyTime.h:53
Definition: Bitmap.h:7