date.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "date.hpp"
  4. #include <ctime>
  5. /*
  6. * Get the current year
  7. */
  8. int date_get_year(void)
  9. {
  10. time_t t;
  11. struct tm * lt;
  12. t = time(NULL);
  13. lt = localtime(&t);
  14. return lt->tm_year+1900;
  15. }
  16. /*
  17. * Get the current month
  18. */
  19. enum e_month date_get_month(void)
  20. {
  21. time_t t;
  22. struct tm * lt;
  23. t = time(NULL);
  24. lt = localtime(&t);
  25. return (enum e_month)(lt->tm_mon+1);
  26. }
  27. /*
  28. * Get the day of the month
  29. */
  30. int date_get_dayofmonth(void)
  31. {
  32. time_t t;
  33. struct tm * lt;
  34. t = time(NULL);
  35. lt = localtime(&t);
  36. return lt->tm_mday;
  37. }
  38. /*
  39. * Get the day of the week
  40. */
  41. enum e_dayofweek date_get_dayofweek(void)
  42. {
  43. time_t t;
  44. struct tm * lt;
  45. t = time(NULL);
  46. lt = localtime(&t);
  47. return (enum e_dayofweek)lt->tm_wday;
  48. }
  49. /*
  50. * Get the day of the year
  51. */
  52. int date_get_dayofyear(void)
  53. {
  54. time_t t;
  55. struct tm * lt;
  56. t = time(NULL);
  57. lt = localtime(&t);
  58. return lt->tm_yday;
  59. }
  60. /*
  61. * Get the current hours
  62. */
  63. int date_get_hour(void)
  64. {
  65. time_t t;
  66. struct tm * lt;
  67. t = time(NULL);
  68. lt = localtime(&t);
  69. return lt->tm_hour;
  70. }
  71. /*
  72. * Get the current minutes
  73. */
  74. int date_get_min(void)
  75. {
  76. time_t t;
  77. struct tm * lt;
  78. t = time(NULL);
  79. lt = localtime(&t);
  80. return lt->tm_min;
  81. }
  82. /*
  83. * Get the current seconds
  84. */
  85. int date_get_sec(void)
  86. {
  87. time_t t;
  88. struct tm * lt;
  89. t = time(NULL);
  90. lt = localtime(&t);
  91. return lt->tm_sec;
  92. }
  93. /*
  94. * Get the value for the specific type
  95. */
  96. int date_get( enum e_date_type type )
  97. {
  98. switch( type ){
  99. case DT_SECOND:
  100. return date_get_sec();
  101. case DT_MINUTE:
  102. return date_get_min();
  103. case DT_HOUR:
  104. return date_get_hour();
  105. case DT_DAYOFWEEK:
  106. return date_get_dayofweek();
  107. case DT_DAYOFMONTH:
  108. return date_get_dayofmonth();
  109. case DT_MONTH:
  110. return date_get_month();
  111. case DT_YEAR:
  112. return date_get_year();
  113. case DT_DAYOFYEAR:
  114. return date_get_dayofyear();
  115. case DT_YYYYMMDD:
  116. return date_get( DT_YEAR ) * 10000 + date_get( DT_MONTH ) * 100 + date_get(DT_DAYOFMONTH);
  117. default:
  118. return -1;
  119. }
  120. }
  121. /*
  122. * Is today a day of the Sun for Star Gladiators?
  123. */
  124. bool is_day_of_sun(void)
  125. {
  126. return (date_get_dayofyear()+1)%2 == 0;
  127. }
  128. /*
  129. * Is today a day of the Moon for Star Gladiators?
  130. */
  131. bool is_day_of_moon(void)
  132. {
  133. return (date_get_dayofyear()+1)%2 == 1;
  134. }
  135. /*
  136. * Is today a day of the Star for Star Gladiators?
  137. */
  138. bool is_day_of_star(void)
  139. {
  140. return (date_get_dayofyear()+1)%5 == 0;
  141. }