/* Time
 
Get time called - result
Sun Nov 24 17:44:23 2002
Sun Nov 2 7:44:23 2002
 
          1         2
012345678901234567890123
    Jan
    Feb
    Mar
    Apr
    May
    Jun
    Jul
    Aug
    Sep
    Oct
    Nov
    Dec
*/
 
#define MAX_DATE_LENGTH 21
 
new g_months[12]= { 31,28,31,30,31,30,31,31,30,31,30,31 };
new g_monthnames[12][] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
 
stock get_dateandtime(&year,&month,&day,&hour,&minute,&second) {
  new time[50];
  servertime(time,50);
 
  year = (time[20]-48) * 1000 +
         (time[21]-48) * 100 +
         (time[22]-48) * 10 +
          time[23]-48;
 
  switch (time[4]) {
    case 'J': {
      switch (time[5]) {
        case 'a': { month=1; }
        case 'u': {
          switch (time[6]) {
            case 'l': { month=7; }
            case 'n': { month=6; }
          }
        }
      }
    }
    case 'F': { month=2; }
 
    case 'M': {
      switch (time[6]) {
        case 'r': { month=3; }
        case 'y': { month=5; }
      }
    }
    case 'A': {
      switch (time[5]) {
        case 'p': { month=4; }
        case 'u': { month=8; }
      }
    }
    case 'S': { month=9; }
    case 'O': { month=10; }
    case 'N': { month=11; }
    case 'D': { month=12; }
  }
 
  if (time[8]==' ') {
    day=time[9]-48;
  }
  else {
    day=(time[8]-48)*10 + time[9]-48;
  }
 
  if (time[11]==' ') {
    hour=time[12]-48;
  }
  else {
    hour=(time[11]-48)*10 + time[12]-48;
  }
 
  minute=(time[14]-48)*10+time[15]-48;
  second=(time[17]-48)*10+time[18]-48;
 
}
 
stock getDaysSince1970(year,month,day) {
  new days = (year-1970) * 365;
  new i;
  for (i=1;i<month;i++) {
    days=days+g_months[i];
  }
 
  /* leap years */
  days=days + (year-1972) / 4;
  if (((year/4)*4 == year) && (month<3)) {
    days=days-1;
  }
  return days+day;
}
 
/* returns number of seconds since 1 Jan 1970 */
stock get_unixtime() {
  new year;
  new month;
  new day;
  new hour;
  new minute;
  new second;
 
  get_dateandtime(year,month,day,hour,minute,second);
  return getDaysSince1970(year,month,day) * 86400 + hour * 3600 + minute * 60 + second;
}
 
 
/* converts seconds into dd hh:mm:ss in str which must be big enough */
 
stock format_seconds(seconds,str[]) {
  new days=0;
  new hours=0;
  new minutes=0;
 
  if (seconds>86400) {
    days= seconds/86400;
    seconds=seconds-(seconds/86400) * 86400;
  }
 
  if (seconds > 3600) {
    hours = seconds/3600;
    seconds = seconds- hours * 3600;
  }
 
  if (seconds > 60) {
    minutes = seconds/60;
    seconds = seconds- minutes *  60;
  }
 
  str[0]=0;
  new pos=0;
  if (days>0) {
    numtostr(days,str);
    pos=strlen(str);
    str[pos]=' ';
    pos++;
  }
 
  if ((hours>0) ||(days>0)) {
    if (hours>9) {
      str[pos] = hours/10 + 48;
      pos++;
    }
    str[pos] = (hours % 10) + 48;
    pos++;
    str[pos] = ':';
    pos++;
  }
 
  str[pos] = (minutes/10) + 48;
  pos++;
  str[pos] = (minutes%10) + 48;
  pos++;
  str[pos] = ':';
  pos++;
  str[pos] = (seconds/10) + 48;
  pos++;
  str[pos] = (seconds%10) + 48;
  pos++;
  str[pos] = 0;
 
}
 
stock get_timestamp(strTimeStamp[]) {
  new year;
  new month;
  new day;
  new hour;
  new minute;
  new second;
 
  get_dateandtime(year,month,day,hour,minute,second);
 
  strTimeStamp[0] = (day/10) + 48;
  strTimeStamp[1] = (day%10) + 48;
  strTimeStamp[2] = 0;
 
  if (month==1) {
    strcat(strTimeStamp," Jan ",MAX_DATE_LENGTH);
  }
  else if (month==2) {
    strcat(strTimeStamp," Feb ",MAX_DATE_LENGTH);
  }
  else if (month==3) {
    strcat(strTimeStamp," Mar ",MAX_DATE_LENGTH);
  }
  else if (month==4) {
    strcat(strTimeStamp," Apr ",MAX_DATE_LENGTH);
  }
  else if (month==5) {
    strcat(strTimeStamp," May ",MAX_DATE_LENGTH);
  }
  else if (month==6) {
    strcat(strTimeStamp," Jun ",MAX_DATE_LENGTH);
  }
  else if (month==7) {
    strcat(strTimeStamp," Jul ",MAX_DATE_LENGTH);
  }
  else if (month==8) {
    strcat(strTimeStamp," Aug ",MAX_DATE_LENGTH);
  }
  else if (month==9) {
    strcat(strTimeStamp," Sep ",MAX_DATE_LENGTH);
  }
  else if (month==10) {
    strcat(strTimeStamp," Oct ",MAX_DATE_LENGTH);
  }
  else if (month==11) {
    strcat(strTimeStamp," Nov ",MAX_DATE_LENGTH);
  }
  else {
    strcat(strTimeStamp," Dec ",MAX_DATE_LENGTH);
  }
 
  new Buf[10];
  numtostr(year,Buf);
  strcat(strTimeStamp,Buf,MAX_DATE_LENGTH);
  strTimeStamp[11]=' ';
  strTimeStamp[12]= (hour/10) + 48;
  strTimeStamp[13]= (hour%10) + 48;
  strTimeStamp[14]=':';
  strTimeStamp[15]= (minute/10) + 48;
  strTimeStamp[16]= (minute%10) + 48;
  strTimeStamp[17]=':';
  strTimeStamp[18]= (second/10) + 48;
  strTimeStamp[19]= (second%10) + 48;
  strTimeStamp[20]=0;
}
 
/* Converts time periods such as 1d, 3w, 5m into minutes */
 
stock parseduration(strDuration[],strFormatted[]) {
	new l = strlen(strDuration);
	new nOut = strtonum(strDuration);
 
	if (l>0) {
		if ((strDuration[l-1]=='h') || (strDuration[l-1]=='H')) {
			snprintf(strFormatted,MAX_DATE_LENGTH,"%i hours",nOut);
			nOut=nOut*60;
		}
		else if ((strDuration[l-1]=='d') || (strDuration[l-1]=='D')) {
			snprintf(strFormatted,MAX_DATE_LENGTH,"%i days",nOut);
			nOut=nOut*1440;
		}
		else if ((strDuration[l-1]=='w') || (strDuration[l-1]=='W')) {
			snprintf(strFormatted,MAX_DATE_LENGTH,"%i weeks",nOut);
			nOut=nOut*10080;
		}
		else if ((strDuration[l-1]=='m') || (strDuration[l-1]=='M')) {
			snprintf(strFormatted,MAX_DATE_LENGTH,"%i months",nOut);
			nOut=nOut*40320;
		}
		else if ((strDuration[l-1]=='y') || (strDuration[l-1]=='Y')) {
			snprintf(strFormatted,MAX_DATE_LENGTH,"%i years",nOut);
			nOut=nOut*525600;
		}
		else {
			snprintf(strFormatted,MAX_DATE_LENGTH,"%i minutes",nOut);
		}
	}
	else {
		snprintf(strFormatted,MAX_DATE_LENGTH,"%i minutes",nOut);
	}
	return nOut;
}