Advertisement
ItsTotallyRSX

I hate time

Jun 26th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. /*
  2. Purpose:
  3. Author: Reece W.
  4. License: All Rights Reserved J. Reece Wilson (2018-01-12)
  5. License: Released under MIT License (https://en.wikipedia.org/wiki/MIT_License) (2018-06-26)
  6. */
  7. #include <xenus_lazy.h>
  8. #include <Utils\DateHelper.hpp>
  9.  
  10. const char * __month_names[] = {
  11. "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
  12. };
  13.  
  14. const char * __day_names[] = {
  15. "Monday", "Tuesday", "MedianDay", "Thursday", "Friday", "Saturday", "Sunday"
  16. };
  17.  
  18. timezone * _sys_tz = nullptr;
  19. uint64_t _tz_offset = -1;
  20.  
  21. bool __is_leap_year(int year);
  22. void __get_year(uint64_t ms, int * years_passed, int * days_passed);
  23. void __month_calc(int year, int day, int * out_month, int * out_days);
  24.  
  25. uint64_t DateHelpers::get_timezone_offset()
  26. {
  27. if (!_sys_tz)
  28. _sys_tz = (timezone *)kallsyms_lookup_name("sys_tz");
  29.  
  30. if (!_sys_tz)
  31. return 0;
  32.  
  33. return _tz_offset = -_sys_tz->tz_minuteswest * 60000;
  34. }
  35.  
  36. uint64_t DateHelpers::get_unix_time()
  37. {
  38. return NS_TO_MS(ktime_get_with_offset(TK_OFFS_REAL));
  39. }
  40.  
  41.  
  42. void DateHelpers::parse_time(uint64_t ms, time_info & timeinfo)
  43. {
  44. int year;
  45. int month;
  46. int days_start_month;
  47. int rem_days;
  48. int days_start_year;
  49.  
  50. __get_year(ms, &year, &days_start_year);
  51.  
  52. rem_days = (ms / 86400000) - days_start_year;
  53.  
  54. __month_calc(year, rem_days, &month, &days_start_month);
  55.  
  56. timeinfo.year = year;
  57.  
  58. timeinfo.month.index = month;
  59. timeinfo.month.month = month + 1;
  60. timeinfo.month.name = __month_names[month];
  61.  
  62. timeinfo.day.day_of_year = rem_days;
  63. timeinfo.day.day_of_week = ((ms % 604800000) / 86400000) - 3; //epoch starts on thursday
  64. timeinfo.day.day_of_month = (rem_days - days_start_month) + 1;
  65. timeinfo.day.index = timeinfo.day.day_of_week - 1;
  66. timeinfo.day.name = __day_names[timeinfo.day.index];
  67.  
  68. timeinfo.time.hours = ((ms % 86400000) / 3600000);
  69. timeinfo.time.minutes = ((ms % 3600000) / 60000);
  70. timeinfo.time.seconds = ((ms % 60000) / 1000);
  71. timeinfo.time.milliseconds = (ms % 1000);
  72. }
  73.  
  74. size_t DateHelpers::ISO8601_format(char * str, size_t length, time_info & time)
  75. {
  76. return snprintf(str, length,
  77. "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
  78. time.year,
  79. time.month.month,
  80. time.day.day_of_month,
  81.  
  82. time.time.hours,
  83. time.time.minutes,
  84. time.time.seconds,
  85. time.time.milliseconds);
  86. }
  87.  
  88. size_t DateHelpers::ISO8601_format(char * str, size_t length, uint64_t ms)
  89. {
  90. time_info time;
  91. DateHelpers::parse_time(ms, time);
  92. return ISO8601_format(str, length, time);
  93. }
  94.  
  95. bool __is_leap_year(int year)
  96. {
  97. return ((year % 400 == 0 || year % 100 != 0) && (year % 4 == 0));
  98. }
  99.  
  100. void __get_days_in_months(int * months, int year)
  101. {
  102. int month_cnt[] = {
  103. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  104. };
  105.  
  106. if (__is_leap_year(year))
  107. month_cnt[1] = 29;
  108.  
  109. memcpy(months, month_cnt, sizeof(month_cnt));
  110. }
  111.  
  112. void __get_year(uint64_t ms, int * o_years_passed, int * o_days_passed)
  113. {
  114. int i;
  115. int32_t fuck;
  116. int32_t you;
  117. int32_t year;
  118.  
  119. fuck = ms / 86400000;
  120. you = 0;
  121. year = 1970;
  122. while (fuck - (i = __is_leap_year(year) ? 366 : 365) >= 0)
  123. {
  124. fuck -= i;
  125. you += i;
  126. year ++;
  127. }
  128. *o_years_passed = year;
  129. *o_days_passed = you;
  130. }
  131.  
  132. void __month_calc(int year, int day, int * out_month, int * out_days)
  133. {
  134. int months[12];
  135. int days = 0;
  136. int month = 0;
  137.  
  138. __get_days_in_months((int *)&months, year);
  139.  
  140. while (days + months[month] <= day)
  141. days += months[month++];
  142.  
  143. *out_days = days;
  144. *out_month = month;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement