5 #ifndef STREAMBASE_TIMESTAMP_H 
    6 #define STREAMBASE_TIMESTAMP_H 
    8 #include "StreamBase.hpp" 
   11 #include "Exceptions.hpp" 
   12 #include <NMSTL/ntime.hpp> 
   13 #include <NMSTL/platform.hpp> 
   19 SB_INTERNAL_FWD(TimestampTest);
 
   20 SB_INTERNAL_FWD(TimestampUtil);
 
   21 SB_INTERNAL_FWD(Errors);
 
   25 NMSTL_NAMESPACE_BEGIN;
 
   41     enum TIMESTAMPTYPE { TIMESTAMP = 0, INTERVAL = 1 };
 
   42     enum TIMESTAMP_FIELD {YEAR, MONTH, DAY, HOUR, MINUTE, SECOND};
 
   47         long long int value : 63;
 
   48         long long int type : 1;
 
   50         long long int type : 1;
 
   51         long long int value : 63;
 
   56     static const int MIN_YEAR = 1400;
 
   57     static const int MAX_YEAR = 9999;
 
   58     static const int MIN_MONTH = 1;
 
   59     static const int MAX_MONTH = 12;
 
   60     static const int MIN_DAY = 1;
 
   61     static const int MAX_DAY = 31;
 
   62     static const int MIN_HOUR = 0;
 
   63     static const int MAX_HOUR = 23;
 
   64     static const int MIN_MINUTE = 0;
 
   65     static const int MAX_MINUTE = 59;
 
   66     static const int MIN_SECOND = 0;
 
   67     static const int MAX_SECOND = 59;
 
  102         MS t = makeMS((TIMESTAMPTYPE)_ms.type, (
long long int)(_ms.value * rhs));
 
  114         MS t = makeMS((TIMESTAMPTYPE)_ms.type, (
long long int)(_ms.value / rhs));
 
  127         return (
double)_ms.value / (double)rhs._ms.value;
 
  132         MS t = makeMS(INTERVAL, _ms.value % rhs._ms.value);
 
  144             return (_ms.value < rhs._ms.value);
 
  149             return (_ms.value <= rhs._ms.value);
 
  153             return (_ms.value == rhs._ms.value);
 
  157             return (_ms.value != rhs._ms.value);
 
  162             return (_ms.value >= rhs._ms.value);
 
  166             return (_ms.value > rhs._ms.value);
 
  171         return (_ms.type != TIMESTAMP);
 
  176         MS t = makeMS(INTERVAL, milliseconds);
 
  183         MS t = makeMS(INTERVAL, ((
long long)(seconds * THOUSAND)));
 
  188         double roundingValue = seconds > 0 ? 0.5 : -0.5;
 
  189         MS t = makeMS(INTERVAL, ((
long long)(seconds * THOUSAND + roundingValue)));
 
  196         MS t = makeMS(INTERVAL, ((
long long)minutes) * MILLISECONDS_PER_MINUTE);
 
  201         double roundingValue = minutes > 0 ? 0.5 : -0.5;
 
  202         MS t = makeMS(INTERVAL, (
long long int)(minutes * MILLISECONDS_PER_MINUTE + roundingValue));
 
  209         MS t = makeMS(INTERVAL, ((
long long)hours) * MILLISECONDS_PER_HOUR);
 
  214         double roundingValue = hours > 0 ? 0.5 : -0.5;
 
  215         MS t = makeMS(INTERVAL, (
long long int)(hours * MILLISECONDS_PER_HOUR + roundingValue));
 
  222         MS t = makeMS(INTERVAL, ((
long long)days) * MILLISECONDS_PER_DAY);
 
  227         double roundingValue = days > 0 ? 0.5 : -0.5;
 
  228         MS t = makeMS(INTERVAL, (
long long int)(days * MILLISECONDS_PER_DAY + roundingValue));
 
  235         MS t = makeMS(INTERVAL, ((
long long)weeks)  * MILLISECONDS_PER_DAY * DAYS_PER_WEEK);
 
  240         double roundingValue = weeks > 0 ? 0.5 : -0.5;
 
  241         MS t = makeMS(INTERVAL, (
long long int)(weeks * MILLISECONDS_PER_DAY * DAYS_PER_WEEK + roundingValue));
 
  252         return _ms.value / THOUSAND;
 
  260         assert(_ms.type == other._ms.type);
 
  261         if(_ms.value < other._ms.value) { 
return -1; }
 
  262         if(_ms.value > other._ms.value) { 
return 1; }
 
  267     double getSecond() 
