[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 
1GetLeftDaysFromYear(fecha[0], fecha[1], fecha[2])-1i++)
    {
        
GetCurrentDateAddingDays(i+1daymonthyear);
        
printf("%i. %s"iGetSpecificDate(daymonthyear));
        
//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(daymonthyear// Basado en http://es.wikipedia.org/wiki/Congruencia...6.52655218
{
    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) / 12d_[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(outputsizeof(output), "%i:%i"_dat[0], _dat[1]);
    
format(outputsizeof(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 && month <= sizeof(MonhtsYears)) {
        
format(month_strsizeof(month_str), "%s"MonhtsYears[month-1]);
    }
    else{
        
format(month_strsizeof(month_str), "Unknown");
    }
    return 
month_str;
}
stock getTotalDaysInMonth(_monthyear)
{
    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 >= && _month <= 12) ? (dias[_month-1] + (IsLeapYear(year) && _month == 0)) : 0);
}
stock GetSpecificDate(day = -1month = -1year = -1)
{
    new 
str[64];
    if(
day == -&& month == -&& year == -1)
    {
        new 
_year_month_day;
        
getdate(_year_month_day);
        
format(strsizeof(str), "%i %s, %s, %i"_dayGetDayOfWeek(_day_month_year), GetMonthName(_month), _year);
    }
    else
    {
        
format(strsizeof(str), "%i %s, %s, %i"dayGetDayOfWeek(daymonthyear), GetMonthName(month), year);
    }
    return 
str;
}
stock GetTotalDaysFromYear(year) return IsLeapYear(year) ? 366 365;
stock GetLeftDaysFromYear(day_monthyear) return GetTotalDaysFromYear(year) - CountDaysSpentInYear(day_monthyear);
stock CountDaysSpentInYear(day_monthYear)
{
    new 
SumaDeDias day;
    for(new 
1<= _month-1i++)
        
SumaDeDias += getTotalDaysInMonth(iYear);
    return 
SumaDeDias;
}
stock GetDateDateAddingDays(daysmonths_day_month_year, &dia, &mes, &year)
{
    return 
UnixToDate(DateToUnix(_day_month_year) + (days * (60 60 24) + months * (60 60 24 30)), diamesyear);
}
stock GetCurrentDateAddingDays(days, &dia, &mes, &year)
{
    new 
_year_month_day;
    
getdate(_year_month_day);
    
GetDateDateAddingDays(days0_day_month_yeardiamesyear);
    return 
1;
}
stock GetCurrentDateAddingMonths(months, &dia, &mes, &year)
{
    new 
_year_month_day;
    
getdate(_year_month_day);
    
GetDateDateAddingDays(0months_day_month_yeardiamesyear);
    return 
1;
}
stock GetDateFromStr_YYYYMMDDHHMM(str[], &day, &month, &year, &hour, &minute)
{
    
// formato -> YYYY/mm/dd, hh:mm
    
new numberStr[12];
    
strmid(numberStrstr810);
    
day strval(numberStr);
    
strmid(numberStrstr57);
    
month strval(numberStr);
    
strmid(numberStrstr04);
    
year strval(numberStr);
    
strmid(numberStrstr1214);
    
hour strval(numberStr);
    
strmid(numberStrstr15strlen(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(numberStrstr02);
    
day strval(numberStr);
    
strmid(numberStrstr35);
    
month strval(numberStr);
    
strmid(numberStrstr610);
    
year strval(numberStr);
    
strmid(numberStrstr1214);
    
hour strval(numberStr);
    
strmid(numberStrstr15strlen(str)-1);
    
minute strval(numberStr);
    return 
1;
}
stock FixFormatHour(hourminute)
{
    
// 6:3 convert to 06:03
    
    
new str[9];
    new 
bool:fixedHour[2];
    
    if(
<= hour <= 9){
        
format(strsizeof(str), "0%i"hour);
        
fixedHour[0] = true;
    }
    if(
<= minute <= 9){
        
format(strsizeof(str), "%s:0%i"strminute);
        
fixedHour[1] = true;
    }
    if(
fixedHour[0] == false){
        
format(strsizeof(str), "%i"hour);
    }
    if(
fixedHour[1] == false){
        
format(strsizeof(str), "%s:%i"strminute);
    }
    
    
/* 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(outputtime0pos+1);
    new 
hora strval(output);
    
strmid(outputtimepos+1strlen(time));
    new 
minuto strval(output);
    new 
_formatTime[3];
    if(
hora >= 13 && hora <= 24){
        
format(_formatTimesizeof(_formatTime), "PM");
        
hora hora 12;
    }
    else if(
hora >= && hora <= 12){
        
format(_formatTimesizeof(_formatTime), "AM");
    }
    
format(outputsizeof(output), "%s %s"FixFormatHour(horaminuto), _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(outputsizeof(output), "%02d/%02d/%d, %02d:%02d"_dat[0], _dat[1], _dat[2], _dat[3], _dat[4]);
    return 
output;
}
stock DateToUnix(daymonthyear)
{
    new 
0;
    for(new 
1970yearj++)
    {
        
+= (60 60 24)* 365;
        if(
IsLeapYear(j)){
            
+= (60*60)*24;
        }
    }
    
month--;
      for (new 
0monthi++){
        
+= getTotalDaysInMonth(monthyear) * 86400;
      }
      
+= day 86400;
    return 
x;
}
stock UnixToDate(x, &_day, &_month, &_year)
{
    new 
year 1970;
    new 
dia 1;
    new 
mes 1;
    while(
>= 86400) {
         
-= 86400;
           
dia ++;
         if(
dia getTotalDaysInMonth(mesyear)) {
               
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)