c++ 时间的转化问题

来源:百度知道 编辑:UC知道 时间:2024/05/31 11:26:57
代码如下:

#include <iostream>
using namespace std;
#include <ctime>
int tim = time(NULL);
int rn(int n)
{
if(n % 40 == 0 && n % 100 != 0 || n % 400 == 0)
return 366;
else
return 355;
}

int ton()
{
int n=1970,tmp=0;
for(;tim>tmp;){
tmp += rn(n)*24*60*60;
n++;
}
return (n-2);
}

int tos()
{
return ((tim % (3600*24))/3600);
}

int tof()
{
return ((tim % (3600*24)) % (24*60)) / 60;
}

int tom()
{
return (tim % (60*24)) %60;
}

void main()
{
cout<< "we will go to :"<< endl;
cout<<ton() << " _ " << tos() << ":" << tof() << ":" << tom();
cout<<endl;
}
输出后与系统时间不一致,只有秒数是一样的。
希望达人帮忙看下错在哪里。能给出正确代码更好。
我只是想知道。为什么我这样输出会是错的。能不能帮我看看哪里错了

/*
c++中如何获取系统时间(日期,时,分,秒)?
*/

#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>

using namespace std;

void showtime(tm* pt)
{
cout<<setfill('0')<<1900 + pt->tm_year<<'-';
cout<<setw(2)<<pt->tm_mon + 1<<'-';
cout<<setw(2)<<pt->tm_mday<<' ';
cout<<setw(2)<<pt->tm_hour<<':';
cout<<setw(2)<<pt->tm_min<<':';
cout<<setw(2)<<pt->tm_sec<<'\n';
}

int main()
{
time_t t = time(NULL);
tm* pt = localtime(&t);
int sec = pt->tm_sec;
showtime(pt);
while(1)
{
t = time(NULL);
pt = localtime(&t);
if(sec != pt->tm_sec)
{