linux c++ 如何获取 系统时间

linux c++ 如何获取 系统时间,第1张

用cstlib函数time,比如

#include <iostream>

#include <cstlib>

using namespace std;

int main()

{

int t, h, m, s;

t = time( 0 );

t = t % 86400;

s = t % 60;

t = ( t - s ) / 60;

m = t % 60;

t = ( t - m ) / 60;

h = t;

cout << "现在是" << h << ":" << m << ":" << s << endl;

return 0;

}

即可显示

#include <iostreamh>

#include <ctime> void main()

{

tm year; //年

tm month; //月

tm day; //日

tm hour;//时

tm minute;

tm second; time_t t;

t=time(0); year = localtime(&t);

month = localtime(&t);

day = localtime(&t);

hour = localtime(&t);

minute = localtime(&t);

second = localtime(&t); cout<<(year ->tm_year+1900)<<"年" //返回的是今年减1900,所以要加上1900

<<(month->tm_mon+1)<<"月" //因为Month (0 – 11; January = 0),所以要加1

<<day ->tm_mday<<"日"

<<hour ->tm_hour<<"时"

<<minute->tm_min<<"分"

<<second->tm_sec<<"秒"

<<endl;

}

C++包含ctime头文件要用#include,而不是import,不要跟python搞混了

测试代码如下:

#include <iostream>

#include <ctime> // 包含头文件

using namespace std;

int main() {

    time_t curtime = time(0);

    cout << "当前时间: " << ctime(&curtime) << endl;

    return 0;

}

运行结果如下:

可见按ctime指定格式成功输出了本地时间,望采纳~

#include <timeh>

time_t ltime;

time( &ltime );

这样就可以了, 下面是示例代码,够详细了吧

// crt_timesc

// compile with: /W3

// This program demonstrates these time and date functions:

// time _ftime ctime_s asctime_s

// _localtime64_s _gmtime64_s mktime _tzset

// _strtime_s _strdate_s strftime

//

// Also the global variable:

// _tzname

//

#include <timeh>

#include <stdioh>

#include <sys/typesh>

#include <sys/timebh>

#include <stringh>

int main()

{

char tmpbuf[128], timebuf[26], ampm[] = "AM";

time_t ltime;

struct _timeb tstruct;

struct tm today, gmt, xmas = { 0, 0, 12, 25, 11, 93 };

errno_t err;

// Set time zone from TZ environment variable If TZ is not set,

// the operating system is queried to obtain the default value

// for the variable

//

_tzset();

// Display operating system-style date and time

_strtime_s( tmpbuf, 128 );

printf( "OS time:\t\t\t\t%s\n", tmpbuf );

_strdate_s( tmpbuf, 128 );

printf( "OS date:\t\t\t\t%s\n", tmpbuf );

// Get UNIX-style time and display as number and string

time( &ltime );

printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );

err = ctime_s(timebuf, 26, &ltime);

if (err)

{

printf("ctime_s failed due to an invalid argument");

exit(1);

}

printf( "UNIX time and date:\t\t\t%s", timebuf );

// Display UTC

err = _gmtime64_s( &gmt, &ltime );

if (err)

{

printf("_gmtime64_s failed due to an invalid argument");

}

err = asctime_s(timebuf, 26, &gmt);

if (err)

{

printf("asctime_s failed due to an invalid argument");

exit(1);

}

printf( "Coordinated universal time:\t\t%s", timebuf );

// Convert to time structure and adjust for PM if necessary

err = _localtime64_s( &today, &ltime );

if (err)

{

printf("_localtime64_s failed due to an invalid argument");

exit(1);

}

if( todaytm_hour >= 12 )

{

strcpy_s( ampm, sizeof(ampm), "PM" );

todaytm_hour -= 12;

}

if( todaytm_hour == 0 ) // Adjust if midnight hour

todaytm_hour = 12;

// Convert today into an ASCII string

err = asctime_s(timebuf, 26, &today);

if (err)

{

printf("asctime_s failed due to an invalid argument");

exit(1);

}

// Note how pointer addition is used to skip the first 11

// characters and printf is used to trim off terminating

// characters

//

printf( "12-hour time:\t\t\t\t%8s %s\n",

timebuf + 11, ampm );

// Print additional time information

_ftime( &tstruct ); // C4996

// Note: _ftime is deprecated; consider using _ftime_s instead

printf( "Plus milliseconds:\t\t\t%u\n", tstructmillitm );

printf( "Zone difference in hours from UTC:\t%u\n",

tstructtimezone/60 );

printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] ); //C4996

// Note: _tzname is deprecated; consider using _get_tzname

printf( "Daylight savings:\t\t\t%s\n",

tstructdstflag "YES" : "NO" );

// Make time for noon on Christmas, 1993

if( mktime( &xmas ) != (time_t)-1 )

{

err = asctime_s(timebuf, 26, &xmas);

if (err)

{

printf("asctime_s failed due to an invalid argument");

exit(1);

}

printf( "Christmas\t\t\t\t%s\n", timebuf );

}

// Use time structure to build a customized time string

err = _localtime64_s( &today, &ltime );

if (err)

{

printf(" _localtime64_s failed due to invalid arguments");

exit(1);

}

// Use strftime to build a customized time string

strftime( tmpbuf, 128,

"Today is %A, day %d of %B in the year %Y\n", &today );

printf( tmpbuf );

}

以上就是关于linux c++ 如何获取 系统时间全部的内容,包括:linux c++ 如何获取 系统时间、C++中获取当前时间的代码、c++获取本地时间用time(0)报错,因为time是静态函数,请问怎么解决这个问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9530567.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存