const;
 
  270     double getMillisecond() 
const;
 
  273     int getMinute() 
const;
 
  279     int getDayOfWeek() 
const;
 
  282     int getDayOfMonth() 
const;
 
  285     int getMonth() 
const;
 
  291     void setSecond(
double seconds);
 
  294     void setMinute(
int minutes);
 
  297     void setHour(
int hours);
 
  300     void setDayOfMonth(
int day_of_month);
 
  303     void setMonth(
int month);
 
  306     void setYear(
int year);
 
  309     std::string as_string(
bool displayTimezone=
true) 
const;
 
  312     friend class sb_internal::TimestampTest;
 
  315     void setField(TIMESTAMP_FIELD, 
int *value);
 
  318     static const long long MILLISECONDS_PER_MINUTE = 60000LL;
 
  319     static const long long MILLISECONDS_PER_HOUR = 3600000LL;
 
  320     static const long long MILLISECONDS_PER_DAY = 86400000LL;
 
  321     static const long long DAYS_PER_WEEK = 7LL;
 
  322     static const long long THOUSAND = 1000LL;
 
  323     static const long long MILLION = 1000000LL;
 
  327     static MS makeMS(TIMESTAMPTYPE t, 
long long v);
 
  333     static bool validateTimestampFields(
 
  341     Timestamp(
long long int ts, 
int type=TIMESTAMP)
 
  352         struct { 
int i1; 
int i2; };
 
  355     inline static Timestamp
 
  356     getUnaligned(
const Timestamp *p)
 
  358         const int *dip = (
const int*)p;
 
  362         return Timestamp(di.ms);
 
  366     setUnaligned(Timestamp *p, Timestamp value)
 
  376     inline static Timestamp getUnaligned(
const Timestamp *p) { 
return *p; }
 
  377     inline static void setUnaligned(Timestamp *p, Timestamp value) { *p = value; }
 
  380     friend class sb_internal::TimestampUtil;
 
  388     static const long long RESOLUTION_PER_SEC = 1000LL;    
 
  389     static const long long MIN_SYSTEM_INTERVAL = 10LL;     
 
  390     static const long long MAX_SYSTEM_INTERVAL = 100LL;    
 
  391     static const unsigned long long TICKS_PER_SEC;         
 
  392     static const unsigned long long TICKS_PER_RESOLUTION;  
 
  397     static SB_THREAD_LOCAL 
unsigned long long systemTicks;    
 
  398     static SB_THREAD_LOCAL 
long long systemTime;
 
  399     static SB_THREAD_LOCAL 
long long systemInterval;
 
  400     static SB_THREAD_LOCAL 
long long lastTimestamp;
 
  403     static unsigned long long initCpuTicksPerSecond();
 
  405     static unsigned long long cpuTicks();
 
  408 inline unsigned long long Timestamp::cpuTicks()
 
  410     unsigned int low, high;
 
  413     asm volatile (
"rd %%tick, %%g1; srlx %%g1, 32, %1" : 
"=r" (low), 
"=r" (high));
 
  416 LARGE_INTEGER timestamp;
 
  419         QueryPerformanceCounter(×tamp);
 
  424                         mov timestamp.LowPart, EAX
 
  425                         mov timestamp.HighPart, EDX
 
  429 high = timestamp.HighPart;
 
  430 low = timestamp.LowPart;
 
  433     asm volatile (
"rdtsc" : 
"=a" (low), 
"=d" (high));
 
  436     return ((
unsigned long long)high<<32) + low;
 
  442 NMSTL_NAMESPACE_BEGIN;
 
  446     return sb::Timestamp::getUnaligned(p);
 
  451     sb::Timestamp::setUnaligned(p, value);
 
  455 #endif //DOXYGEN_SKIP