4

UNIX時間を日付に変換する関数を実行しています(dd-mm-yyyy)

stock UnixToTime(x)
{
    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;
            }
        }
    }
    printf("%i-%i-%i", dia, mes, year);
    return x;
}

しかし、動作しません。

1458342000 (今日...) で関数をテストしていますが、2022 年 13 月 3 日を超えると出力されます。エラーは何ですか?

#define IsLeapYear(%1)      ((%1 % 4 == 0 && %1 % 100 != 0) || %1 % 400 == 0)

getTotalDaysInMonth はこれです。

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);
}
4

1 に答える 1