C
C
How to Parse a TimeStamp (such as 2016-11-11T14:32:17.0908971Z)
Timestamps are frequently used in REST API responses. This example demonstrates how to parse a timestamp string to get at the date/time components in the local timezone or in the GMT/UTC timezone.Chilkat C Downloads
#include <C_CkDateTime.h>
#include <C_CkDtObj.h>
void ChilkatSample(void)
{
const char *strTimestamp;
HCkDateTime dateTime;
BOOL bLocalTimezone;
HCkDtObj dt;
// Let's say we have a timestamp string such as 2016-11-11T14:32:17.0908971Z
strTimestamp = "2016-11-11T14:32:17.0908971Z";
dateTime = CkDateTime_Create();
CkDateTime_SetFromTimestamp(dateTime,strTimestamp);
// Get a DtObj in the local timezone.
bLocalTimezone = TRUE;
dt = CkDtObj_Create();
CkDateTime_ToDtObj(dateTime,bLocalTimezone,dt);
// Get the individual date/time components
printf("-- Local Time --\n");
printf("Year: %d\n",CkDtObj_getYear(dt));
printf("Month: %d\n",CkDtObj_getMonth(dt));
printf("Day: %d\n",CkDtObj_getDay(dt));
printf("Hour: %d\n",CkDtObj_getHour(dt));
printf("Minutes: %d\n",CkDtObj_getMinute(dt));
printf("Seconds: %d\n",CkDtObj_getSecond(dt));
// Get a DtObj in the GMT/UTC timezone.
bLocalTimezone = FALSE;
CkDateTime_ToDtObj(dateTime,bLocalTimezone,dt);
// Get the individual date/time components
printf("-- UTC Time --\n");
printf("Year: %d\n",CkDtObj_getYear(dt));
printf("Month: %d\n",CkDtObj_getMonth(dt));
printf("Day: %d\n",CkDtObj_getDay(dt));
printf("Hour: %d\n",CkDtObj_getHour(dt));
printf("Minutes: %d\n",CkDtObj_getMinute(dt));
printf("Seconds: %d\n",CkDtObj_getSecond(dt));
CkDateTime_Dispose(dateTime);
CkDtObj_Dispose(dt);
}