UTC is the DateTime displayed in the following format:
yyyy-mm-ddThh:mm:ss
yyyy-mm-dd hh:mm:ss
Convert UTC char string to tm and tm to a UTC formatted char string
#include <stdio.h>
#include <time.h>
struct tm Tm1 = {0};
char TextBuffer[50];
//Convert UTC string to tm
char MyUtcString[50] = "2020-08-01 11:05:13";
if (strptime(MyUtcString, "%Y-%m-%d %H:%M:%S", &Tm1) == NULL) //Convert the character string pointed to by buf to values which are stored in the tm structure pointed to by tm, using the format specified by format
printf("strptime Failed\n");
//The == NULL check will trigger if all the values aren't present, e.g. if you give a "yyyy-mm-dd hh:mm" string with a "%Y-%m-%d %H:%M:%S" format specifier it will return NULL because the seconds are missing
//Convert tm to string
strftime(TextBuffer, sizeof(TextBuffer), "%Y-%m-%d %H:%M:%S", &Tm1);
printf("UTC: %s\n", TextBuffer);
Note, strftime() returns the total number of characters written (not including the terminating null-character), or zero on error. To get a pointer to the next character in the char string being written you can use this:
#include <stdio.h>
#include <time.h>
//*****************************************************
//*****************************************************
//********** ADD UTC DATETIME TO CHAR STRING **********
//*****************************************************
//*****************************************************
char *AddUtcDateTimeToCharString (struct tm tmDateTime, char *pDest, char *pDestBufferEnd)
{
int Count;
Count = strftime(pDest, ((pDestBufferEnd - pDest) + 1), "%Y-%m-%d %H:%M:%S", &tmDateTime); //Get timestamp
//Count = the total number of characters copied to ptr (not including the terminating null-character), or zero on error
if (Count > 0)
return(pDest + Count);
else
return(pDest); //An error occured
}
