Advertisement
Jochemd

TimestampToDate.inc

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