Skip to content

Instantly share code, notes, and snippets.

@kosmakoff
Created May 11, 2016 16:58
Show Gist options
  • Save kosmakoff/4adfddcf588d5179987525f80923ac6a to your computer and use it in GitHub Desktop.
Save kosmakoff/4adfddcf588d5179987525f80923ac6a to your computer and use it in GitHub Desktop.
Showing plist time issue, C version
#include <plist/plist.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAC_EPOCH 978307200
static void parse_date(const char *strval, struct tm *btime)
{
if (!btime) return;
memset(btime, 0, sizeof(struct tm));
if (!strval) return;
#ifdef strptime
strptime((char*)strval, "%Y-%m-%dT%H:%M:%SZ", btime);
#else
sscanf(strval, "%d-%d-%dT%d:%d:%dZ", &btime->tm_year, &btime->tm_mon, &btime->tm_mday, &btime->tm_hour, &btime->tm_min, &btime->tm_sec);
btime->tm_year-=1900;
btime->tm_mon--;
#endif
btime->tm_isdst=0;
}
int main()
{
char* xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
"<plist version=\"1.0\">\n"
"<dict>\n"
"<key>Last Backup Date</key>\n"
"<date>2016-04-06T10:16:17Z</date>\n"
"</dict>\n"
"</plist>\n";
printf("The xml is:\n%s\n==================\n", xml);
plist_t dict = NULL;
plist_from_xml(xml, strlen(xml), &dict);
plist_t datetimeNode = plist_dict_get_item(dict, "Last Backup Date");
int32_t sec, usec;
plist_get_date_val(datetimeNode, &sec, &usec);
printf("The date in secs = %d\n", sec);
plist_free(dict);
char* time = "2016-04-06T10:16:17Z";
struct tm btime;
struct tm* tm_utc;
time_t timev = 0;
parse_date((const char*)time, &btime);
timev = mktime(&btime);
tm_utc = gmtime(&timev);
timev -= (mktime(tm_utc) - timev);
printf("Another time: %ld\n", (long)(timev - MAC_EPOCH));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment