C++中的时间计算_C/C++_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > C/C++ > C++中的时间计算

C++中的时间计算

 2012/6/11 0:14:22  Jia_er  程序员俱乐部  我要评论(0)
  • 摘要:1.time(0)functioninthectimeheaderfile,returnsthecurrenttimeinsecondselapsedsincethetime00:00:00onJanuary1,1970GMT,whichisknownastheUNIXepochbecause1970wastheyearwhentheUNIXoperatingsystemwasformallyintroduced
  • 标签: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;
}
上一篇: C++ Primitive Data Types 下一篇: 安装cmake
发表评论
用户名: 匿名