Advertisement
Calgon

Untitled

Nov 12th, 2011
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 3.27 KB | None | 0 0
  1. stock dateFromTimestamp( timestamp, _form=0 )
  2. {
  3.     /*
  4.         ~ convert a Timestamp to a Date. (unknown source)
  5.         ~ 10.07.2009
  6.  
  7.         date( 1247182451 )  will print >> 09.07.2009-23:34:11
  8.         date( 1247182451, 1) will print >> 09/07/2009, 23:34:11
  9.         date( 1247182451, 2) will print >> July 09, 2009, 23:34:11
  10.         date( 1247182451, 3) will print >> 9 Jul 2009, 23:34
  11.     */
  12.     new year=1970, day=0, month=0, hour=0, mins=0, sec=0;
  13.  
  14.     new days_of_month[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
  15.     new names_of_month[12][10] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
  16.     new returnstring[32];
  17.  
  18.     while(timestamp>31622400){
  19.         timestamp -= 31536000;
  20.         if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) ) timestamp -= 86400;
  21.         year++;
  22.     }
  23.  
  24.     if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) )
  25.         days_of_month[1] = 29;
  26.     else
  27.         days_of_month[1] = 28;
  28.  
  29.  
  30.     while(timestamp>86400){
  31.         timestamp -= 86400, day++;
  32.         if(day==days_of_month[month]) day=0, month++;
  33.     }
  34.  
  35.     while(timestamp>60){
  36.         timestamp -= 60, mins++;
  37.         if( mins == 60) mins=0, hour++;
  38.     }
  39.  
  40.     sec=timestamp;
  41.  
  42.     switch( _form ){
  43.         case 1: format(returnstring, 31, "%02d/%02d/%d %02d:%02d:%02d", day+1, month+1, year, hour, mins, sec);
  44.         case 2: format(returnstring, 31, "%s %02d, %d, %02d:%02d:%02d", names_of_month[month],day+1,year, hour, mins, sec);
  45.         case 3: format(returnstring, 31, "%d %c%c%c %d, %02d:%02d", day+1,names_of_month[month][0],names_of_month[month][1],names_of_month[month][2], year,hour,mins);
  46.  
  47.         default: format(returnstring, 31, "%02d.%02d.%d-%02d:%02d:%02d", day+1, month+1, year, hour, mins, sec);
  48.     }
  49.  
  50.     return returnstring;
  51. }
  52.  
  53. stock unixTimeConvert(timestamp, compare = -1) {
  54.     // Made by Blacklite, fixed to work in Pawn by Calgon
  55.  
  56.     if (compare == -1) {
  57.         compare = gettime();
  58.     }
  59.    
  60.     new
  61.         n,
  62.         Float:d = (timestamp > compare) ? timestamp - compare : compare - timestamp,
  63.         returnstr[32];
  64.        
  65.     if (d < 60) {
  66.         format(returnstr, sizeof(returnstr), "< 1 minute");
  67.         return returnstr;
  68.     } else if (d < 3600) { // 3600 = 1 hour
  69.         n = floatround(floatdiv(d, 60.0), floatround_floor);
  70.         format(returnstr, sizeof(returnstr), "minute");
  71.     } else if (d < 86400) { // 86400 = 1 day
  72.         n = floatround(floatdiv(d, 3600.0), floatround_floor);
  73.         format(returnstr, sizeof(returnstr), "hour");
  74.     } else if (d < 2592000) { // 2592000 = 1 month
  75.         n = floatround(floatdiv(d, 86400.0), floatround_floor);
  76.         format(returnstr, sizeof(returnstr), "day");
  77.     } else if (d < 31536000) { // 31536000 = 1 year
  78.         n = floatround(floatdiv(d, 2592000.0), floatround_floor);
  79.         format(returnstr, sizeof(returnstr), "month");
  80.     } else {
  81.         n = floatround(floatdiv(d, 31536000.0), floatround_floor);
  82.         format(returnstr, sizeof(returnstr), "year");
  83.     }
  84.    
  85.     if (n == 1) {
  86.         format(returnstr, sizeof(returnstr), "1 %s", returnstr);
  87.     } else {
  88.         format(returnstr, sizeof(returnstr), "%d %ss", n, returnstr);
  89.     }
  90.     return returnstr;
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement