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
}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through resources like this. We hope you find it helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support here. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *