S2OPC OPCUA Toolkit
Data Structures | Typedefs | Functions
sopc_time.h File Reference
#include "sopc_platform_time.h"
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include "sopc_enums.h"

Go to the source code of this file.

Data Structures

struct  SOPC_tm
 S2OPC equivalent of standard struct tm. Fields use fix-length numerical values. More...
 

Typedefs

typedef struct SOPC_tm SOPC_tm
 S2OPC equivalent of standard struct tm. Fields use fix-length numerical values. More...
 

Functions

char * SOPC_Time_GetString (int64_t time, bool local, bool compact)
 returns a C string representation of the given time in DateTime format E.g.: More...
 
char * SOPC_Time_GetStringOfCurrentLocalTime (bool compact)
 return the current local time as a C String, e.g.: More...
 
char * SOPC_Time_GetStringOfCurrentTimeUTC (bool compact)
 return the current UTC time as a C String, e.g.: More...
 
SOPC_TimeReference SOPC_TimeReference_AddMilliseconds (SOPC_TimeReference timeRef, uint64_t ms)
 return the time reference corresponding to the given time reference incremented by the given duration in milliseconds More...
 
int8_t SOPC_TimeReference_Compare (SOPC_TimeReference left, SOPC_TimeReference right)
 return the comparison of given time references More...
 
SOPC_ReturnStatus SOPC_Time_FromTimeT (time_t time, int64_t *res)
 Converts a UNIX timestamp to a time expressed in 100ns slices since 1601/01/01 00:00:00 UTC. More...
 
SOPC_ReturnStatus SOPC_Time_ToTimeT (int64_t dt, time_t *res)
 Converts a time expressed in 100ns slices since 1601/01/01 00:00:00 UTC to a UNIX timestamp. More...
 
bool SOPC_tm_FromXsdDateTime (const char *datetime, size_t len, SOPC_tm *tm)
 Parse a string containing an XSD format datetime '[YYYY]-[MM]-[DD]T[hh]:[mm]:[ss]', it might be followed by 'Z' to indicate UTC timezone or '+/-[hh]:[mm]' for an offset of UTC. See https://www.w3.org/TR/2012/REC-xmlschema11-2-20120405/datatypes.html#dateTime, the equivalent regular expression is: -?([1-9][0-9]{3,}|0[0-9]{3}) -(0[1-9]|1[0-2]) -(0[1-9]|[12][0-9]|3[01]) T(([01][0-9]|2[0-3]):[0-5][0-9]:[0-5]0-9?|(24:00:00(.0+)?)) (Z|(+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00))? More...
 
SOPC_ReturnStatus SOPC_Time_FromXsdDateTime (const char *dateTime, size_t len, int64_t *res)
 Converts a string using XSD DateTime format (see SOPC_tm_FromXsdDateTime) to a time expressed in 100ns slices since 1601/01/01 00:00:00 UTC. More...
 

Typedef Documentation

◆ SOPC_tm

typedef struct SOPC_tm SOPC_tm

S2OPC equivalent of standard struct tm. Fields use fix-length numerical values.

Function Documentation

◆ SOPC_Time_GetString()

char* SOPC_Time_GetString ( int64_t  time,
bool  local,
bool  compact 
)

returns a C string representation of the given time in DateTime format E.g.:

  • compact == false: "2018/01/30 13:15:52.694\0"
  • compact == true: "20180130_131552_694\0"
Parameters
timetime value in DataTime format, which is 100 nanoseconds from 1601/01/01 00:00:00 UTC
localprovides local time if set, UTC time otherwise
compactprovides compact version when flag is set

◆ SOPC_Time_GetStringOfCurrentLocalTime()

char* SOPC_Time_GetStringOfCurrentLocalTime ( bool  compact)

return the current local time as a C String, e.g.:

  • compact == false: "2018/01/30 13:15:52.694\0"
  • compact == true: "20180130_131552_694\0"
Parameters
compactprovides compact version when flag is set
Returns
the current local time as C string (to be deallocated after use)

◆ SOPC_Time_GetStringOfCurrentTimeUTC()

char* SOPC_Time_GetStringOfCurrentTimeUTC ( bool  compact)

return the current UTC time as a C String, e.g.:

  • compact == false: "2018/01/30 13:15:52.694\0"
  • compact == true: "20180130_131552_694\0"
