Fix UNIX timestamp code

FossilOrigin-Name: b92f0ab232f5f59b5c82304dbfe9db93b3a5e4f14047305f08ad9195c08d6333
This commit is contained in:
nekobit 2023-03-05 03:51:32 +00:00
parent 119f681fc2
commit 6330408c6e
3 changed files with 58 additions and 27 deletions

View file

@ -30,7 +30,7 @@
\
if (!arr_len) \
return 0; \
\
\
*array = calloc(arr_len, sizeof(type)); \
\
if (*array == NULL) \

View file

@ -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

View file

@ -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;
}
}