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>
19SB_INTERNAL_FWD(TimestampTest);
20SB_INTERNAL_FWD(TimestampUtil);
21SB_INTERNAL_FWD(Errors);
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; }
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();
409NMSTL_NAMESPACE_BEGIN;
413 return sb::Timestamp::getUnaligned(p);
418 sb::Timestamp::setUnaligned(p, value);
Attempted to parse an invalid Timestamp string.
Definition: Timestamp.hpp:35
Class for representing moments in time and intervals at millisecond granularity.
Definition: Timestamp.hpp:39
long long toSecs() const
Return the time as seconds.
Definition: Timestamp.hpp:251
void setMonth(int month)
Set month since January.
Timestamp()
Create a default Timestamp.
Definition: Timestamp.hpp:71
static Timestamp seconds(T seconds)
Create a time from a count of seconds.
Definition: Timestamp.hpp:182
double getSecond() const
Return seconds after the minute.
long long toMsecs() const
Return the time as milliseconds.
Definition: Timestamp.hpp:246
double operator/(Timestamp rhs) const
Divide two Timestamps representing an interval to find their quotient.
Definition: Timestamp.hpp:126
static Timestamp now()
Return a Timestamp representing the current moment in time.
int getMonth() const
Return month since January.
int compare(const Timestamp &other) const
compare 2 timestamps return less than zero this < other return greater than zero this > other return ...
Definition: Timestamp.hpp:259
int getMinute() const
Return minutes after hour.
Timestamp & operator/=(T rhs)
Divide a Timestamp representing an interval by a constant.
Definition: Timestamp.hpp:119
bool isInterval() const
Return true if this Timestamp is an interval.
Definition: Timestamp.hpp:170
Timestamp operator/(T rhs) const
Divide a Timestamp representing an interval by a constant.
Definition: Timestamp.hpp:113
static Timestamp minutes(T minutes)
Construct an interval from minutes.
Definition: Timestamp.hpp:195
Timestamp & operator+=(const Timestamp &rhs)
Increment the Timestamp by the interval.
bool operator!=(const Timestamp &rhs) const
Return true if this Timestamp is not at the same time as rhs.
Definition: Timestamp.hpp:156
Timestamp & operator-=(const Timestamp &rhs)
Decrement the Timestamp by the interval.
static Timestamp days(T days)
Construct an interval from days.
Definition: Timestamp.hpp:221
bool operator>=(const Timestamp &rhs) const
Return true if this Timestamp is after or at the same time as rhs.
Definition: Timestamp.hpp:161
static Timestamp hours(T hours)
Construct an interval from hours.
Definition: Timestamp.hpp:208
void setDayOfMonth(int day_of_month)
Set day of month.
static Timestamp days(double days)
Construct an interval from days.
Definition: Timestamp.hpp:226
int getHour() const
Return hours since midnight.
Timestamp & operator*=(T rhs)
Multiply a Timestamp representing an interval by a constant.
Definition: Timestamp.hpp:107
void setSecond(double seconds)
Set seconds after the minute.
static Timestamp milliseconds(long long milliseconds)
Create a time from a count of milliseconds.
Definition: Timestamp.hpp:175
static Timestamp weeks(T weeks)
Construct an interval from weeks.
Definition: Timestamp.hpp:234
bool operator>(const Timestamp &rhs) const
Return true if this Timestamp is after rhs.
Definition: Timestamp.hpp:165
void setHour(int hours)
Set hours since midnight.
Timestamp operator+(const Timestamp &rhs) const
Add a Timestamp and an interval to get a Timestamp.
Timestamp operator%(const Timestamp &rhs) const
Modulo a Timestamp by an interval.
Definition: Timestamp.hpp:131
static Timestamp hours(double hours)
Construct an interval from hours.
Definition: Timestamp.hpp:213
Timestamp operator-(const Timestamp &rhs) const
Subtract two Timestamps to return an interval.
Timestamp operator*(T rhs) const
Multiply a Timestamp representing an interval by a constant.
Definition: Timestamp.hpp:101
Timestamp operator-() const
Negate an interval to return an interval.
double getMillisecond() const
Return milliseconds after the second.
Timestamp operator%=(const Timestamp &rhs)
Modulo a Timestamp by an interval.
Definition: Timestamp.hpp:137
void setMinute(int minutes)
Set minutes after the hour.
void setYear(int year)
Set year.
static Timestamp minutes(double minutes)
Construct an interval from minutes.
Definition: Timestamp.hpp:200
int getDayOfWeek() const
Return day since Sunday.
int getYear() const
Return year.
int getDayOfMonth() const
Return day of month.
bool operator<(const Timestamp &rhs) const
Return true if this Timestamp is before rhs.
Definition: Timestamp.hpp:143
bool operator==(const Timestamp &rhs) const
Return true if this Timestamp is at the same time as rhs.
Definition: Timestamp.hpp:152
bool operator<=(const Timestamp &rhs) const
Return true if this Timestamp is before or at the same time as rhs.
Definition: Timestamp.hpp:148
static Timestamp weeks(double weeks)
Construct an interval from weeks.
Definition: Timestamp.hpp:239
static Timestamp seconds(double seconds)
Create a time from a count of seconds.
Definition: Timestamp.hpp:187
std::string as_string(bool displayTimezone=true) const
Return a human readable string rep of this Timestamp.