我正在做一个功能,UNIX时间转换为日期(DD-MM-YYYY)转换Unix时间去约会
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(今天...)但打印> 13-3-2022,有什么错误?
#define IsLeapYear(%1) ((%1 % 4 == 0 && %1 % 100 != 0) || %1 % 400 == 0)
getTotalDaysInMonth is this;
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);
}
还张贴'IsLeapYear'的代码。 – chqrlie
如果你需要类似的东西来制作,我会看看这个只包含头文件的库:https://github.com/HowardHinnant/date – MikeMB
这里有一些库函数。你为什么不使用它们呢? –