Parameters
compactprovides compact version when flag is set
Returns
the current UTC time as C string (to be deallocated after use)

◆ SOPC_TimeReference_AddMilliseconds()

SOPC_TimeReference SOPC_TimeReference_AddMilliseconds ( SOPC_TimeReference  timeRef,
uint64_t  ms 
)

return the time reference corresponding to the given time reference incremented by the given duration in milliseconds

Parameters
timeRefthe time reference to be incremented
msthe duration in milliseconds to use for increment
Returns
the new time reference incremented by the given duration or with the maximum value in case of overflow or NULL incase timerRef == NULL or new memory allocation failed

◆ SOPC_TimeReference_Compare()

int8_t SOPC_TimeReference_Compare ( SOPC_TimeReference  left,
SOPC_TimeReference  right 
)

return the comparison of given time references

Parameters
leftthe left time reference operand (NULL pointer considered less than any other value)
rightthe right time reference operand (NULL pointer considered less than any other value)
Returns
-1 if left < right operand, 0 if left = right and 1 if left > right

◆ SOPC_Time_FromTimeT()

SOPC_ReturnStatus SOPC_Time_FromTimeT ( time_t  time,
int64_t *  res 
)

Converts a UNIX timestamp to a time expressed in 100ns slices since 1601/01/01 00:00:00 UTC.

Parameters
timethe UNIX timestamp
resthe resulting time
Returns
SOPC_STATUS_OK on success, an error code on failure

◆ SOPC_Time_ToTimeT()

SOPC_ReturnStatus SOPC_Time_ToTimeT ( int64_t  dt,
time_t *  res 
)

Converts a time expressed in 100ns slices since 1601/01/01 00:00:00 UTC to a UNIX timestamp.

Parameters
dtthe input time
resthe resulting time_t
Returns
SOPC_STATUS_OK on success, an error code on failure

◆ SOPC_tm_FromXsdDateTime()

bool SOPC_tm_FromXsdDateTime ( const char *  datetime,
size_t  len,
SOPC_tm tm 
)

Parse a string containing an XSD format datetime '[YYYY]-[MM]-[DD]T[hh]:[mm]:[ss]', it might be followed by 'Z' to indicate UTC timezone or '+/-[hh]:[mm]' for an offset of UTC. See https://www.w3.org/TR/2012/REC-xmlschema11-2-20120405/datatypes.html#dateTime, the equivalent regular expression is: -?([1-9][0-9]{3,}|0[0-9]{3}) -(0[1-9]|1[0-2]) -(0[1-9]|[12][0-9]|3[01]) T(([01][0-9]|2[0-3]):[0-5][0-9]:[0-5]0-9?|(24:00:00(.0+)?)) (Z|(+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00))?

Note
Output NULL pointers are not set and do not lead to return function failure
Parameters
datetimeThe string containing a XSD datetime value to parse
lenThe length of the the string (strlen(datetime) if datetime is a C string)
tmPointer to broken-down time structure used as output parameter. It is filled in case of success.
Returns
Returns true if the datetime parsing succeeded and len characters parsed, false otherwise

◆ SOPC_Time_FromXsdDateTime()

SOPC_ReturnStatus SOPC_Time_FromXsdDateTime ( const char *  dateTime,
size_t  len,
int64_t *  res 
)

Converts a string using XSD DateTime format (see SOPC_tm_FromXsdDateTime) to a time expressed in 100ns slices since 1601/01/01 00:00:00 UTC.

Warning
This function uses similar limitation as indicated for encoding for OPC UA binary DateTime (see part 6):
  • A date/time value is encoded as 0 if the year is earlier than 1601
  • A date/time is encoded as the maximum value for an Int64 if the year is greater than 9999 Since those values cannot be encoded as indicated by OPC UA standard, there is no need to manage converting those.
This function considers leap days but not leap seconds since it is not requested by OPC UA specification (see part 3 and 6). It means each day is considered to be 86400 seconds without considering any leap seconds.
Parameters
dateTimeThe string containing a XSD datetime value to parse
lenThe length of the the string (strlen(datetime) if datetime is a C string)
resThe resulting time
Returns
SOPC_STATUS_OK on success, an error code on failure