Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Purpose:
- Author: Reece W.
- License: All Rights Reserved J. Reece Wilson (2018-01-12)
- License: Released under MIT License (https://en.wikipedia.org/wiki/MIT_License) (2018-06-26)
- */
- #include <xenus_lazy.h>
- #include <Utils\DateHelper.hpp>
- const char * __month_names[] = {
- "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
- };
- const char * __day_names[] = {
- "Monday", "Tuesday", "MedianDay", "Thursday", "Friday", "Saturday", "Sunday"
- };
- timezone * _sys_tz = nullptr;
- uint64_t _tz_offset = -1;
- bool __is_leap_year(int year);
- void __get_year(uint64_t ms, int * years_passed, int * days_passed);
- void __month_calc(int year, int day, int * out_month, int * out_days);
- uint64_t DateHelpers::get_timezone_offset()
- {
- if (!_sys_tz)
- _sys_tz = (timezone *)kallsyms_lookup_name("sys_tz");
- if (!_sys_tz)
- return 0;
- return _tz_offset = -_sys_tz->tz_minuteswest * 60000;
- }
- uint64_t DateHelpers::get_unix_time()
- {
- return NS_TO_MS(ktime_get_with_offset(TK_OFFS_REAL));
- }
- void DateHelpers::parse_time(uint64_t ms, time_info & timeinfo)
- {
- int year;
- int month;
- int days_start_month;
- int rem_days;
- int days_start_year;
- __get_year(ms, &year, &days_start_year);
- rem_days = (ms / 86400000) - days_start_year;
- __month_calc(year, rem_days, &month, &days_start_month);
- timeinfo.year = year;
- timeinfo.month.index = month;
- timeinfo.month.month = month + 1;
- timeinfo.month.name = __month_names[month];
- timeinfo.day.day_of_year = rem_days;
- timeinfo.day.day_of_week = ((ms % 604800000) / 86400000) - 3; //epoch starts on thursday
- timeinfo.day.day_of_month = (rem_days - days_start_month) + 1;
- timeinfo.day.index = timeinfo.day.day_of_week - 1;
- timeinfo.day.name = __day_names[timeinfo.day.index];
- timeinfo.time.hours = ((ms % 86400000) / 3600000);
- timeinfo.time.minutes = ((ms % 3600000) / 60000);
- timeinfo.time.seconds = ((ms % 60000) / 1000);
- timeinfo.time.milliseconds = (ms % 1000);
- }
- size_t DateHelpers::ISO8601_format(char * str, size_t length, time_info & time)
- {
- return snprintf(str, length,
- "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
- time.year,
- time.month.month,
- time.day.day_of_month,
- time.time.hours,
- time.time.minutes,
- time.time.seconds,
- time.time.milliseconds);
- }
- size_t DateHelpers::ISO8601_format(char * str, size_t length, uint64_t ms)
- {
- time_info time;
- DateHelpers::parse_time(ms, time);
- return ISO8601_format(str, length, time);
- }
- bool __is_leap_year(int year)
- {
- return ((year % 400 == 0 || year % 100 != 0) && (year % 4 == 0));
- }
- void __get_days_in_months(int * months, int year)
- {
- int month_cnt[] = {
- 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
- };
- if (__is_leap_year(year))
- month_cnt[1] = 29;
- memcpy(months, month_cnt, sizeof(month_cnt));
- }
- void __get_year(uint64_t ms, int * o_years_passed, int * o_days_passed)
- {
- int i;
- int32_t fuck;
- int32_t you;
- int32_t year;
- fuck = ms / 86400000;
- you = 0;
- year = 1970;
- while (fuck - (i = __is_leap_year(year) ? 366 : 365) >= 0)
- {
- fuck -= i;
- you += i;
- year ++;
- }
- *o_years_passed = year;
- *o_days_passed = you;
- }
- void __month_calc(int year, int day, int * out_month, int * out_days)
- {
- int months[12];
- int days = 0;
- int month = 0;
- __get_days_in_months((int *)&months, year);
- while (days + months[month] <= day)
- days += months[month++];
- *out_days = days;
- *out_month = month;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement