TimestampToDate.inc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. - Timestamp To Date converter -
  3. - Made by Jochemd -
  4. - http://forum.sa-mp.com/member.php?u=580 -
  5. native TimestampToDate(Timestamp, &year, &month, &day, &hour, &minute, &second, HourGMT, MinuteGMT = 0);
  6. native DateToTimestamp(str[11]);
  7. */
  8. //#include <a_samp>
  9. //#include <sscanf2>
  10. #define SPLITTER .
  11. new MonthTimes[12][4] =
  12. {
  13. { 31, 31, 2678400, 2678400 },
  14. { 28, 29, 2419200, 2505600 },
  15. { 31, 31, 2678400, 2678400 },
  16. { 30, 30, 2592000, 2592000 },
  17. { 31, 31, 2678400, 2678400 },
  18. { 30, 30, 2592000, 2592000 },
  19. { 31, 31, 2678400, 2678400 },
  20. { 31, 31, 2678400, 2678400 },
  21. { 30, 30, 2592000, 2592000 },
  22. { 31, 31, 2678400, 2678400 },
  23. { 30, 30, 2592000, 2592000 },
  24. { 31, 31, 2678400, 2678400 }
  25. };
  26. stock IsLeapYear(year)
  27. {
  28. if(year % 4 == 0) return 1;
  29. else return 0;
  30. }
  31. stock TimestampToDate(Timestamp, &year, &month, &day, &hour, &minute, &second, HourGMT, MinuteGMT = 0)
  32. {
  33. new tmp = 2;
  34. year = 1970;
  35. month = 1;
  36. Timestamp -= 172800; // Delete two days from the current timestamp. This is necessary, because the timestamp retrieved using gettime() includes two too many days.
  37. for(;;)
  38. {
  39. if(Timestamp >= 31536000)
  40. {
  41. year ++;
  42. Timestamp -= 31536000;
  43. tmp ++;
  44. if(tmp == 4)
  45. {
  46. if(Timestamp >= 31622400)
  47. {
  48. tmp = 0;
  49. year ++;
  50. Timestamp -= 31622400;
  51. }
  52. else break;
  53. }
  54. }
  55. else break;
  56. }
  57. for(new i = 0; i < 12; i ++)
  58. {
  59. if(Timestamp >= MonthTimes[i][2 + IsLeapYear(year)])
  60. {
  61. month ++;
  62. Timestamp -= MonthTimes[i][2 + IsLeapYear(year)];
  63. }
  64. else break;
  65. }
  66. day = 1 + (Timestamp / 86400);
  67. Timestamp %= 86400;
  68. hour = HourGMT + (Timestamp / 3600);
  69. Timestamp %= 3600;
  70. minute = MinuteGMT + (Timestamp / 60);
  71. second = (Timestamp % 60);
  72. if(minute > 59)
  73. {
  74. minute = 0;
  75. hour ++;
  76. }
  77. if(hour > 23)
  78. {
  79. hour -= 24;
  80. day ++;
  81. }
  82. if(day > MonthTimes[month][IsLeapYear(year)])
  83. {
  84. day = 1;
  85. month ++;
  86. }
  87. if(month > 12)
  88. {
  89. month = 1;
  90. year ++;
  91. }
  92. return 1;
  93. }
  94. stock DateToTimestamp(str[11])
  95. {
  96. new date[3]; // date[0] = day date[1] = month date[2] = year
  97. if(!sscanf(str,"p<"#SPLITTER">ddd",date[0],date[1],date[2]))
  98. {
  99. new total = 0, tmp = 0;
  100. total += date[0] * 86400;
  101. if(date[1] == 2 && date[0] < 29) tmp = ((date[2] - 1968) / 4 - 2);
  102. else tmp = ((date[2] - 1968) / 4 - 1);
  103. total += tmp * 31622400;
  104. total += (date[2] - 1970 - tmp) * 31536000;
  105. for(new i = 1; i < date[1]; i ++) total += MonthTimes[i][0 + IsLeapYear(date[2])] * 86400;
  106. return total;
  107. }
  108. else return -1;
  109. }