`
Jia_er
  • 浏览: 10424 次
  • 性别: Icon_minigender_2
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

C++中的时间计算

    博客分类:
  • C++
阅读更多

1. time(0) function

    in the ctime header file, returns the current time in seconds elapsed since the time 00:00:00 on January 1, 1970 GMT,  which is known as the UNIX epoch because 1970 was the year when the UNIX operating system was formally introduced.

 

a sample code from Introduction to Programming with C++

 

#include <iostream>
#include <ctime>

using namespace std;

int main(){
    // Obtain the total seconds since the midnight, Jan 1, 1970
    int totalSeconds = time(0);

    // Compute the current second in the minute in the hour
    int currentSecond = totalSeconds % 60;

    // Obtain the total minutes
    int totalMinutes = totalSeconds / 60;

    // Compute the current minute in the hour
    int currentMinute = totalMinutes  %  60;

    // Obtain the total hours
    long totalHours = totalMinutes / 60;

    // Compute the current hour
    int currentHour = (int)( totalHours  %  24 );

    // Display the results
    cout << "Current time is " << currentHour << " : "
        << currentMinute << " : " << currentSecond << " GMT " << endl;

    return 0;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics