Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if defined STRFTIME_V1
- ********************************************************************************
- * STRFTIME EMULATOR V0.1 BY DANIEL-92 12/07/2011 *
- * Copyright © 2011 [Daniel-92 / [92]Daniel / (=Maxell=)] *
- * This file is provided as is (no warranties). *
- ********************************************************************************
- native strftime(format[],dest[],length = sizeof(dest));
- native getweek(day,month,year);
- native ToString(value);
- native yearday(day,month,year);
- native IsLeapYear(year);
- native weekday(day, month, year);
- native AddDay(new_days=1,day=0,month=0,year=0);
- native GetDateByYearDay(_yearday,year=0);
- native GetDateFormat(pos_1,pos_2 = 0,pos_3 = 0);
- native GetTimeFormat(bool:12_hours = false);
- #endif
- #if !defined _samp_included
- #include a_samp
- #endif
- #if !defined IsLeapYear
- #define IsLeapYear(%1) ((%1 % 4 == 0 && %1 % 100 != 0) || %1 % 400 == 0)
- #endif
- #if defined STRF_TIME_DEBUG
- #define DEBUG printf("CASE = %%%c",formato[i]);
- #else
- #define DEBUG
- #endif
- new const month_n[][] = {
- "Ene", "Enero",
- "Feb", "Febrero",
- "Mar", "Marzo",
- "Abr", "Abril",
- "May", "Mayo",
- "Jun", "Junio",
- "Jul", "Julio",
- "Ago", "Agosto",
- "Sep", "Septiembre",
- "Oct", "Octubre",
- "Nov", "Noviembre",
- "Dic", "Diciembre"
- };
- new const days_n[][] = {
- "Sab", "Sábado",
- "Dom", "Domingo",
- "Lun", "Lunes",
- "Mar", "Martes",
- "Mier", "Miercoles",
- "Juv", "Jueves",
- "Vier", "Viernes"
- };
- /*------------------------------------------------------------------------------
- * Función strftime(formato[],dest[],length) by Daniel-92 (inspirada en la libreria ctime de c++)
- * Parámetros
- * formato = Cadena de texto a formatear con los especificadores
- * dest = Salida de la cadena de texto formateada y procesada
- * length = Tamaño de la cadena de salida
- */
- stock strftime(formato[],dest[],length = sizeof(dest),n_day=0,n_month=0,n_year=0)
- {
- new
- len = strlen(formato),
- last_pos = 0,
- time[3],
- date[3];
- if(!len || !length) return 0;
- gettime(time[0],time[1],time[2]);
- if(n_day==0 || n_month==0 || n_year==0) {
- getdate(date[0],date[1],date[2]);
- }
- else {
- date[2] = n_day;
- date[1] = n_month;
- date[0] = n_year;
- }
- for(new i=0; i < length; i++) {
- dest[i] = EOS;
- }
- for(new i=0; i < len; i++) {
- if(formato[i] == '%') {
- if(formato[++i] == '\0' || formato[i] == '\1' || last_pos >= length) {
- break;
- }
- switch(formato[i]) {
- case '%': {//%% Es reemplazado por %
- strins(dest,"%",last_pos++,length);
- }
- case 'a': {// %a Es reemplazado por la abreviatura del nombre del día de la semana de la localidad
- strins(dest,days_n[weekday(date[2],date[1],date[0])*2],last_pos,length);
- last_pos = strlen(dest);
- }
- case 'A': {//%A Es reemplazado por el nombre completo del día de la semana de la localidad
- strins(dest,days_n[(weekday(date[2],date[1],date[0])*2)+1],last_pos,length);
- last_pos = strlen(dest);
- }
- case 'b': {//%b Es reemplazado por la abreviatura del nombre del mes de la localidad
- DEBUG
- strins(dest,month_n[(date[1]-1)*2],last_pos,length);
- last_pos = strlen(dest);
- }
- case 'B': {//%B Es reemplazado por el nombre completo del mes de la localidad
- DEBUG
- strins(dest,month_n[((date[1]-1)*2)+1],last_pos,length);
- last_pos = strlen(dest);
- }
- case 'c': {//%c Es reemplazado por la fecha apropiada y la representación de la hora de la localidad "10/10/11 10:08:53"
- DEBUG //pawn no pude obtener la representación adecuada (se toma por defecto %d/%m/%y %H:%M:%S)
- new dh_format[32],date_[3];
- strmid(date_,ToString(date[0],2),2,4);
- format(dh_format,sizeof(dh_format),"%02d/%02d/%02d %02d:%02d:%02d",date[2],date[1],strval(date_),time[0],time[1],time[2]);
- strins(dest,dh_format,last_pos,length);
- last_pos = strlen(dest);
- }
- case 'C': {//%C Es remplazado por el siglo como un número decimal
- DEBUG
- strins(dest,ToString(floatround(floatdiv(date[0],100)),2),last_pos,length);
- last_pos = strlen(dest);
- }
- case 'd': {//%d Es reemplazado por el día del mes como un número decimal (01-31)
- DEBUG
- strins(dest,ToString(date[2],2),last_pos,length);
- last_pos = strlen(dest);
- }
- case 'F': {//%x Es reemplazado por la fecha en formato año-mes-dia %Y-%m-%d (normalmente usado en las bases de datos)
- DEBUG
- new d_format[16];
- format(d_format,sizeof(d_format),"%04d-%02d-%02d",date[0],date[1],date[2]);
- strins(dest,d_format,last_pos,length);
- last_pos = strlen(dest);
- }
- case 'H': {//%H Es reemplazado por la hora (reloj de 24 horas) como un número decimal (00-23)
- DEBUG
- strins(dest,ToString(time[0],2),last_pos,length);
- last_pos = strlen(dest);
- }
- case 'I': {//%I Es reemplazado por la hora (reloj de 12 horas) como un número decimal (01-12)
- DEBUG
- strins(dest,ToString(time[0] > 12 ? (time[0]-12):time[0],2),last_pos,length);
- last_pos = strlen(dest);
- }
- case 'j': {//%j Es reemplazado por el día del año como un número decimal (001-366)
- strins(dest,ToString(yearday(date[2],date[1],date[0]),2),last_pos,length);
- last_pos = strlen(dest);
- }
- case 'm': {//%m Es reemplazado por el mes como un número decimal (01-12)
- DEBUG
- strins(dest,ToString(date[1],2),last_pos,length);
- last_pos = strlen(dest);
- }
- case 'M': {//%M Es reemplazado por el minuto como un número decimal (00-59)
- DEBUG
- strins(dest,ToString(time[1],2),last_pos,length);
- last_pos = strlen(dest);
- }
- case 'p': {//%p Es reemplazado por el equivalente de la localidad de las designaciones de AM/PM en mayúsculas asociadas con un reloj de 12 horas
- DEBUG
- strins(dest,time[0] >= 12 ? ("PM"):("AM"),last_pos,length);
- last_pos = strlen(dest);
- }
- case 'P': {//%p Es reemplazado por el equivalente de la localidad de las designaciones de AM/PM en minúsculas asociadas con un reloj de 12 horas
- DEBUG
- strins(dest,time[0] >= 12 ? ("pm"):("am"),last_pos,length);
- last_pos = strlen(dest);
- }
- case 'r': {//%x Es reemplazado por la hora minutos y segundos como un reloj de 12 horas especificando AM/PM, lo mismo que usar (%I:%M:%S %p)
- DEBUG
- new h_format[16];
- format(h_format,sizeof(h_format),"%02d:%02d:%02d %s",time[0] > 12 ? (time[0]-12):(time[0]),time[1],time[2],time[0]<12 ? ("AM"):("PM"));
- strins(dest,h_format,last_pos,length);
- last_pos = strlen(dest);
- }
- case 'R': {//%r Es reemplazado por la hora minutos y segundos como un reloj de 24 horas sin segundos, lo mismo que usar (%H:%M)
- DEBUG
- new h_format[16];
- format(h_format,sizeof(h_format),"%02d:%02d",time[0],time[1]);
- strins(dest,h_format,last_pos,length);
- last_pos = strlen(dest);
- }
- case 'S': {//%S Es reemplazado por el segundo como un número decimal (00-61)
- DEBUG
- strins(dest,ToString(time[2],2),last_pos,length);
- last_pos = strlen(dest);
- }
- case 'U': {//%W Es reemplazado por el número de la semana del año (el primer Domingo como el primer día de la semana 1) como un número decimal (00-53)
- DEBUG
- strins(dest,ToString(getweek(date[2],date[1],date[0],2),2),last_pos,length);
- last_pos = strlen(dest);
- }
- case 'W': {//%W Es reemplazado por el número de la semana del año (el primer Lunes como el primer día de la semana 1) como un número decimal (00-53)
- DEBUG
- strins(dest,ToString(getweek(date[2],date[1],date[0],3),2),last_pos,length);
- last_pos = strlen(dest);
- }
- case 'w': {//%w Es reemplazado por el día de la semana como un número decimal (0-6), donde Domingo es 0
- DEBUG
- new day = weekday(date[2],date[1],date[0]);
- strins(dest,ToString(day ? (day-1):(6),2),last_pos,length);
- last_pos = strlen(dest);
- }
- case 'x': {//%x Es reemplazado por la representación apropiada de la fecha de la localidad "10/10/11"
- DEBUG //pawn no puede obtener la representación adecuada (se toma por defecto %d/%m/%y)
- new d_format[16],date_[3];
- strmid(date_,ToString(date[0],2),2,4);
- format(d_format,sizeof(d_format),"%02d/%02d/%02d",date[2],date[1],strval(date_));
- strins(dest,d_format,last_pos,length);
- last_pos = strlen(dest);
- }
- case 'X': {//%X Es reemplazado por la representación apropiada de la hora de la localidad "10:08:53"
- DEBUG //pawn no puede obtener la representación adecuada (se toma por defecto como un reloj de 24 horas lo mismo que usar %H:%M:%S)
- new h_format[16];
- format(h_format,sizeof(h_format),"%02d:%02d:%02d",time[0],time[1],time[2]);
- strins(dest,h_format,last_pos,length);
- last_pos = strlen(dest);
- }
- case 'y': {//%y Es reemplazado por el año sin siglo como un número decimal (00-99)
- DEBUG
- new date_[3];
- strmid(date_,ToString(date[0],2),2,4);
- strins(dest,date_,last_pos,length);
- last_pos = strlen(dest);
- }
- case 'Y': {//%Y Es reemplazado por el año con siglo como un número decimal (2012)
- DEBUG
- strins(dest,ToString(date[0],2),last_pos,length);
- last_pos = strlen(dest);
- }
- case 'Z': {//%Z Es reemplazado por el nombre o la abreviatura del uso horario si el uso horario es determinable
- DEBUG //%Z no está soportado en esta versíón. (pawn no puede obtener el uso horario en su versión 3.2.3664)
- strins(dest,"-6 GTM",last_pos,length);
- last_pos = strlen(dest);
- }
- default: {
- continue;
- }
- }
- }
- else {
- dest[last_pos++] = formato[i];
- }
- }
- return last_pos;
- }
- /*------------------------------------------------------------------------------
- * ISO-8601 Week Number - V 1.1
- * Processes the week number of a date,
- *
- * Procedure by Rick McCarty, 1999
- * Adapted to CVI by R.Bozzolo, 2006
- * Adapted to AS2/AS3 by A.Colonna, 2008
- * Adapted to Pawn by Daniel-92 2011
- *
- * Retorna el número de la semana tomando first_day como el primer dia de la semana*/
- stock getweek(day,month,year,firts_day) { //
- #pragma unused firts_day
- new bool:isLeapYear = IsLeapYear(year)?(true):(false);
- new month_array[] = {0,31,59,90,120,151,181,212,243,273,304,334};
- new DayOfYearNumber = day + month_array[month];
- if(isLeapYear && month > 2 || month == 2 && day > 28)
- DayOfYearNumber++;
- new YY = (year-1) % 100;
- new C = (year-1) - YY;
- new G = YY + YY / 4;
- new Jan1Weekday = floatround(1 + (((((C / 100) % 4) * 5) + G) % 7),floatround_floor);
- new H = DayOfYearNumber + (Jan1Weekday - 1);
- new Weekday = floatround(1 + ((H -1) % 7),floatround_floor);
- new YearNumber = year;
- new WeekNumber;
- if(DayOfYearNumber <= (8 - Jan1Weekday) && Jan1Weekday > 4) {
- YearNumber = year - 1;
- if (Jan1Weekday == 5 || (Jan1Weekday == 6 && isLeapYear)) {
- WeekNumber = 53;
- }
- else {
- WeekNumber = 52;
- }
- }
- if(YearNumber == year) {
- new I = 365;
- if (isLeapYear) {
- I = 366;
- }
- if (I - DayOfYearNumber < 4 - Weekday) {
- YearNumber = year + 1;
- WeekNumber = 1;
- }
- }
- if(YearNumber == year) {
- new J = DayOfYearNumber + (7 - Weekday) + (Jan1Weekday -1);
- WeekNumber = J / 7;
- if (Jan1Weekday > 4) {
- WeekNumber--;
- }
- }
- return (WeekNumber - 4);
- }
- /*------------------------------------------------------------------------------
- * Función TosSting(value,zeros=0,type[] = "0") by Daniel-92
- * Convierte un número entero a un número en forma de string
- * Parámetros
- * value = Número entero que se quiera convertir
- * zeros = Dígitos que serán comprobados
- * type = Caracter que sera insertado si el valor no alcanza los digitos especificados en el parámetro zeros
- * Ejemplo: ToString(6,3," ") Retornará " 6" */
- #define MAX_INTEGER_LEN 32
- stock ToString(value,zeros = 0,type[]="0") {
- new
- result[MAX_INTEGER_LEN],
- pos = 0;
- if(zeros > 0) {
- new temp[MAX_INTEGER_LEN];
- valstr(temp,value);
- new len = strlen(temp);
- for(new i = 0; i < zeros; i++) {
- strcat(result,type);
- }
- pos = zeros - len;
- if(pos < 0) {
- pos = 0;
- }
- for(new i=0; i < len; i++) {
- result[pos+i] = temp[i];
- }
- }
- else {
- valstr(result,value);
- }
- return result;
- }
- /*------------------------------------------------------------------------------
- * Función yearday(day,month,year) by Daniel-92
- * Parámetros
- * day = dia del mes
- * month = mes
- * year = año
- * Returns = dia del año (1-365 o hasta 366 si el año es biciesto) */
- stock yearday(day,month,year) {
- new //1 2 3 4 5 6 7 8 9 10 11 12
- days[] = {31,0,31,30,31,30,31,31,30,31,30,31},
- output = 0;
- days[1] = IsLeapYear(year)?(29):(28);
- for(new i=0; i < month-1; i++) {
- output += days[i];
- }
- return output + day;
- }
- /*------------------------------------------------------------------------------
- * Función IsLeapYear(year) (Popular en intenet fuente desconocida)
- * Parámetros
- * year = año
- * Returns: verdadero si el año es biciesto de lo contrario retornará falso */
- #if !defined IsLeapYear
- stock IsLeapYear(year) {
- return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
- }
- #endif
- /*------------------------------------------------------------------------------
- * Función weekday(day, month, year) By ITB CompuPhase (script weekday.pwn)
- * Parámetros
- * day = dia
- * month= mes
- * year = año
- * Returns: Retorna el dia de la semana como un número decimal (0-6) donde 0 es Domingo */
- stock weekday(day, month, year) {
- if (month <= 2) {
- month += 12, --year;
- }
- new j = year % 100;
- new e = year / 100;
- return (day + (month+1)*26/10 + j + j/4 + e/4 - 2*e) % 7;
- }
- /*------------------------------------------------------------------------------
- * Funcion AddDay(new_days, day, month, year) by Daniel-92
- * Parámetros
- * new_days = dias que se le agregarán a una fecha
- * day = dia desde el cual se empezaran a sumar los dias.
- * month = mes desde el cual se empezaran a sumar los dias.
- * year = año desde el cual se empezaran a sumar los dias.
- * returns: Retorna la nueva fecha como un array
- array[0] = retorna el dia.
- array[1] = retorna el mes.
- array[0] = retorna el año.
- *Notas: - La función SOLO FUNCIONA PARA AGREGAR DIAS POSITIVOS
- - Los paramtros day, month y year son opcionales si no se colocan
- la función tomará la fecha local como punto de partida.
- */
- stock AddDay(new_days=1,day=0,month=0,year=0) {
- if(day == 0 && month == 0 && year == 0) {
- getdate(year,month,day);
- }
- new date[3],leapyear = IsLeapYear(year);
- new days = yearday(day,month,year)+new_days;
- if(days > 0) {
- if(days <= (leapyear ? 366 : 365)) {
- date = GetDateByYearDay(days,year);
- }
- else {
- new total_days = 0;
- while(total_days < days) {
- total_days += IsLeapYear(year) ? 366 : 365;
- year++;
- }
- year = year - 1;
- days = total_days-days;
- date = GetDateByYearDay((IsLeapYear(year) ? 366 : 365) - days,year);
- }
- }
- return date;
- }
- /* -----------------------------------------------------------------------------
- * Funcion GetDateByYearDay by Daniel-92
- * Parámetros
- * _yearday = dia del año (0-365 o 0-366 si el año es biciesto)
- * year = año al cual se le quiere sacar la fecha
- * Returns: Retorna la fecha según el dia del año que se ha pasado en los parámetros.
- array[0] = retorna el dia.
- array[1] = retorna el mes.
- array[0] = retorna el año.
- */
- stock GetDateByYearDay(_yearday,year=0) {
- new //1 2 3 4 5 6 7 8 9 10 11 12
- days[] = {31,0,31,30,31,30,31,31,30,31,30,31},
- date[] = {0 ,0, 0};
- if(!year) getdate(year);
- days[1] = IsLeapYear(year)?(29):(28);
- for(new i=0,j,t_days; i < 12; i++) {
- for(j=0; j < days[i]; j++) {
- if(++t_days == _yearday) {
- date[0] = j+1;
- date[1] = i+1;
- date[2] = year;
- return date;
- }
- }
- }
- return date;
- }
- /*------------------------------------------------------------------------------
- * Función GetDateFormat(parametros_1,parametro_2,parametro_3) by Daniel-92
- * Parámetros
- * 3 parámetros donde se pueden asignar 4 tipos de valores ('d','m','y',0)
- * 'd' = devuelve el dia como un número decimal (1- (28,29,30,31))
- * 'm' = devuelve el mes como un número decimal (1-12)
- * 'y' = devuelve el año como un número decimal con siglo (2012)
- ' 0 = no devuelve nada, se usa para retornar solamente 1 o 2 valores
- GetDateFormat('d','0','0') retornará solamente el dia, sin embargo el especificador 0
- es asignado internamente por la función cuando no se especifican los 3 parámetros
- y no es necesario usarlo, pudiendose usar GetDateFormat('d') que seria lo mismo que GetDateFormat('d','0','0')
- * Returns: (string) de fecha formateada por los parámetros de la función */
- stock GetDateFormat(pos_1,pos_2 = 0,pos_3 = 0) {
- new
- string_result[16],
- value[4];
- getdate(value[0],value[1],value[2]);
- for(new i=0; i < 3; i++) {
- if(!i) value[3] = pos_1; else if(i == 1) value[3] = pos_2; else value[3] = pos_3;
- switch(value[3]) {
- case 'y': strcat(string_result,ToString(value[0],2));
- case 'm': strcat(string_result,ToString(value[1],2));
- case 'd': strcat(string_result,ToString(value[2],2));
- }
- strcat(string_result,"-");
- }
- string_result[strlen(string_result)-1] = EOS;
- return string_result;
- }
- /*------------------------------------------------------------------------------
- * GetTimeFormat(12_hours) by Daniel-92
- * Parámetros
- * 12_hours = parametro opcional (true: devuelve la hora en formato H:M:S PM/AM)
- * Returns: (string) Devuelve la hora con minutos y segundos como reloj de 24 horas
- si se especifica el parámeto 12_hours como "true" devuelve la hora en
- formato de 12 horas*/
- stock GetTimeFormat(hours_12 = false) {
- new hour,minute,secound,tiempo[16];
- gettime(hour,minute,secound);
- if(!hours_12) {
- strcat(tiempo,ToString(hour,2));
- strcat(tiempo,":");
- strcat(tiempo,ToString(minute,2));
- strcat(tiempo,":");
- strcat(tiempo,ToString(secound,2));
- //format(tiempo,sizeof(tiempo),"%02d:%02d:%02d",hour,minute,secound);
- }
- else {
- new am = true;
- if(hour >= 12) {
- am = false;
- if(hour != 12)
- hour = hour - 12;
- }
- else if(hour == 0) hour = 12;
- strcat(tiempo,ToString(hour,2));
- strcat(tiempo,":");
- strcat(tiempo,ToString(minute,2));
- strcat(tiempo,":");
- strcat(tiempo,ToString(secound,2));
- strcat(tiempo,(am)?(" AM"):(" PM"));
- //format(tiempo,sizeof(tiempo),"%02d:%02d:%02d %s",hour,minute,secound,(am)?("AM"):("PM"));
- }
- return tiempo;
- }
- #tryinclude getparams
- #if defined _getparams_included
- /* Funcion DiferenciaFecha(date_1[],date_[]) by Daniel-92
- * Parametros
- * date_1[] - Primera fecha ala que se le restará la fecha 2
- * date_2[] - segunda fecha que le resta a la primera fecha
- * returns - Diferencia en dias entre la primera fecha y la segunda fecha
- * Ejemplo - DiferenciaFecha("10/09/2011","10/12/11");
- */
- stock DiferenciaFecha(date_1[],date_2[],separator = '/') {//By Lenin Ruiz
- new
- index[] = {0,1},
- date[3][2],
- days;
- GetParamsUseIndex(true);
- GetParamsInt(date_1,1,date[0][index[0]],separator);
- GetParamsInt(date_1,2,date[1][index[0]],separator,true);
- GetParamsInt(date_1,3,date[2][index[0]],separator,true);
- GetParamsInt(date_2,1,date[0][index[1]],separator,true);
- GetParamsInt(date_2,2,date[1][index[1]],separator,true);
- GetParamsInt(date_2,3,date[2][index[1]],separator,true);
- GetParamsUseIndex(false);
- if(date[2][0]!=date[2][1]) {
- if(date[2][0]>date[2][1]) {
- index[0] = 1;
- index[1] = 0;
- }
- for(new i=date[2][index[0]]; i<=date[2][index[1]]; i++) {
- switch(i==date[2][index[1]]) {
- case true: days+=yearday(date[0][index[1]],date[1][index[1]],date[2][index[1]]);
- case false: days+=(IsLeapYear(i))?(366):365;
- }
- }
- return !index[0]?(days-yearday(date[0][index[0]],date[1][index[0]],date[2][index[0]])):
- -(days-yearday(date[0][index[0]],date[1][index[0]],date[2][index[0]]));
- }
- return yearday(date[0][index[1]],date[1][index[1]],date[2][index[1]])
- -yearday(date[0][index[0]],date[1][index[0]],date[2][index[0]]);
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment