Advertisement
ItsTotallyRSX

Untitled

Jun 26th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. /*
  2. Purpose:
  3. Author: Reece W.
  4. License: All Rights Reserved
  5. */
  6. #include <xenus_lazy.h>
  7.  
  8. #include <Utils\DateHelper.hpp>
  9.  
  10. const char * __month_names[] = {
  11. "January", "Febuary", "March", "April", "May", "June", "July", "August", "September11", "October", "November", "December"
  12. };
  13.  
  14. int __month_calc(int day, int year, int * out_days)
  15. {
  16. int days;
  17. int month;
  18. int next_app;
  19.  
  20. int month_cnt[] = {
  21. 31, 0, 31, 30, 31, 31, 30, 31, 30, 31
  22. };
  23.  
  24. if ((year % 400 == 0 || year % 100 != 0) && (year % 4 == 0))
  25. month_cnt[1] = 29;
  26. else
  27. month_cnt[1] = 28;
  28.  
  29. days = 0;
  30. month = 0;
  31.  
  32. next_app = month_cnt[0];
  33. while (days + next_app < day)
  34. {
  35. month++;
  36. days += next_app;
  37. next_app = month_cnt[month];
  38. }
  39.  
  40. if (out_days)
  41. *out_days = days;
  42.  
  43. return month;
  44. }
  45.  
  46. 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)
  47. {
  48. int year;
  49. const char * month_name;
  50. int month;
  51. int days_start_month;
  52. size_t rem_days;
  53. size_t rem_hours;
  54. size_t rem_mins;
  55. size_t rem_secs;
  56. size_t rem_millisecs;
  57.  
  58. rem_days = ((ms % 31556952000) / 86400000);
  59. rem_hours = ((ms % 86400000) / 3600000);
  60. rem_mins = ((ms % 3600000) / 60000);
  61. rem_secs = ((ms % 60000) / 1000);
  62. rem_millisecs = (ms % 1000);
  63.  
  64. year = 1970 + (ms / 31556952000);
  65.  
  66. // Humans are fucking retards
  67. month = __month_calc(rem_days, year, &days_start_month);
  68. month_name = __month_names[month];
  69. month++;
  70. rem_days++;
  71.  
  72. if (o_year)
  73. *o_year = year;
  74.  
  75. if (o_month)
  76. *o_month = (int)month;
  77.  
  78. if (o_month_name)
  79. *o_month_name = month_name;
  80.  
  81. if (o_day_of_year)
  82. *o_day_of_year = (int)rem_days;
  83.  
  84. if (o_day_of_month)
  85. *o_day_of_month = (int)rem_days - days_start_month;
  86.  
  87. if (o_hours)
  88. *o_hours = rem_hours;
  89.  
  90. if (o_minutes)
  91. *o_minutes = rem_mins;
  92.  
  93. if (o_seconds)
  94. *o_seconds = rem_secs;
  95.  
  96. if (o_milliseconds)
  97. *o_milliseconds = rem_millisecs;
  98. }
  99.  
  100. size_t DateHelpers::ISO8601_format(char * str, size_t length, uint64_t ms)
  101. {
  102. int year, month, day, hour, minute, second, mills;
  103. DateHelpers::parse_epoch(ms, &year, &month, nullptr, nullptr, &day, &hour, &minute, &second, &mills);
  104. return snprintf(str, length, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", year, month, day, hour, minute, second, mills);
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement