• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
CDP Studio logo

CDP Studio

The no-code and full-code software development tool for distributed control systems and HMI

  • Doc
  • Why CDP
    • Software developers
    • Automation engineers
    • Managers
  • Products
    • Automation Designer
    • HMI Designer
    • Maritime HMIs
  • Services
  • Use cases
  • Pricing
  • Try CDP

CDP Studio Documentation

  • Framework - CDP Core
  • CDPTime
  • 5.0.0

CDPTime Class

The CDPTime holds time and date in double and string format. More...

Header: #include <CDPTime>
  • List of all members, including inherited members

Public Functions

CDPTime(void)
CDPTime(const CDPTime &rhs)
~CDPTime(void)
void Clear()
std::string GetDateString(bool updateCurrentTimeFirst, bool unixFormat, bool americanFormatting, bool UTC)
std::string GetDateTimeMsString(bool updateCurrentTimeFirst, bool unixFormat, bool americanFormatting, bool UTC)
std::string GetDateTimeString(bool updateCurrentTimeFirst, bool unixFormat, bool americanFormatting, bool UTC)
std::string GetTimeMsString(bool updateCurrentTimeFirst, bool UTC)
double GetTimeRelative(const char *time, const char *date, bool UTC)
double GetTimeRelative()
double GetTimeRelative(std::string &strDosTime, bool UTC)
std::string GetTimeString(bool updateCurrentTimeFirst, bool UTC)
void SetFromDosTime(std::string strTime, bool UTC)
CDPTime &Time()
void Update()
void Update(double time)
void Update(const char *time, const char *date, bool UTC)
void Update(const char *dateTime, bool UTC)
bool operator!=(const CDPTime &rhs)
CDPTime &operator-=(const CDPTime &rhs)
CDPTime &operator=(const CDPTime &rhs)
bool operator==(const CDPTime &rhs)

Static Public Members

void AddGlobalTimeChangedHandler(const std::function<void( double, const std::string &, const std::string & ) > &myTimeChangedHandler)
std::string ConvertGivenDoubleToTimeString(double secondsSince1970, bool UTC, CDPTimeFormat timeFormat)
double ConvertGivenTimeToDouble(const char *givenTimeStr, bool UTC, CDPTimeFormat timeFormat = CDP_DOS_American)
std::string GetCurrentDate(bool unixFormat, bool UTC)
std::string GetCurrentDateTimeMsString(bool unixFormat, bool UTC)
std::string GetCurrentDateTimeMsStringCompensated(bool unixFormat, double timeSeconds, bool UTC)
std::string GetCurrentTimeMsString(bool UTC)
std::string GetCurrentTimeMsStringAccurate(int nrDecimals, bool UTC)
double GetGlobalClockZero()
double GetGlobalTime()
double GetGlobalTimeFromHardware()
void GetTZandDSTinfo(int &TZnumbSecond, int &DST)
double GetTimeSince(const char *givenTimeStr, bool UTC)
const CDPTime &GetZeroTime()
double GlobalClock()
double GlobalClockMs()
std::string GlobalClockString(double secondsSince1970, int nrDecimals, bool UTC)
std::string GlobalClockString(int numberOfDecimals, bool UTC)
void Update(double time, CDPTime &timeToUpdate)

Protected Functions

bool CreateTime(CDPTime &specc_time, const char *time, const char *date, bool UTC)

Protected Variables

short dstflag
unsigned short millitm
int64_t time
short timezone

Static Protected Members

CDPTimer cdpTimer
double zeroGlobalClock
CDPTime zeroTime

Related Non-Members

enum CDPTimeFormat { CDP_DOS_American, CDP_UNIX, CDP_DOS_American_No_Ms, CDP_DOS_American_Date, ..., CDP_Day_TimeShortMs }
int OSAPIAsctime(char *buffer, const int numbElements, const tm *_tm)
int OSAPICTime(char *buffer, const int numbElements, const int64_t *time)
int OSAPIGMTime(tm *_tm, const int64_t *time)
int OSAPILocalTime(tm *_tm, const int64_t *time)
int64_t OSAPIMKGMTime(tm *time)
int64_t OSAPIMKTime(tm *time)

Detailed Description

The CDPTime holds time and date in double and string format.

Use this class to work with time and dates and return them as strings or doubles.

It is possible to convert between string data, time and relative CDPTime time.

The CDPTime time is easy to work with when comparing times, as it is a double number specifying time in seconds.

Specifications

  • Resolution: HW dependent, typically equals clock frequency of the computer.
  • Accuracy: OS and HW dependent.

Usage

  • Alternative 1:

    Use the static member functions without creating instances first. This is the preferred method when reading the current time.

  • Alternative 2:

    This method is preferred when not wanting the time to be updated to the current time for each call to a member function.

    • Create a CDPTime object.
    • Either read out date and/or time with Get...() methods. The get methods will return the time/date of last Update() call. Update() is called from the non-static Get methods if the first parameter is true.

Examples

In the examples below, Operating system is set up with timezone 'UTC+1' and automatically adjust clock for Daylight Saving Time.

Alternative 1 with static member functions: When a CDP application starts, there is a printout in top of the CDPMessage log about date and time. The code is like this:

std::string CDPMessages::GetTimeStampStringIfNewDate()
{
  if (m_bConfigured)
  {
    bool unixFormat = true;
    std::string currentDate = CDPTime::GetCurrentDate(unixFormat,false);
    if (currentDate != m_currentDate)
    {
      const char *firstChar = currentDate.c_str();
      if (firstChar[0] != '?')
      {
        std::string currentLocalTime = CDPTime::GetCurrentTimeMsString(false);
        std::string currentUTCTime = CDPTime::GetCurrentTimeMsString(true);
        m_currentDate = currentDate;
        return "* " + currentDate + " " + currentLocalTime + " (" + currentUTCTime + " UTC) *\n";
      }
    }
  }
  return "";
}

The printed line in CDPMessage log could be like this:

* Fri Sep 23 2016 08:53:51.176 (06:53:51.176 UTC) *

Since summertime is active, the difference between currentLocalTime and currentUTCTime is 2 hours.

Alternative 2 where creating a CDPTime object:

CDPTime testCDPTime;
std::string dateTimeObj = testCDPTime.GetDateTimeMsString(false,true,true,true);
CDPMessage("testCDPTime.GetDateTimeMsString(false,true,true,true) = %s.\n", dateTimeObj.c_str());
dateTimeObj = testCDPTime.GetDateTimeMsString(false,true,true,false);
CDPMessage("testCDPTime.GetDateTimeMsString(false,true,true,false) = %s.\n", dateTimeObj.c_str());
dateTimeObj = testCDPTime.GetDateTimeMsString(true,true,true,true);
CDPMessage("testCDPTime.GetDateTimeMsString(true,true,true,true) = %s.\n", dateTimeObj.c_str());
dateTimeObj = testCDPTime.GetDateTimeMsString(true,true,true,false);
CDPMessage("testCDPTime.GetDateTimeMsString(true,true,true,false) = %s.\n", dateTimeObj.c_str());

could give these printouts when using CDPMessage:

10:31:46.206 testCDPTime.GetDateTimeMsString(false,true,true,true) = Thu Jan 01 00:00:00.000 1970.
10:31:46.206 testCDPTime.GetDateTimeMsString(false,true,true,false) = Thu Jan 01 01:00:00.000 1970.
10:31:46.207 testCDPTime.GetDateTimeMsString(true,true,true,true) = Wed Sep 14 08:31:46.206 2016.
10:31:46.207 testCDPTime.GetDateTimeMsString(true,true,true,false) = Wed Sep 14 10:31:46.207 2016.

The two first GetDateTimeMsString function-calls have false in first argument (updateCurrentTimeFirst), and the two last have true. The last argument (UTC) is changing between true and false. The reason why there is 1 hour difference between the two first printouts, and 2 hours difference in the two last printouts, is that summertime is not active Jan 01, and active Sep 14.

See also CDPTimerMs, CDPParameterTimerMs, and CDPTimer.

Member Function Documentation

CDPTime::CDPTime(void)

Constructor where non-static member variables are initialized to 0.

CDPTime::CDPTime(const CDPTime &rhs)

Constructs CDPTime from another CDPTime instance.

CDPTime::~CDPTime(void)

Destructs the instance.

[static] void CDPTime::AddGlobalTimeChangedHandler(const std::function<void( double, const std::string &, const std::string & ) > &myTimeChangedHandler)

Adds a time changed handler function myTimeChangedHandler to m_ListTimeChangedHandlers

Example code if you want to add a function which will be called by changes in time (typical due to clocksync):

#include <functional>
...
using namespace std::placeholders;
CDPTime::AddGlobalTimeChangedHandler( std::bind(&Application::CDPTimeChangedHandler, this, _1, _2, _3) );

where CDPTimeChangedHandler is declared as

void CDPTimeChangedHandler(double secondsChanged, const std::string& oldDateTime, const std::string& newDateTime);

void CDPTime::Clear()

Sets all non-static members to 0.

[static] std::string CDPTime::ConvertGivenDoubleToTimeString(double secondsSince1970, bool UTC, CDPTimeFormat timeFormat)

Converts a double-value (number of seconds since 1.1.1970) to time string of UNIX or DOS format.

Warning: If UTC is set to false, the result will be different depending on DST setting is 1 or not in the OS.

Returns timestring in wanted timeformat.

Parameter secondsSince1970Number of seconds since 1.1.1970 (must not be negative)
Parameter UTCIf true: return-value will be in UTC time (there will be no adjustments for TZ and DST). If false: return-value will be in localtime (adjusted for TZ and DST). Internally, OSAPIGMTime() or OSAPILocalTime()/OSAPICTime() is used respectively.
Parameter timeFormatChosen timeformat of string to be returned, see CDPTimeFormat.

[static] double CDPTime::ConvertGivenTimeToDouble(const char *givenTimeStr, bool UTC, CDPTimeFormat timeFormat = CDP_DOS_American)

Converts a string-time to double-value (number of seconds since 1.1.1970).

Get the number of seconds since 1.1.1970 to given time. Internally, OSAPIMKTime() is used if UTC is false, and OSAPIMKGMTime() is used if UTC is true. See description of these functions.

Warning: If UTC is set to false, the result will be different depending on DST setting is 1 or not in the OS.

Returns number of seconds between 1.1.1970 and givenTimeStr.

Parameters givenTimeStrGiven time string (yy: will be interpreted as year in range [1970..2069].
Parameter UTCIf true, given time/date is in UTC and there will be no adjustments for TZ and DST. If false, given time is in local time and it will be adjusted for TZ and DST (fetched from OS).
Parameter timeFormatChosen timeformat of string given, see CDPTimeFormat. Default is CDP_DOS_American.

[protected] bool CDPTime::CreateTime(CDPTime &specc_time, const char *time, const char *date, bool UTC)

Creates time struct from time and date strings.

Valid date formats

  • Fri Jan 21 2003
  • ddmm
  • dd.mm
  • ddmmyyyy
  • dd.mm.yyyy

Valid time formats

  • HH:MM
  • HH:MM:SS
  • HH:MM:SS.mmm

Internally, OSAPIMKTime() is used if UTC is false, and OSAPIMKGMTime() is used if UTC is true. See description of these functions. The updated CDPTime variable (specc_time) contains UTC time in time and millitm. 'timezone' and 'dstflag' is neither used nor changed.

Warning: If UTC is set to false, the result will be different depending on DST setting is 1 or not in the OS.

Returns true if success.

Parameter specc_timeVariable that the converted time will be stored in when returning (time and millitm are updated).
Parameter timeValid time string (see above for valid time formats)
Parameter dateValid date string (see above for valid date formats)
Parameter UTCIf true, given time/date is in UTC and there will be no adjustments for TZ and DST. If false, given time is in local time and it will be adjusted for TZ and DST (fetched from OS).

[static] std::string CDPTime::GetCurrentDate(bool unixFormat, bool UTC)

Returns current date in UNIX or DOS format.

Formats:

  • UNIX format: "Tue May 13 2015"
  • DOS format: "05/13/15"

Returns string with date in selected format.

Parameter unixFormatSelect Unix format (true), or DOS format (false).
Parameter UTCIf true, return-value will be in UTC time. If false, return value will be in localtime adjusted for TZ and DST. Internally, OSAPIGMTime() or OSAPICTime() is used respectively.

[static] std::string CDPTime::GetCurrentDateTimeMsString(bool unixFormat, bool UTC)

Returns current date, time and milliseconds in DOS or UNIX format.

Formats:

  • UNIX format: "Tue May 13 12:57:48.323 2015"
  • DOS format: "05/13/15 12:57:48.232"

Returns string with date and time in selected format.

Parameter unixFormatSelect Unix format (true), or DOS format (false).
Parameter UTCIf true, return-value will be in UTC time. If false, return value will be in localtime adjusted for TZ and DST. Internally, OSAPIGMTime() or OSAPICTime() is used respectively.

[static] std::string CDPTime::GetCurrentDateTimeMsStringCompensated(bool unixFormat, double timeSeconds, bool UTC)

Returns current date, time and milliseconds in DOS or UNIX format, compensated with value of parameter timeSeconds.

Formats:

  • UNIX format: "Tue May 13 12:57:48.323 2015"
  • DOS format: "05/13/15 12:57:48.232"

Returns string with date and time in selected format.

Parameter unixFormatSelect Unix format (true), or DOS format (false).
Parameter timeSecondsNumber of seconds and milliseconds to compensate the time in the returning string.
Parameter UTCIf true, return-value will be in UTC time. If false, return value will be in localtime adjusted for TZ and DST. Internally, OSAPIGMTime() or OSAPICTime() is used respectively.

Note: This will not change the CDPTime, but only return a time string compensated with timeSeconds.

[static] std::string CDPTime::GetCurrentTimeMsString(bool UTC)

Returns current time with milliseconds as a string.

Returns the time in format: "HH:MM:SS.mmm".

Parameter UTCIf true, return-value will be in UTC time. If false, return value will be in localtime adjusted for TZ and DST. Internally, OSAPIGMTime() or OSAPICTime() is used respectively.

[static] std::string CDPTime::GetCurrentTimeMsStringAccurate(int nrDecimals, bool UTC)

More accurate version of GetCurrentTimeMsString().

Uses zeroTime as starting point, then adds the cdpTimer.Time() value to this time to get a very high accuracy.

Returns the time in format: "HH:MM:SS.mmmmmm"

Parameter nrDecimalsNumber of decimals to output to resulting string, from 0 to 20.
Parameter UTCIf true, return-value will be in UTC time. If false, return value will be in localtime adjusted for TZ and DST. Internally, OSAPIGMTime() or OSAPICTime() is used respectively.

std::string CDPTime::GetDateString(bool updateCurrentTimeFirst, bool unixFormat, bool americanFormatting, bool UTC)

Returns the date string.

Formats

  • UNIX format: "Tue May 13 2015"
  • DOS format: "05/13/15" (American formatting); "13.05.15" (regular formatting)

Returns string with date formatted as selected by unixFormat parameter.

Parameter updateCurrentTimeFirstIf true, Update() is called first to get the current time/date.
Parameter unixFormatSelect Unix format (true), or DOS format (false).
Parameter americanFormattingFormat as mm/dd/yy if true, dd.mm.yy if false (DOS format only)
Parameter UTCIf true, return-value will be in UTC time. If false, return value will be in localtime adjusted for TZ and DST. Internally, OSAPIGMTime() or OSAPICTime() is used respectively.

std::string CDPTime::GetDateTimeMsString(bool updateCurrentTimeFirst, bool unixFormat, bool americanFormatting, bool UTC)

Returns the date, time and milliseconds text string.

Formats

  • UNIX format: "Tue May 13 12:57:48.921 2015"
  • DOS format: "05/13/15 12:57:48.921" (American formatting); "13.05.15 12:57:48.921" (regular formatting).

Returns string with date and time in selected format.

Parameter updateCurrentTimeFirstIf true, Update() is called first to get the current time/date.
Parameter unixFormatSelect Unix format (true), or DOS format (false).
Parameter americanFormattingFormat as mm/dd/yy if true, dd.mm.yy if false (DOS format only)
Parameter UTCIf true, return-value will be in UTC time. If false, return value will be in localtime adjusted for TZ and DST. Internally, OSAPIGMTime() or OSAPICTime() is used respectively.

std::string CDPTime::GetDateTimeString(bool updateCurrentTimeFirst, bool unixFormat, bool americanFormatting, bool UTC)

Returns the date and time text string.

Formats

  • UNIX format: "Tue May 13 12:57:48 2015"
  • DOS format: "05/13/15 12:57:48" (American formatting); "13.05.15 12:57:48" (regular formatting).

Returns string with date and time in selected format.

Parameter updateCurrentTimeFirstIf true, Update() is called first to get the current time/date.
Parameter unixFormatSelect Unix format (true), or DOS format (false).
Parameter americanFormattingFormat as mm/dd/yy if true, dd.mm.yy if false (DOS format only).
Parameter UTCIf true, return-value will be in UTC time. If false, return value will be in localtime adjusted for TZ and DST. Internally, OSAPIGMTime() or OSAPICTime() is used respectively.

[static] double CDPTime::GetGlobalClockZero()

Returns number of seconds since 1.1.1970 to application startup, initialized when application is started.

May be adjusted by CDP, if clocksynch is enabled. All relative times read from this class is relative to this zero time. Returns zeroTime (UTC) converted to seconds.

[static] double CDPTime::GetGlobalTime()

Returns internal globalTime. Use this function instead of extern globalTime directly.

Note: Time returned by CDPTime::GetGlobalTime() is only updated before a Component's Process-function is called. So in threaded code, CDPTime::GetGlobalTimeFromHardware() should be used instead, since CDPTime::GetGlobalTime() may return outdated value. CDPTime::GetGlobalTime() is on the other hand less expensive to call than CDPTime::GetGlobalTimeFromHardware().

[static] double CDPTime::GetGlobalTimeFromHardware()

Returns time (in s) since first CDPTimer was initialized (since application startup). For better performance (but also less accuracy), use CDPTime::GetGlobalTime(). CDPTime::GetGlobalTime() should not be used in threaded code because time returned by CDPTime::GetGlobalTime() is only updated before a Component's Process-function is called. In threaded code, CDPTime::GetGlobalTimeFromHardware() should be used.

[static] void CDPTime::GetTZandDSTinfo(int &TZnumbSecond, int &DST)

Get Timezone and Daylight saving information used/given by OS.

Parameter TZnumbSecondDifference in seconds, moving westward, between UTC and local time (if TZ is e.g. GMT+1, TZnumbSecond will get value -3600).
Parameter DSTNonzero if daylight savings time is currently in effect for the local time zone.

std::string CDPTime::GetTimeMsString(bool updateCurrentTimeFirst, bool UTC)

Returns the time in this format: "HH:MM:SS.mmm"

Returns string with time.

Parameter updateCurrentTimeFirstIf true, Update() is called first to get the current time/date.
Parameter UTCIf true, return-value will be in UTC time. If false, return value will be in localtime adjusted for TZ and DST. Internally, OSAPIGMTime() or OSAPICTime() is used respectively.

double CDPTime::GetTimeRelative(const char *time, const char *date, bool UTC)

Returns time in s from application startup to given time and date.

Will return timedifference between given time and date, and zeroTime. See valid formats for time and date in function CreateTime(). Internally, OSAPIMKTime() is used if UTC is false, and OSAPIMKGMTime() is used if UTC is true. See description of these functions.

Warning: If UTC is set to false, the result will be different depending on DST setting is 1 or not in the OS.

Parameter timeValid time string (see CreateTime())
Parameter dateValid date string (see CreateTime())
Parameter UTCIf true, given time/date is in UTC and there will be no adjustments for TZ and DST. If false, given time is in local time and it will be adjusted for TZ and DST (fetched from OS).

double CDPTime::GetTimeRelative()

Returns time in s relative to zeroTime for this application.

Will return timedifference between current time (*this), and zeroTime.

double CDPTime::GetTimeRelative(std::string &strDosTime, bool UTC)

Returns time in s from given time to current time.

Will return timedifference between current time in CDPTime class (only CDPTime members time and millitm are used), and given time (strDosTime). Internally, OSAPIMKTime() is used if UTC is false, and OSAPIMKGMTime() is used if UTC is true. See description of these functions.

Warning: If UTC is set to false, the result will be different depending on DST setting is 1 or not in the OS.

Parameter strDosTimeInput-parameter with this format: "mm/dd/yy hh:mm:ss:mls" (yy: will be interpreted as year in range [1970..2069], e.g. yy="10" means 2010)
Parameter UTCIf true, given time/date is in UTC and there will be no adjustments for TZ and DST. If false, given time is in local time and it will be adjusted for TZ and DST (fetched from OS).

[static] double CDPTime::GetTimeSince(const char *givenTimeStr, bool UTC)

Get the number of seconds between current time and the given time.

Internally, OSAPIMKTime() is used if UTC is false, and OSAPIMKGMTime() is used if UTC is true. See description of these functions.

Returns number of seconds between current time and givenTimeStr.

Warning: If UTC is set to false, the result will be different depending on DST setting is 1 or not in the OS.

Parameter givenTimeStrTime of format "MM/DD/YY HH:MM:SS.ZZZ" (yy: will be interpreted as year in range [1970..2069], e.g. yy="10" means 2010).
Parameter UTCIf true, given time/date is in UTC and there will be no adjustments for TZ and DST. If false, given time is in local time and it will be adjusted for TZ and DST (fetched from OS).

std::string CDPTime::GetTimeString(bool updateCurrentTimeFirst, bool UTC)

Returns the time in this format: "HH:MM:SS".

Returns string with time.

Parameter updateCurrentTimeFirstIf true, Update() is called first to get the current time/date.
Parameter UTCIf true, return-value will be in UTC time. If false, return value will be in localtime adjusted for TZ and DST. Internally, OSAPIGMTime() or OSAPICTime() is used respectively.

[static] const CDPTime &CDPTime::GetZeroTime()

Returns zeroTime (time of application startup, relative to 1.1.1970 UTC) May be adjusted by CDP, if clocksynch is enabled.

[static] double CDPTime::GlobalClock()

Returns number of seconds since 1.1.1970. Very accurate. The returned time is influenced by clocksynch, if enabled, which may cause discontinuity in returned values.

[static] double CDPTime::GlobalClockMs()

Returns number of seconds since 1.1.1970. Is using value returned by CDPTime::GetGlobalTime(), so CDPTime::GlobalClockMs() should not be used in threaded code, see note in CDPTime::GetGlobalTime(). The returned time is influenced by clocksynch, if enabled, which may cause discontinuity in returned values.

[static] std::string CDPTime::GlobalClockString(double secondsSince1970, int nrDecimals, bool UTC)

Convert number of seconds since 1970 to a time string.

Returns the time in format: "HH:MM:SS.mmmmmm".

Parameter secondsSince1970Input-parameter giving number of seconds since 1970.
Parameter nrDecimalsNumber of decimals to output to resulting string, from 0 to 20.
Parameter UTCIf true, return-value will be in UTC time. If false, return value will be in localtime adjusted for TZ and DST. Internally, OSAPIGMTime() or OSAPICTime() is used respectively.

[static] std::string CDPTime::GlobalClockString(int numberOfDecimals, bool UTC)

Returns the global clock (current time) as a time string.

Is using GlobalClock(), so returned time is influenced by clocksynch, if enabled. Returns the time in format: "HH:MM:SS.mmmmmm".

Parameter numberOfDecimalsNumber of decimals to output to resulting string, from 0 to 20.
Parameter UTCIf true, return-value will be in UTC time. If false, return value will be in localtime adjusted for TZ and DST. Internally, OSAPIGMTime() or OSAPICTime() is used respectively.

void CDPTime::SetFromDosTime(std::string strTime, bool UTC)

Sets the content of this CDPTime from strTime.

Internally, OSAPIMKTime() is used if UTC is false, and OSAPIMKGMTime() is used if UTC is true. See description of these functions.

Warning: If UTC is set to false, the result will be different depending on DST setting is 1 or not in the OS.

Parameter strTimeInput-parameter with this format: "mm/dd/yy hh:mm:ss:mls" (yy: will be interpreted as year in range [1970..2069], e.g. yy="10" means 2010).
Parameter UTCIf true, given time/date is in UTC and there will be no adjustments for TZ and DST. If false, given time is in local time and it will be adjusted for TZ and DST (fetched from OS).

CDPTime &CDPTime::Time()

Returns current value of CDPTime (is returning '*this').

void CDPTime::Update()

Updates the member variables time and millitm.

The updated values will contain current time: sum of zeroTime (time of application startup, relative to 1.1.1970) and cdpTimer.Time() (time since application startup). This method is called by the GetTime/Date...() functions if their first parameter is true.

void CDPTime::Update(double time)

Updates the member variables time and millitm.

The updated values will contain sum of zeroTime (time of application startup, relative to 1.1.1970) and input argument time.

Parameter timeInput-parameter giving time [s].

void CDPTime::Update(const char *time, const char *date, bool UTC)

Updates CDPTime's member variables time and millitm with values from the strings time and date.

See valid formats for time and date in function CreateTime().

Internally, OSAPIMKTime() is used if UTC is false, and OSAPIMKGMTime() is used if UTC is true. See description of these functions.

CDPTime's member variables timezone and dstflag is neither used nor changed.

Warning: If UTC is set to false, the result will be different depending on DST setting is 1 or not in the OS.

Parameter timeValid time string (see CreateTime())
Parameter dateValid date string (see CreateTime())
Parameter UTCIf true, given time/date is regarded as UTC and any settings for TZ and DST in OS will not be used/adjusted for.

If false: given time/date is regarded as local time and settings for TZ and DST in OS will be used/adjusted for.

void CDPTime::Update(const char *dateTime, bool UTC)

Updates CDPTime's member variables time and millitm with values from the string dateTime.

Valid formats

  • Fri Jan 21 2015 HH:MM:SS
  • Fri Jan 21 HH:MM:SS 2015

Internally, OSAPIMKTime() is used if UTC is false, and OSAPIMKGMTime() is used if UTC is true. See description of these functions.

CDPTime's member variables timezone and dstflag is neither used nor changed.

Warning: If UTC is set to false, the result will be different depending on DST setting is 1 or not in the OS.

Parameters dateTimeValid date and time string (see above for valid formats).
Parameter UTCIf true, given dateTime is regarded as UTC and any settings for TZ and DST in OS will not be used/adjusted for.

If false: given dateTime is regarded as local time and settings for TZ and DST in OS will be used/adjusted for.

[static] void CDPTime::Update(double time, CDPTime &timeToUpdate)

Updates the output-parameter 'timeToUpdate' with value equal zeroTime + 'time' input-parameter.

Parameter timeInput-parameter giving time [s] (often seconds since application start is used: cdpTimer.Time()).
Parameter timeToUpdateThe members timeToUpdate.time and timeToUpdate.millitm are updated. 'timeToUpdate' is calculated as zeroTime+input-parameter 'time'.

bool CDPTime::operator!=(const CDPTime &rhs)

Returns true if one of the non-static members are not equal.

CDPTime &CDPTime::operator-=(const CDPTime &rhs)

Subtracts rhs from this, returns *this.

CDPTime &CDPTime::operator=(const CDPTime &rhs)

Sets internal members from rhs, returns *this.

bool CDPTime::operator==(const CDPTime &rhs)

Returns true if all non-static members are equal.

Member Variable Documentation

CDPTimer CDPTime::cdpTimer

This variable holds the timer object used to read system time in CDP.

cdpTimer.Time() returns time [s] since application startup.

short CDPTime::dstflag

This variable holds currently not used. Nonzero if daylight time is currently active in the local time zone.

Should be set to 0 to indicate that time member is in UTC.

unsigned short CDPTime::millitm

This variable holds milliseconds part of time.

int64_t CDPTime::time

This variable holds number of seconds since 1.1.1970 UTC.

short CDPTime::timezone

This variable holds currently not used. Number of minutes between UTC and the local time (moving westward).

Should be set to 0 to indicate that time member is in UTC.

double CDPTime::zeroGlobalClock

This variable holds time of application startup [s], relative to 1.1.1970 (UTC).

May be adjusted by CDP, if clocksynch is enabled, which will affect clock returned by GlobalClock() and GlobalClockMs().

CDPTime CDPTime::zeroTime

This variable contains time of application startup, relative to 1.1.1970.

The member variables time and millitm contain UTC time. The member variables timezone and dstflag are set to 0. Initialized during startup. May be adjusted by CDP, if clocksynch is enabled. Is returned in function CDPTime::GetZeroTime().

Related Non-Members

enum CDPTimeFormat

This enum type specifies different string formats for CDPTime

ConstantValueDescription
CDP_DOS_Americanstatic_cast<int> ( CDPMeta::Type::Time::Format::DOS_American )01/17/11 08:45:32.510
CDP_UNIXstatic_cast<int> ( CDPMeta::Type::Time::Format::UNIX )Mon Jan 17 08:45:32.510 2011
CDP_DOS_American_No_Msstatic_cast<int> ( CDPMeta::Type::Time::Format::DOS_American_No_Ms )01/17/11 08:45:32
CDP_DOS_American_Datestatic_cast<int> ( CDPMeta::Type::Time::Format::DOS_American_Date )01/17/11
CDP_TimeOfDaystatic_cast<int> ( CDPMeta::Type::Time::Format::TimeOfDay )08:45:32.510
CDP_TimeOfDay_No_Msstatic_cast<int> ( CDPMeta::Type::Time::Format::TimeOfDay_No_Ms )08:45:32
CDP_UNIX_No_Msstatic_cast<int> ( CDPMeta::Type::Time::Format::UNIX_No_Ms )Mon Jan 17 08:45:32 2011
CDP_UNIX_No_TimeOfDaystatic_cast<int> ( CDPMeta::Type::Time::Format::UNIX_No_TimeOfDay )Mon Jan 17 2011
CDP_ISO_8601_With_Msstatic_cast<int> ( CDPMeta::Type::Time::Format::ISO_8601_With_Ms )2011-01-17 08:45:32.510
CDP_ISO_8601static_cast<int> ( CDPMeta::Type::Time::Format::ISO_8601 )2011-01-17 08:45:32
CDP_ISO_8601_No_TimeOfDaystatic_cast<int> ( CDPMeta::Type::Time::Format::ISO_8601_No_TimeOfDay )2011-01-17
CDP_ISO_8601_With_Ms_ShortYearstatic_cast<int> ( CDPMeta::Type::Time::Format::ISO_8601_With_Ms_ShortYear )11-01-17 08:45:32.510
CDP_ISO_8601_ShortYearstatic_cast<int> ( CDPMeta::Type::Time::Format::ISO_8601_ShortYear )11-01-17 08:45:32
CDP_ISO_8601_No_TimeOfDay_ShortYearstatic_cast<int> ( CDPMeta::Type::Time::Format::ISO_8601_No_TimeOfDay_ShortYear )11-01-17
CDP_Day_TimeShortMsstatic_cast<int> ( CDPMeta::Type::Time::Format::Day_TimeShortMs )17. 08:45:32.5

int OSAPIAsctime(char *buffer, const int numbElements, const tm *_tm)

Converts a tm time structure to a character string with date and time.

Format of string: "Wed Jan 02 02:03:55 1980".

Examples of output from OSAPIAsctime() is found in description for the functions OSAPIGMTime() and OSAPILocalTime().

Returns 0 if successful, or error code.

Parameter bufferWill contain the date and time string, or NULL if failed. Must be at least 26 characters large.
Parameter numbElementsSize of buffer. Must be at least 26.
Parameter _tmPointer to a tm time structure which we want to convert (the value of tm_isdst has no influence).

int OSAPICTime(char *buffer, const int numbElements, const int64_t *time)

Converts a int64_t time to local time character string, adjusted for timezone (TZ) and daylight savings time (DST) if present in OS.

Format of string: "Wed Jan 02 02:03:55 1980". In the examples below, Operating system is set up with timezone 'UTC+1' and automatically adjust clock for Daylight Saving Time.

Example1: If 'time' is 0, OSAPICTime() would return "Thu Jan 01 01:00:00 1970" (1 hour because of TZ and no summertime in January).

Example2: If 'time' is e.g. 1271252560, buffer would become "Wed Apr 14 15:42:40 2010".

Example3: If 'time' is e.g. 3600*24*365*46 + 3600*24*183, buffer would become "Tue Jun 21 02:00:00.000 2016". The corresponding string if using OSAPIGMTime() and OSAPIAsctime() would be "Tue Jun 21 00:00:00.000 2016". There is two hours time-difference between OSAPICTime() and OSAPIGMTime() because of +1 hour for TZ and +1 hour for daylight saving in June.

Example4: If 'time' is e.g. 3600*24*365*20, buffer would become "Wed Dec 27 01:00:00.000 1989". The corresponding string if using OSAPIGMTime() and OSAPIAsctime() would be "Wed Dec 27 00:00:00.000 1989". There is only 1 hour time-difference between OSAPICTime() and OSAPIGMTime() because of +1 hour for TZ (daylight saving not active in December).

Returns 0 if successful, or error code.

Parameter bufferWill contain the date and time string, or NULL if failed. Must be at least 26 characters large.
Parameter numbElementsSize of buffer. Must be at least 26.
Parameter timePointer to stored time which we want to convert.

int OSAPIGMTime(tm *_tm, const int64_t *time)

Converts time given by int64_t to tm*, NOT adjusted for timezone (TZ) and daylight savings time (DST).

Tip: OSAPIAsctime() can be used on this returnvalue (unless pointer is NULL) to convert the result to a string in this format: "Wed Jan 02 02:03:55 1980". In the examples below, Operating system is set up with timezone 'UTC+1' and automatically adjust clock for Daylight Saving Time.

Example1: If 'time' is 0, OSAPIGMTime() would return a struct corresponding to "Thu Jan 01 00:00:00 1970".

Example2: If 'time' is e.g. 1271252560, _tm's member would become: tm_sec=40, tm_min=42, tm_hour=13, tm_mday=14, tm_mon=3, tm_year=110, tm_wday=3, tm_yday=103, tm_isdst=0.

OSAPIAsctime() would give "Wed Apr 14 13:42:40 2010" for this _tm.

Returns 0 if successful, or error code.

Parameter _tmWill contain the converted tm structure (tm_isdst is always 0).
Parameter timePointer to stored time which we want to convert.

int OSAPILocalTime(tm *_tm, const int64_t *time)

Converts time given by int64_t to tm*, adjusted for timezone (TZ) and daylight savings time (DST).

This function behaves like OSAPICTime(), but result is found in tm *. In the example below, Operating system is set up with timezone 'UTC+1' and automatically adjust clock for Daylight Saving Time.

Example: If 'time' is e.g. 1271252560, _tm's member would become: tm_sec=40, tm_min=42, tm_hour=15, tm_mday=14, tm_mon=3, tm_year=110, tm_wday=3, tm_yday=103, tm_isdst=1.

OSAPIAsctime() would give "Wed Apr 14 15:42:40 2010" for this _tm.

Returns 0 if successful, or error code.

Parameter _tmWill contain the converted tm structure.
Parameter timePointer to stored time which we want to convert.

int64_t OSAPIMKGMTime(tm *time)

Converts UTC time (tm *time) to number of seconds elapsed since midnight, January 1, 1970 (UTC) (int64_t).

Example: If time corresponds to "01/01/70 01:00:00:000", the returned value will be 3600 sec independent of OS settings for TZ and DST, and independent of DST in specified time-struct.

Note: members in time may be changed, like tm_hour and tm_isdst.

Parameter timeInput-parameter giving UTC time.

int64_t OSAPIMKTime(tm *time)

Converts local time (tm *time) to number of seconds elapsed since midnight, January 1, 1970 (UTC) (int64_t).

If TZ in OS is 'GMT+1' and DST in specified time-struct is 0, one hour is first subtracted from 'time' (to get UTC) before the return-value is calculated (independent of OS settings for DST).

If TZ in OS is 'GMT+1' and DST in specified time-struct is 1, two hours are first subtracted from 'time' (to get UTC) before the return-value is calculated (independent of OS settings for DST).

If TZ in OS is 'GMT+1' and DST in specified time-struct is -1 (will then use DST from OS), one or two hours are first subtracted from 'time' (to get UTC) before the return-value is calculated, dependent of OS settings for DST and if it is summertime or not.

Example: If time corresponds to "01/01/70 01:00:00:000", and TZ in OS is 'GMT+1' and DST in specified time-struct is 0, the returned value will be 0 sec.

Note: members in time may be changed, like tm_hour and tm_isdst.

Parameter timeInput-parameter giving local time.

The content of this document is confidential information not to be published without the consent of CDP Technologies AS.

CDP Technologies AS, www.cdpstudio.com

Get started with CDP Studio today

Let us help you take your great ideas and turn them into the products your customer will love.

Try CDP Studio for free
Why CDP Studio?

CDP Technologies AS
Hundsværgata 8,
P.O. Box 144
6001 Ålesund, Norway

Tel: +47 990 80 900
E-mail: info@cdptech.com

Company

About CDP

Contact us

Services

Partners

Blog

Developers

Get started

User manuals

Support

Document download

Release notes

My account

Follow CDP

  • LinkedIn
  • YouTube
  • GitHub

© Copyright 2025 CDP Technologies. Privacy and cookie policy.

Return to top