根据Linux时间戳计算时间,不分大小月,每月30天,每年按照360天计算。开始时间1970/01/01 00:00:00,输入秒数,显示时间。
输入:10
返回:1970/01/01 00:00:10
输入:12345678
返回:1970/05/23 21:21:18
代码实现:
string CalculationDate(long long second)
{
int year = 1970, month = 1, day = 1;
int hours = 0, minute = 0;
while (second > 60)
{
second -= 60;
minute += 1;
if (minute == 60)
{
minute = 0;
hours += 1;
if (hours == 24)
{
hours = 0;
day += 1;
if (day == 30)
{
day = 1;
month += 1;
if (month == 12)
{
month = 1;
year += 1;
}
}
}
}
}
char buff[] = "1970/01/01 00:00:10";
sprintf(buff,"%d/%02d/%02d %02d:%02d:%02d", year, month, day, hours, minute, second);
return buff;
}
void Text()
{
cout << CalculationDate(10) << endl;
cout << CalculationDate(12345678) << endl;
}