C
C
JSON Date Parsing
See more JSON Examples
Demonstrates how to parse date/time strings from JSON.Note: This example uses the DtOf and DateOf methods introduced in Chilkat v9.5.0.73
Chilkat C Downloads
#include <C_CkJsonObject.h>
#include <C_CkDateTime.h>
#include <C_CkDtObj.h>
void ChilkatSample(void)
{
BOOL success;
HCkJsonObject json;
HCkDateTime dateTime;
HCkDtObj dt;
BOOL getAsLocal;
success = FALSE;
json = CkJsonObject_Create();
CkJsonObject_putEmitCompact(json,FALSE);
// First, let's create JSON containing some date/time strings.
CkJsonObject_UpdateString(json,"test.timestamp","2018-01-30T20:35:00Z");
CkJsonObject_UpdateString(json,"test.rfc822","Tue, 24 Apr 2018 08:47:03 -0500");
CkJsonObject_UpdateString(json,"test.dateStrings[0]","2018-01-30T20:35:00Z");
CkJsonObject_UpdateString(json,"test.dateStrings[1]","Tue, 24 Apr 2018 08:47:03 -0500");
CkJsonObject_UpdateNumber(json,"test.StartLoggingTime","1446834998.695");
CkJsonObject_UpdateNumber(json,"test.Expiration","1442877512.0");
CkJsonObject_UpdateInt(json,"test.StartTime",1518867432);
printf("%s\n",CkJsonObject_emit(json));
// We've built the following JSON:
// {
// "test": {
// "timestamp": "2018-01-30T20:35:00Z",
// "rfc822": "Tue, 24 Apr 2018 08:47:03 -0500",
// "dateStrings": [
// "2018-01-30T20:35:00Z",
// "Tue, 24 Apr 2018 08:47:03 -0500"
// ],
// "StartLoggingTime": 1446834998.695,
// "Expiration": 1442877512.0,
// "StartTime": 1518867432
// }
// }
// Use the DateOf and DtOf methods to load Chilkat date/time objects with the date/time values.
// The CkDateTime object is primarily for loading a date/time from numerous formats, and then getting
// the date/time in various formats. Thus, it's primarly for date/time format conversion.
// The DtObj object holds a date/time where the individual components (day, month, year, hour, minutes, etc.) are
// immediately accessible as integers.
dateTime = CkDateTime_Create();
dt = CkDtObj_Create();
getAsLocal = FALSE;
// Load the date/time at test.timestamp into the dateTime object.
success = CkJsonObject_DateOf(json,"test.timestamp",dateTime);
printf("%s\n",CkDateTime_getAsTimestamp(dateTime,getAsLocal));
printf("%u\n",CkDateTime_GetAsUnixTime(dateTime,FALSE));
printf("%s\n",CkDateTime_getAsRfc822(dateTime,getAsLocal));
success = CkJsonObject_DateOf(json,"test.rfc822",dateTime);
printf("%s\n",CkDateTime_getAsTimestamp(dateTime,getAsLocal));
CkJsonObject_putI(json,0);
success = CkJsonObject_DateOf(json,"test.dateStrings[i]",dateTime);
printf("%s\n",CkDateTime_getAsTimestamp(dateTime,getAsLocal));
CkJsonObject_putI(json,1);
success = CkJsonObject_DateOf(json,"test.dateStrings[i]",dateTime);
printf("%s\n",CkDateTime_getAsTimestamp(dateTime,getAsLocal));
success = CkJsonObject_DateOf(json,"test.StartLoggingTime",dateTime);
printf("%s\n",CkDateTime_getAsTimestamp(dateTime,getAsLocal));
success = CkJsonObject_DateOf(json,"test.Expiration",dateTime);
printf("%s\n",CkDateTime_getAsTimestamp(dateTime,getAsLocal));
success = CkJsonObject_DateOf(json,"test.StartTime",dateTime);
printf("%s\n",CkDateTime_getAsTimestamp(dateTime,getAsLocal));
// Output so far:
// 2018-01-30T20:35:00Z
// 1517344500
// Tue, 30 Jan 2018 20:35:00 GMT
// 2018-04-24T13:47:03Z
// 2018-01-30T20:35:00Z
// 2018-04-24T13:47:03Z
// 2015-11-07T00:36:38Z
// 2015-09-22T04:18:32Z
// 2018-02-17T17:37:12Z
// Now load the date/time strings into the dt object:
success = CkJsonObject_DtOf(json,"test.timestamp",getAsLocal,dt);
printf("month=%d, day=%d, year=%d, hour=%d, minute=%d\n",CkDtObj_getMonth(dt),CkDtObj_getDay(dt),CkDtObj_getYear(dt),CkDtObj_getHour(dt)
,CkDtObj_getMinute(dt));
success = CkJsonObject_DtOf(json,"test.rfc822",getAsLocal,dt);
printf("month=%d, day=%d, year=%d, hour=%d, minute=%d\n",CkDtObj_getMonth(dt),CkDtObj_getDay(dt),CkDtObj_getYear(dt),CkDtObj_getHour(dt)
,CkDtObj_getMinute(dt));
CkJsonObject_putI(json,0);
success = CkJsonObject_DtOf(json,"test.dateStrings[i]",getAsLocal,dt);
printf("month=%d, day=%d, year=%d, hour=%d, minute=%d\n",CkDtObj_getMonth(dt),CkDtObj_getDay(dt),CkDtObj_getYear(dt),CkDtObj_getHour(dt)
,CkDtObj_getMinute(dt));
CkJsonObject_putI(json,1);
success = CkJsonObject_DtOf(json,"test.dateStrings[i]",getAsLocal,dt);
printf("month=%d, day=%d, year=%d, hour=%d, minute=%d\n",CkDtObj_getMonth(dt),CkDtObj_getDay(dt),CkDtObj_getYear(dt),CkDtObj_getHour(dt)
,CkDtObj_getMinute(dt));
success = CkJsonObject_DtOf(json,"test.StartLoggingTime",getAsLocal,dt);
printf("month=%d, day=%d, year=%d, hour=%d, minute=%d\n",CkDtObj_getMonth(dt),CkDtObj_getDay(dt),CkDtObj_getYear(dt),CkDtObj_getHour(dt)
,CkDtObj_getMinute(dt));
success = CkJsonObject_DtOf(json,"test.Expiration",getAsLocal,dt);
printf("month=%d, day=%d, year=%d, hour=%d, minute=%d\n",CkDtObj_getMonth(dt),CkDtObj_getDay(dt),CkDtObj_getYear(dt),CkDtObj_getHour(dt)
,CkDtObj_getMinute(dt));
success = CkJsonObject_DtOf(json,"test.StartTime",getAsLocal,dt);
printf("month=%d, day=%d, year=%d, hour=%d, minute=%d\n",CkDtObj_getMonth(dt),CkDtObj_getDay(dt),CkDtObj_getYear(dt),CkDtObj_getHour(dt)
,CkDtObj_getMinute(dt));
// Output:
// month=1, day=30, year=2018, hour=20, minute=35
// month=4, day=24, year=2018, hour=13, minute=47
// month=1, day=30, year=2018, hour=20, minute=35
// month=4, day=24, year=2018, hour=13, minute=47
// month=11, day=6, year=2015, hour=18, minute=36
// month=9, day=21, year=2015, hour=23, minute=18
// month=2, day=17, year=2018, hour=11, minute=37
CkJsonObject_Dispose(json);
CkDateTime_Dispose(dateTime);
CkDtObj_Dispose(dt);
}