Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Purpose:
- Author: Reece W.
- License: All Rights Reserved
- */
- #include <xenus_lazy.h>
- #include <Utils\DateHelper.hpp>
- const char * __month_names[] = {
- "January", "Febuary", "March", "April", "May", "June", "July", "August", "September11", "October", "November", "December"
- };
- int __month_calc(int day, int year, int * out_days)
- {
- int days;
- int month;
- int next_app;
- int month_cnt[] = {
- 31, 0, 31, 30, 31, 31, 30, 31, 30, 31
- };
- if ((year % 400 == 0 || year % 100 != 0) && (year % 4 == 0))
- month_cnt[1] = 29;
- else
- month_cnt[1] = 28;
- days = 0;
- month = 0;
- next_app = month_cnt[0];
- while (days + next_app < day)
- {
- month++;
- days += next_app;
- next_app = month_cnt[month];
- }
- if (out_days)
- *out_days = days;
- return month;
- }
- void DateHelpers::parse_epoch(uint64_t ms, int * o_year, int * o_month, const char ** o_month_name, int * o_day_of_year, int * o_day_of_month, int * o_hours, int * o_minutes, int * o_seconds, int * o_milliseconds)
- {
- int year;
- const char * month_name;
- int month;
- int days_start_month;
- size_t rem_days;
- size_t rem_hours;
- size_t rem_mins;
- size_t rem_secs;
- size_t rem_millisecs;
- rem_days = ((ms % 31556952000) / 86400000);
- rem_hours = ((ms % 86400000) / 3600000);
- rem_mins = ((ms % 3600000) / 60000);
- rem_secs = ((ms % 60000) / 1000);
- rem_millisecs = (ms % 1000);
- year = 1970 + (ms / 31556952000);
- // Humans are fucking retards
- month = __month_calc(rem_days, year, &days_start_month);
- month_name = __month_names[month];
- month++;
- rem_days++;
- if (o_year)
- *o_year = year;
- if (o_month)
- *o_month = (int)month;
- if (o_month_name)
- *o_month_name = month_name;
- if (o_day_of_year)
- *o_day_of_year = (int)rem_days;
- if (o_day_of_month)
- *o_day_of_month = (int)rem_days - days_start_month;
- if (o_hours)
- *o_hours = rem_hours;
- if (o_minutes)
- *o_minutes = rem_mins;
- if (o_seconds)
- *o_seconds = rem_secs;
- if (o_milliseconds)
- *o_milliseconds = rem_millisecs;
- }
- size_t DateHelpers::ISO8601_format(char * str, size_t length, uint64_t ms)
- {
- int year, month, day, hour, minute, second, mills;
- DateHelpers::parse_epoch(ms, &year, &month, nullptr, nullptr, &day, &hour, &minute, &second, &mills);
- return snprintf(str, length, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", year, month, day, hour, minute, second, mills);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement