Files
2024-09-23 12:00:04 -04:00

291 lines
7.5 KiB
C++

/*
* Copyright Onix Solutions Limited [OnixS]. All rights reserved.
*
* This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
* and international copyright treaties.
*
* Access to and use of the software is governed by the terms of the applicable OnixS Software
* Services Agreement (the Agreement) and Customer end user license agreements granting
* a non-assignable, non-transferable and non-exclusive license to use the software
* for it's own data processing purposes under the terms defined in the Agreement.
*
* Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
* of this source code or associated reference material to any other location for further reproduction
* or redistribution, and any amendments to this copyright notice, are expressly prohibited.
*
* Any reproduction or redistribution for sale or hiring of the Software not in accordance with
* the terms of the Agreement is a violation of copyright law.
*/
#pragma once
#include <string>
#include <OnixS/FIXEngine/ABI.h>
#include <OnixS/FIXEngine/FIX/TimeSpan.h>
namespace OnixS {
namespace FIX {
typedef Amount Year;
typedef Amount Day;
typedef Amount Hour;
typedef Amount Minute;
typedef Amount Second;
typedef Amount Millisecond;
typedef Amount Microsecond;
typedef Amount Nanosecond;
typedef HugeAmount Picosecond;
/// Defines all the months in the year.
struct ONIXS_FIXENGINE_API Month {
enum Enum {
Undefined,
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
};
};
/// Year, month, day fields.
struct ONIXS_FIXENGINE_API YearMonthDay {
Year year;
Month::Enum month;
Day day;
};
/// The collection of timestamp formats supported.
struct ONIXS_FIXENGINE_API TimestampFormat {
enum Enum {
/// Indicates the timestamp in the "YYYYMMDD" format.
YYYYMMDD,
/// Indicates the timestamp in the "YYYYMMDD-HH:MM:SS" format.
YYYYMMDDHHMMSS,
/// Indicates the timestamp in the "YYYYMMDD-HH:MM:SS.sss" format.
YYYYMMDDHHMMSSMsec,
/// Indicates the timestamp in the "YYYYMMDD-HH:MM:SS.ssssss" format.
YYYYMMDDHHMMSSUsec,
/// Indicates the timestamp in the "YYYYMMDD-HH:MM:SS.sssssssss" format.
YYYYMMDDHHMMSSNsec,
/// Indicates the timestamp in the "YYYYMMDD-HH:MM:SS.ssssssssssss" format.
YYYYMMDDHHMMSSPsec
};
};
/// The timestamps related functionality.
class ONIXS_FIXENGINE_API Timestamp
{
public:
/// Constructs an uninitialized instance.
Timestamp();
/// Initializes from seconds and nanoseconds since Jan 1, 0001, 00:00:00.
Timestamp(
TotalSeconds seconds,
Nanosecond nanosecond);
/// Initializes from seconds and picoseconds since Jan 1, 0001, 00:00:00.
Timestamp(
TotalSeconds seconds,
Picosecond picosecond);
/// Initializes date-time from all details.
Timestamp(
Year year, Month::Enum month, Day day,
Hour hour = 0, Minute minute = 0, Second second = 0,
Nanosecond nanosecond = 0);
/// Initializes date-time from all details.
Timestamp(
Year year, Month::Enum month, Day day,
Hour hour, Minute minute, Second second,
Picosecond picosecond);
/// Initializes from nanoseconds since Jan 1, 1970, 00:00:00 (Unix epoch).
explicit Timestamp(TotalNanoseconds unixTimestampNanoseconds);
/// The current year.
Year year() const;
/// The current month.
Month::Enum month() const;
/// The current day of month.
/// Valid values are 1 through 31.
Day day() const;
/// Returns the date part of the timestamp.
void date(YearMonthDay &) const;
/// The current hour.
/// Valid values are 0 through 23.
Hour hour() const;
/// The current minute.
/// Valid values are 0 through 59.
Minute minute() const;
/// The current second.
/// Valid values are 0 through 59.
Second second() const;
/// The current millisecond.
/// Valid values are 0 through 999.
Millisecond millisecond() const;
/// The current microsecond.
/// Valid values are 0 through 999999.
Microsecond microsecond() const;
/// The current nanosecond.
/// Valid values are 0 through 999999999.
Nanosecond nanosecond() const;
/// The current picosecond.
/// Valid values are 0 through 999999999999.
Picosecond picosecond() const;
/// The total number of seconds since Jan 1, 0001, 00:00:00.
TotalSeconds totalSeconds() const;
/// Compares the instance with another one.
bool operator == (const Timestamp &) const;
/// Compares the instance with another one.
bool operator != (const Timestamp &) const;
/// Tests whether the instance is less than another one.
bool operator < (const Timestamp &) const;
/// Increases the instance by time span
Timestamp & operator += (const TimeSpan &);
/// Decreases the instance by time span
Timestamp & operator -= (const TimeSpan &);
/// Returns the timestamp text presentation in the requested
/// format ("YYYYMMDD-HH:MM:SS.sssssssss" by default).
std::string
toString(
TimestampFormat::Enum =
TimestampFormat::YYYYMMDDHHMMSSNsec) const;
/// Appends the timestamp text presentation in the requested
/// format ("YYYYMMDD-HH:MM:SS.sssssssss" by default).
void
toString(
std::string & str,
TimestampFormat::Enum =
TimestampFormat::YYYYMMDDHHMMSSNsec) const;
/// Returns the nanosecond timestamp since the Unix epoch.
TotalNanoseconds toUnixNanosecondTimestamp() const;
/// Returns the current UTC time.
///
/// @note The timestamp resolution depends on
/// capabilities of an operating system.
static Timestamp utc();
/// Returns the current local time.
///
/// @note The timestamp resolution depends on
/// capabilities of an operating system.
static Timestamp local();
/// Parses the timestamp from its text presentation assuming it's in
/// the specified format ("YYYYMMDD-HH:MM:SS.sssssssss" by default).
static
Timestamp
parse(
const std::string &,
TimestampFormat::Enum =
TimestampFormat::YYYYMMDDHHMMSSNsec);
private:
friend class TimeManager;
TotalSeconds totalSeconds_;
Picosecond picoseconds_;
};
inline
Timestamp::Timestamp(
TotalSeconds totalSeconds, Nanosecond subseconds)
: totalSeconds_(totalSeconds),
picoseconds_(subseconds * static_cast<Picosecond>(TimeDetails::PicosecondsPerNanosecond))
{
}
inline
Timestamp::Timestamp(
TotalSeconds totalSeconds, Picosecond subseconds)
: totalSeconds_(totalSeconds),
picoseconds_(subseconds)
{
}
inline
TotalSeconds
Timestamp::totalSeconds() const
{
return totalSeconds_;
}
inline
Millisecond
Timestamp::millisecond() const
{
return static_cast<Millisecond>(picoseconds_ / TimeDetails::PicosecondsPerMillisecond);
}
inline
Microsecond
Timestamp::microsecond() const
{
return static_cast<Microsecond>(picoseconds_ / TimeDetails::PicosecondsPerMicrosecond);
}
inline
Nanosecond
Timestamp::nanosecond() const
{
return static_cast<Nanosecond>(picoseconds_ / TimeDetails::PicosecondsPerNanosecond);
}
inline
Picosecond
Timestamp::picosecond() const
{
return picoseconds_;
}
inline
std::string
Timestamp::toString(
TimestampFormat::Enum format) const
{
std::string str;
toString(str, format);
return str;
}
}
}