C++ Time类初始化问题
各位大虾,用time(0)来初始化Time类应该有误差吧,怎么算哈,还有更好的初始化方法吗?
Time::Time()
34 {
35 long int totalTime; // time in seconds since 1970
36 int currentYear = 1998 - 1970; // current year
37 double totalYear; // current time in years
38 double totalDay; // days since beginning of year
39 double day; // current time in days
40 double divisor; // conversion divisor
41 int timeShift = 7; // time returned by time() is
42 // given as the number of seconds
43 // elapsed since 1/1/70 GMT.
44 // Depending on the time zone
45 // you are in, you must shift
46 // the time by a certain
47 // number of hours. For this
48 // problem, 7 hours is the
49 // current shift for EST.
50 double tempMinute; // Used in conversion to seconds.
51 double tempSecond; // Used to set seconds.
52
53 totalTime = time( 0 );
54 divisor = ( 60.0 * 60.0 * 24.0 * 365.0 );
55 totalYear = totalTime / divisor - currentYear;
56 totalDay = 365 * totalYear; // leap years ignored
57 day = totalDay - static_cast< int >( totalDay );
58 tempMinute = totalDay * 24 * 60;
59 setHour( day * 24 + timeShift );
60 setMinute( ( day * 24 - static_cast< int >( day * 24 ) ) * 60 );
61 tempMinute -= static_cast< int > ( tempMinute ) * 60;
62 tempSecond = tempMinute;
63 setSecond( tempSecond );
64 }
[解决办法]
time(0)可以获得秒数,用localtime或gtime得到一个tm结构体,里面有你想要的东西