[Include] zTime
#1

zTime
Date/Time functions

Time is a simple include functions for get or manipulate time data, either in what day falls the date that is, obtain data quickly in various formats (see functions that obtain the date in a format established and individual functions to sort in another for the moment not there function to sort time-data without a function which have a absolute format).

FUNCIONES.

Code:
v 1
* GetDayOfWeek(day, month, year); - get the name of a day from week from an date specific
* GetDate(); - get date in format  "Sбbado 23 de Agosto del 2014".
* GetTime(); - get time in format "08:47:56".
* GetMonthName(month); - get the month name
* IsLeapDay(year); - check if is leap-day (thank's to http://es.wikipedia.org/wiki/A%C3%B1..._computacional)


v 1.1 - Last UPDATE
[CHANGE] GetDate(); before it show: "21 Monday, March, 2016", now prints: "21 Monday, March, 2016 - 03:19 PM
[ADD] getTotalDaysInMonth(_month, year) - get total day's in a specific month
[ADD] GetSpecificDate(day = -1, month = -1, year = -1); get date in format: "21 Monday, March, 2016", this function is to get specific date, no today. To get date from now the function is GetDate() but without the time.
[ADD] GetTotalDaysFromYear(year) get the total day's from a year, taking into consideration leap-years
[ADD] GetLeftDaysFromYear(day, _month, year) get the total day's which left in a year.
[ADD] CountDaysSpentInYear(day, _month, Year) obtain the total of days that have passed in the year

// allow get the date in variables, when we add month or days. If is 22/03/2016 and we add +15 days, this not will be 36/03/2016, this function allow apply the date sintax, therefore will be 06/04/2016. (applying the format although be 29/12/2016, and we add 5 days).
[ADD] GetDateDateAddingDays(days, months, _day, _month, _year, &dia, &mes, &year) 

/////////

[ADD] GetCurrentDateAddingDays(days, &dia, &mes, &year), its the same as GetDateDateAddingDays only wich here we add only days.

[ADD] PrintDate() print now date in YYYY-MM-DD, HH:MM
[ADD] DateToUnix(day, month, year) convert real time (day, month and year) to unix time.
[ADD] UnixToDate(x, &_day, &_month, &_year) convert unix time to real time (day, month and year)


* Functions of time to string - POSSIBLY TEMPORARY
[ADD] GetDateFromStr_YYYYMMDDHHMM(str[], &day, &month, &year, &hour, &minute) convert date to format YYYY-MM-DD HH:MM (pass the vals to variables)
[ADD] GetDateFromStr_DDMMYYYYHHMM(str[], &day, &month, &year, &hour, &minute) convert date to format DD-MM-YYYY HH:MM (pass the vals to variables)


[ADD] FixFormatHour(hour, minute) convert time from 3:2 to 03:02.
[ADD] convert24HoursTo12Hours(time[]) convert 24 hours time: 13:00 to 12 hours time: 01:00 PM.
An example, is a code to know what day of the week / general date will be in the remaining days in the year.

PHP Code:
main()
{
    new 
day,
         
month,
         
year,
        
fecha[3] = {19,3,2016};
    for(new 
i = 1; i < GetLeftDaysFromYear(fecha[0], fecha[1], fecha[2])-1; i++)
    {
        
GetCurrentDateAddingDays(i+1, day, month, year);
        
printf("%i. %s", i, GetSpecificDate(day, month, year));
        
//sleep(1000/4); 
     
}
} 


CODE.


PHP Code:
/* zTime - _Zume */
#include <a_samp>
#if !defined gettime_ex
    #define gettime_ex gettime
#endif
#define GetCurrentDate()     GetSpecificDate(-1, -1, -1)
#define IsLeapYear(%1)         ((%1 % 4 == 0 && %1 % 100 != 0) || %1 % 400 == 0)
stock GetDayOfWeek(day, month, year) // Basado en http://es.wikipedia.org/wiki/Congruencia...8.68191372
{
    static const 
DaysWeek[][24] = {
        
"Monday",        //       0
        
"Tuesday",       //      1
        
"Wednesday",    //      2
        
"Thursday",       //      3
        
"Friday",      //      4
        
"Saturday",       //      5
        
"Sunday"       //      6
    
};
    new
        
d_[4],
        
m_[10]
    ;
    
d_[0] = (14-month) / 12, d_[2] = year - d_[0], d_[1] = month + (12*d_[0]) - 2;
    
d_[3] = (day + d_[2] + (d_[2]/4) - (d_[2]/100) + (d_[2]/400) + (31*d_[1]) / 12) % 7;
    if(
d_[3] == 0)
    {
        
d_[3] = sizeof(DaysWeek) - 1;
    }
    else
    {
        
d_[3] -= 1;
    }
    
    
format(m_, sizeof(m_), "%s", DaysWeek[d_[3]]);
    return 
m_;
}
stock GetDate()
{
    new 
output[40];
    new 
_dat[3];
    
gettime_ex(_dat[0], _dat[1], _dat[2]);
    
format(output, sizeof(output), "%i:%i", _dat[0], _dat[1]);
    
format(output, sizeof(output), "%s - %s", GetCurrentDate(), convert24HoursTo12Hours(output));
    return 
output;
}
stock GetTime()
{
    new
        
m_[34],
           
d_[3]
    ;
    
gettime_ex(d_[0], d_[1], d_[2]), format(m_, sizeof(m_), "%02d:%02d", d_[0], d_[1], d_[2]);
       return 
m_;
}
stock GetMonthName(month)
{
    static const 
MonhtsYears[12][] = {
        
"January",            // 0
        
"February",          // 1
        
"March",            // 2
        
"April",            // 3
        
"May",                // 4
        
"June",               // 5
        
"July",               // 6
        
"August",           // 7
        
"September",          // 8
        
"October",          // 9
        
"November",           // 10
        
"December"            // 11
    
};
    new
        
month_str[24]
    ;
    if(
month > 0 && month <= sizeof(MonhtsYears)) {
        
format(month_str, sizeof(month_str), "%s", MonhtsYears[month-1]);
    }
    else{
        
format(month_str, sizeof(month_str), "Unknown");
    }
    return 
month_str;
}
stock getTotalDaysInMonth(_month, year)
{
    new 
dias[] = {
        
31, // Enero
        
28, // Febrero
        
31, // Marzo
        
30, // Abril
        
31, // Mayo
        
30, // Junio
        
31, // Julio
        
31, // Agosto
        
30, // Septiembre
        
31, // Octubre
        
30, // Noviembre
        
31  // Diciembre
    
};
    return ((
_month >= 1 && _month <= 12) ? (dias[_month-1] + (IsLeapYear(year) && _month == 2 ? 1 : 0)) : 0);
}
stock GetSpecificDate(day = -1, month = -1, year = -1)
{
    new 
str[64];
    if(
day == -1 && month == -1 && year == -1)
    {
        new 
_year, _month, _day;
        
getdate(_year, _month, _day);
        
format(str, sizeof(str), "%i %s, %s, %i", _day, GetDayOfWeek(_day, _month, _year), GetMonthName(_month), _year);
    }
    else
    {
        
format(str, sizeof(str), "%i %s, %s, %i", day, GetDayOfWeek(day, month, year), GetMonthName(month), year);
    }
    return 
str;
}
stock GetTotalDaysFromYear(year) return IsLeapYear(year) ? 366 : 365;
stock GetLeftDaysFromYear(day, _month, year) return GetTotalDaysFromYear(year) - CountDaysSpentInYear(day, _month, year);
stock CountDaysSpentInYear(day, _month, Year)
{
    new 
SumaDeDias = day;
    for(new 
i = 1; i <= _month-1; i++)
        
SumaDeDias += getTotalDaysInMonth(i, Year);
    return 
SumaDeDias;
}
stock GetDateDateAddingDays(days, months, _day, _month, _year, &dia, &mes, &year)
{
    return 
UnixToDate(DateToUnix(_day, _month, _year) + (days * (60 * 60 * 24) + months * (60 * 60 * 24 * 30)), dia, mes, year);
}
stock GetCurrentDateAddingDays(days, &dia, &mes, &year)
{
    new 
_year, _month, _day;
    
getdate(_year, _month, _day);
    
GetDateDateAddingDays(days, 0, _day, _month, _year, dia, mes, year);
    return 
1;
}
stock GetCurrentDateAddingMonths(months, &dia, &mes, &year)
{
    new 
_year, _month, _day;
    
getdate(_year, _month, _day);
    
GetDateDateAddingDays(0, months, _day, _month, _year, dia, mes, year);
    return 
1;
}
stock GetDateFromStr_YYYYMMDDHHMM(str[], &day, &month, &year, &hour, &minute)
{
    
// formato -> YYYY/mm/dd, hh:mm
    
new numberStr[12];
    
strmid(numberStr, str, 8, 10);
    
day = strval(numberStr);
    
strmid(numberStr, str, 5, 7);
    
month = strval(numberStr);
    
strmid(numberStr, str, 0, 4);
    
year = strval(numberStr);
    
strmid(numberStr, str, 12, 14);
    
hour = strval(numberStr);
    
strmid(numberStr, str, 15, strlen(str)-1);
    
minute = strval(numberStr);
    return 
1;
}
stock GetDateFromStr_DDMMYYYYHHMM(str[], &day, &month, &year, &hour, &minute)
{
    
// formato -> dd/mm/YYYY, hh:mm
    
new numberStr[12];
    
strmid(numberStr, str, 0, 2);
    
day = strval(numberStr);
    
strmid(numberStr, str, 3, 5);
    
month = strval(numberStr);
    
strmid(numberStr, str, 6, 10);
    
year = strval(numberStr);
    
strmid(numberStr, str, 12, 14);
    
hour = strval(numberStr);
    
strmid(numberStr, str, 15, strlen(str)-1);
    
minute = strval(numberStr);
    return 
1;
}
stock FixFormatHour(hour, minute)
{
    
// 6:3 convert to 06:03
    
    
new str[9];
    new 
bool:fixedHour[2];
    
    if(
0 <= hour <= 9){
        
format(str, sizeof(str), "0%i", hour);
        
fixedHour[0] = true;
    }
    if(
0 <= minute <= 9){
        
format(str, sizeof(str), "%s:0%i", str, minute);
        
fixedHour[1] = true;
    }
    if(
fixedHour[0] == false){
        
format(str, sizeof(str), "%i", hour);
    }
    if(
fixedHour[1] == false){
        
format(str, sizeof(str), "%s:%i", str, minute);
    }
    
    
/* format(str, sizeof(str), "%02d:%02d", hour, minute); <-- This does not work, does not meet the need correctly. */
    
return str;
}
stock convert24HoursTo12Hours(time[])
{
    
// 13:00 -> 01:00 PM
    
new output[32];
    new 
pos = strfind(time, ":");
    
    
strmid(output, time, 0, pos+1);
    new 
hora = strval(output);
    
strmid(output, time, pos+1, strlen(time));
    new 
minuto = strval(output);
    new 
_formatTime[3];
    if(
hora >= 13 && hora <= 24){
        
format(_formatTime, sizeof(_formatTime), "PM");
        
hora = hora - 12;
    }
    else if(
hora >= 0 && hora <= 12){
        
format(_formatTime, sizeof(_formatTime), "AM");
    }
    
format(output, sizeof(output), "%s %s", FixFormatHour(hora, minuto), _formatTime);
    return 
output;
}
stock PrintDate() // YYYYMMDDHHMM
{
    new 
output[40],
        
_dat[6];
        
    
getdate(_dat[0], _dat[1], _dat[2]);
    
gettime_ex(_dat[3], _dat[4], _dat[5]);
    
format(output, sizeof(output), "%02d/%02d/%d, %02d:%02d", _dat[0], _dat[1], _dat[2], _dat[3], _dat[4]);
    return 
output;
}
stock DateToUnix(day, month, year)
{
    new 
x = 0;
    for(new 
j = 1970; j < year; j++)
    {
        
x += (60 * 60 * 24)* 365;
        if(
IsLeapYear(j)){
            
x += (60*60)*24;
        }
    }
    
month--;
      for (new 
i = 0; i < month; i++){
        
x += getTotalDaysInMonth(month, year) * 86400;
      }
      
x += day * 86400;
    return 
x;
}
stock UnixToDate(x, &_day, &_month, &_year)
{
    new 
year = 1970;
    new 
dia = 1;
    new 
mes = 1;
    while(
x >= 86400) {
         
x -= 86400;
           
dia ++;
         if(
dia > getTotalDaysInMonth(mes, year)) {
               
dia = 1;
             
mes ++;
               if(
mes > 12) {
                
year ++;
                
mes = 1;
              }
         }
    }
    
_day = dia;
    
_month = mes;
    
_year = year;
    return 
x;
} 
Reply
#2

Shit!
you went to the wiki page that it has a algorithm for calenders and made an include for it

I can't say anything
Reply
#3

Very nice. Thank you.
Reply
#4

the include was updated. the function to get month had some errors in leap-years, and some functions was added
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)