Ocean
Loading...
Searching...
No Matches
Ocean::DateTime Class Reference

This class provides basic functionalities for date and time handling. More...

#include <DateTime.h>

Static Public Member Functions

static double timestamp ()
 Returns the seconds since 1970.01.01 00::00::00 to current UTC time.
static long long microseconds ()
 Returns the microseconds since 1970.01.01 00::00::00 to current UTC time.
static std::string string (const bool addMilliseconds=false)
 Returns the string of the UTC date and time.
static std::string string (const double timestamp, const bool addMilliseconds=false)
 Returns the string of a given unix timestamp.
static std::string stringDate (const char delimiter='.')
 Returns the string of the UTC date.
static std::string stringDate (const double timestamp, const bool nameMonths=false)
 Returns the string of a given unix timestamp.
static std::string stringTime (const bool addMilliseconds=false, const char delimiter='-')
 Returns the string of the UTC time.
static std::string stringTime (const double timestamp, const bool addMilliseconds=false, const char delimiter='-')
 Returns the string of a given unix timestamp.
static double localTimestamp ()
 Returns the seconds since 1970.01.01 00::00::00 to current local time.
static long long localMicroseconds ()
 Returns the microseconds since 1970.01.01 00::00::00 to current local time.
static std::string localString ()
 Returns the string of the local date and time suitable for a text/log output.
static std::string localStringForFile ()
 Returns the string of the local date and time suitable for a filename.
static std::string localStringDate (const char delimiter='.')
 Returns the string of the local date.
static double date2timestamp (const unsigned int year, const unsigned int month, const unsigned int day, const unsigned int hour, const unsigned int minute, const unsigned int second, const unsigned int millisecond=0u)
 Converts a date to the seconds since 1970.01.01 00:00:00.
static void timestamp2date (const double timestamp, unsigned int &year, unsigned int &month, unsigned int &day, unsigned int &hour, unsigned int &minute, unsigned int &second, unsigned int *millisecond=nullptr)
 Converts a unix timestamp (the seconds since 1970.01.01 00:00:00) to a date.
static std::string seconds2string (const double totalSeconds, const bool addDescription, const bool addMilliseconds=false)
 Converts a number of seconds to a time string.
static std::string localStringTime (const bool addMilliseconds=false, const char delimiter=':')
 Returns the string of the local time.
static unsigned int isLeapYear (const unsigned int year)
 Returns whether a given year is a leap year.
static bool isDateValid (const unsigned int year, const unsigned int month, const unsigned int day)
 Returns whether a given date is a valid date (e.g., in no leap years the 29th February is an invalid date).

Detailed Description

This class provides basic functionalities for date and time handling.


All functions are available for UTC and local time.

Member Function Documentation

◆ date2timestamp()

double Ocean::DateTime::date2timestamp ( const unsigned int year,
const unsigned int month,
const unsigned int day,
const unsigned int hour,
const unsigned int minute,
const unsigned int second,
const unsigned int millisecond = 0u )
static

Converts a date to the seconds since 1970.01.01 00:00:00.

Parameters
yearThe year, with range [1970, 2037)
monthThe month, with range [1, 12]
dayThe day, with range [1, 31]
hourThe hour, with range [0, 23]
minuteThe minute, with range [0, 59]
secondThe second, with range [0, 59]
millisecondThe millisecond, with range [0, 999]
Returns
The unix timestamp, -1 if the given date is invalid

◆ isDateValid()

bool Ocean::DateTime::isDateValid ( const unsigned int year,
const unsigned int month,
const unsigned int day )
static

Returns whether a given date is a valid date (e.g., in no leap years the 29th February is an invalid date).

Parameters
yearThe year of the date, with range [0, infinity)
monthThe month of the date, with range [1, 12]
dayThe day of the date, with range [1, 31]
Returns
True, if so

◆ isLeapYear()

unsigned int Ocean::DateTime::isLeapYear ( const unsigned int year)
inlinestatic

Returns whether a given year is a leap year.

Parameters
yearThe year to check

◆ localMicroseconds()

long long Ocean::DateTime::localMicroseconds ( )
static

Returns the microseconds since 1970.01.01 00::00::00 to current local time.

Returns
Microseconds

◆ localString()

std::string Ocean::DateTime::localString ( )
static

Returns the string of the local date and time suitable for a text/log output.

The format of the resulting string is YYYY.MM.DD HH:MM:SS.

Returns
The resulting date and time string
See also
localStringForFile().

◆ localStringDate()

std::string Ocean::DateTime::localStringDate ( const char delimiter = '.')
static

Returns the string of the local date.

Parameters
delimiterThe delimiter to be used
Returns
Date string of local date, with format YYYY.MM.DD, if delimiter is '.'

◆ localStringForFile()

std::string Ocean::DateTime::localStringForFile ( )
static

Returns the string of the local date and time suitable for a filename.

The format of the resulting string is YYYY-MM-DD_HH-MM-SS.

Returns
The resulting date and time string
See also
localString().

◆ localStringTime()

std::string Ocean::DateTime::localStringTime ( const bool addMilliseconds = false,
const char delimiter = ':' )
static

Returns the string of the local time.

The resulting string has the following format: HH:MM:SS, or HH:MM:SS:mmmm if the milliseconds are added.

Parameters
addMillisecondsTrue, to add the milliseconds to the string
delimiterThe delimiter to be used
Returns
Time string of local time

◆ localTimestamp()

double Ocean::DateTime::localTimestamp ( )
static

Returns the seconds since 1970.01.01 00::00::00 to current local time.

Returns
Seconds

◆ microseconds()

long long Ocean::DateTime::microseconds ( )
static

Returns the microseconds since 1970.01.01 00::00::00 to current UTC time.

Returns
Microseconds

◆ seconds2string()

std::string Ocean::DateTime::seconds2string ( const double totalSeconds,
const bool addDescription,
const bool addMilliseconds = false )
static

Converts a number of seconds to a time string.

HH:MM:SS                                        e.g. "01:02:03", or "111:06:40" beyond 99 hours
HH:MM:SS:mmmm                                   e.g. "01:02:03:0500"                                    (addMilliseconds)
H hours, M minutes, S seconds                   e.g. "1 hours, 2 minutes, 3 seconds"                    (addDescription)
H hours, M minutes, S seconds, m milliseconds   e.g. "1 hours, 2 minutes, 3 seconds, 500 milliseconds"  (both)
Parameters
totalSecondsThe number of seconds to convert, with range [0, 15461882265600)
addDescriptionTrue, to write the units out instead of using the HH:MM:SS format
addMillisecondsTrue, to append the milliseconds
Returns
The resulting time string

◆ string() [1/2]

std::string Ocean::DateTime::string ( const bool addMilliseconds = false)
static

Returns the string of the UTC date and time.

Parameters
addMillisecondsTrue, to add the milliseconds to the string
Returns
Date and time string

◆ string() [2/2]

std::string Ocean::DateTime::string ( const double timestamp,
const bool addMilliseconds = false )
static

Returns the string of a given unix timestamp.

Parameters
timestampThe unix timestamp (seconds since 1970.01.01 00:00:00) to be converted, with range [0, infinity)
addMillisecondsTrue, to add the milliseconds to the string
Returns
Date and time string

◆ stringDate() [1/2]

std::string Ocean::DateTime::stringDate ( const char delimiter = '.')
static

Returns the string of the UTC date.

Parameters
delimiterThe delimiter to be used
Returns
Date string, with pattern YYYY.MM.DD, if delimiter is '.'

◆ stringDate() [2/2]

std::string Ocean::DateTime::stringDate ( const double timestamp,
const bool nameMonths = false )
static

Returns the string of a given unix timestamp.

Parameters
timestampThe unix timestamp (seconds since 1970.01.01 00:00:00) to be converted, with range [0, infinity)
nameMonthsTrue, to receive a date with named months; False, to receive a date with digits only (but with constant length)
Returns
Date string, with pattern YYYY.MM.DD (2016.08.05) for nameMonths=false; or MMMMMMMMMM D/DD, YYYY (May 5, 2016) for nameMonths=true

◆ stringTime() [1/2]

std::string Ocean::DateTime::stringTime ( const bool addMilliseconds = false,
const char delimiter = '-' )
static

Returns the string of the UTC time.

The resulting string has the following format: HH-MM-SS, or HH-MM-SS-mmmm if the milliseconds are added.

Parameters
addMillisecondsTrue, to add the milliseconds to the string
delimiterThe delimiter to be used
Returns
Time string of UTC time

◆ stringTime() [2/2]

std::string Ocean::DateTime::stringTime ( const double timestamp,
const bool addMilliseconds = false,
const char delimiter = '-' )
static

Returns the string of a given unix timestamp.

The resulting string has the following format: HH-MM-SS, or HH-MM-SS-mmmm if the milliseconds are added.

Parameters
timestampThe unix timestamp (seconds since 1970.01.01 00:00:00) to be converted, with range [0, infinity)
addMillisecondsTrue, to add the milliseconds to the string
delimiterThe delimiter to be used
Returns
Time string of the UTC time

◆ timestamp()

double Ocean::DateTime::timestamp ( )
static

Returns the seconds since 1970.01.01 00::00::00 to current UTC time.

Returns
Seconds

◆ timestamp2date()

void Ocean::DateTime::timestamp2date ( const double timestamp,
unsigned int & year,
unsigned int & month,
unsigned int & day,
unsigned int & hour,
unsigned int & minute,
unsigned int & second,
unsigned int * millisecond = nullptr )
static

Converts a unix timestamp (the seconds since 1970.01.01 00:00:00) to a date.

Parameters
timestampThe unix timestamp to convert
yearThe resulting year, with range [1970, 2037)
monthThe resulting month, with range [1, 12]
dayThe resulting date, with range [1, 31]
hourThe resulting hour, with range [0, 23]
minuteThe resulting minute, with range [0, 59]
secondThe resulting second, with range [0, 59]
millisecondOptional resulting millisecond, with range [0, 999]

The documentation for this class was generated from the following file: