From 6330408c6e861cd0e17e8f21b4832e19579529b6 Mon Sep 17 00:00:00 2001 From: nekobit Date: Sun, 5 Mar 2023 03:51:32 +0000 Subject: [PATCH] Fix UNIX timestamp code FossilOrigin-Name: b92f0ab232f5f59b5c82304dbfe9db93b3a5e4f14047305f08ad9195c08d6333 --- include/mastodont_generate.h | 2 +- src/json_helper.c | 57 +++++++++++++++++++++++------------- tests/get_feed.c | 26 ++++++++++++---- 3 files changed, 58 insertions(+), 27 deletions(-) diff --git a/include/mastodont_generate.h b/include/mastodont_generate.h index 2f5e3c3..560adaf 100644 --- a/include/mastodont_generate.h +++ b/include/mastodont_generate.h @@ -30,7 +30,7 @@ \ if (!arr_len) \ return 0; \ - \ + \ *array = calloc(arr_len, sizeof(type)); \ \ if (*array == NULL) \ diff --git a/src/json_helper.c b/src/json_helper.c index 1f91608..34e70d2 100644 --- a/src/json_helper.c +++ b/src/json_helper.c @@ -55,34 +55,51 @@ void _mstdnt_val_string_unix_call(cJSON* v, void* _type) *type = v->valuestring != endptr ? conv : 0; } +static time_t +time_to_seconds(struct tm* time) +{ + int const mday = time->tm_mday + 1; + int const ly = (mday % 4 == 0 && mday % 100 != 0) || mday % 400 == 0 ? 28 : 29; + int const days_past_per_month[] = { + 31, + 31+ly, + 31+ly+31, + 31+ly+31+30, + 31+ly+31+30+31, /* >-\ */ + 31+ly+31+30+31+30, /* :( */ + 31+ly+31+30+31+30+31, + 31+ly+31+30+31+30+31+31, + 31+ly+31+30+31+30+31+31+30, + 31+ly+31+30+31+30+31+31+30+31, + 31+ly+31+30+31+30+31+31+30+31+30, + 31+ly+31+30+31+30+31+31+30+31+30+31, /* >->-O */ + }; + return (time->tm_year * 60 * 60 * 24 * 365) + + (days_past_per_month[time->tm_mon] * 60 * 60 * 24) + + (time->tm_mday * 60 * 60 * 24) + + (time->tm_hour * 60 * 60) + + (time->tm_min * 60) + + time->tm_sec; +} + void _mstdnt_val_datetime_unix_call(cJSON* v, void* _type) { - // First, assure correct time properties like DST - time_t loc_time = time(NULL); - // Note: Not thread safe? This is a static pointer returned - struct tm* conv_time = gmtime(&loc_time); + struct tm conv_time; time_t* type = _type; if (sscanf(v->valuestring, "%d-%d-%dT%d:%d:%d.000Z", - &conv_time->tm_year, - &conv_time->tm_mon, - &conv_time->tm_mday, - &conv_time->tm_hour, - &conv_time->tm_min, - &conv_time->tm_sec) == 6) + &conv_time.tm_year, + &conv_time.tm_mon, + &conv_time.tm_mday, + &conv_time.tm_hour, + &conv_time.tm_min, + &conv_time.tm_sec) == 6) { - // ?????? - conv_time->tm_year -= 1900; - conv_time->tm_mon -= 1; - // TODO -#if 0 - // not portable - *type = mktime(&conv_time) - timezone; -#endif - *type = mktime(&conv_time); + conv_time.tm_year -= 1970; + *type = time_to_seconds(&conv_time); } else - *type = 0; // 70's, baby! + *type = 0; // Non-conforming server. Shouldn't really happen } // Fuck you Gargron diff --git a/tests/get_feed.c b/tests/get_feed.c index ff2a067..5b90740 100644 --- a/tests/get_feed.c +++ b/tests/get_feed.c @@ -24,6 +24,12 @@ char* get_line(char const* prompt) return result; } +static char const* +itob(int i) +{ + return i ? "true" : "false"; +} + int tl_callback(mstdnt_request_cb_data* cb_data, void* args) { @@ -33,7 +39,15 @@ tl_callback(mstdnt_request_cb_data* cb_data, void* args) { struct mstdnt_status* status = statuses->statuses + i; puts("---------------- BEGIN STATUS ----------------"); - printf(" Author: %s", status->account.username); + printf(" Author: %s\n", status->account.username); + printf(" id: %s\n", status->id); + printf(" R/F/REPLIES: %u %u %u\n", status->reblogs_count, + status->favourites_count, + status->replies_count); + printf(" Is Reply? %s\n", itob(status->in_reply_to_id != NULL)); + printf(" Contains emojos? %s\n", itob(status->emojis_len > 0)); + printf(" Has attachments? %s\n", itob(status->media_attachments_len > 0)); + puts("----------------- END STATUS -----------------"); } @@ -63,14 +77,14 @@ main(int argc, char** argv) .flags = 0, }; - mstdnt_timeline_public(&data, &m_args, tl_callback, NULL, (struct mstdnt_timeline_args){}); + mstdnt_timeline_public(&data, &m_args, tl_callback, NULL, (struct mstdnt_timeline_args){.limit=20}); mstdnt_await(&data, 0, NULL, 0); // Cleanup - free(instance); - mstdnt_cleanup(&data); - mstdnt_global_curl_cleanup(); + //free(instance); + //mstdnt_cleanup(&data); + //mstdnt_global_curl_cleanup(); return 1; -} \ No newline at end of file +}