打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C++时间标准库时间time和系统时间的使用
userphoto

2018.02.09

关注
#include <iostream>
/*包含time头文件*/
#include <time.h>
using namespace std;
int main()
{
    //time_t是long类型,精确到秒,是当前时间和1970年1月1日零点时间的差
    const time_t t = time(NULL);
    cout<<"current time is "<<t<<endl;
    /*本地时间:日期,时间 年月日,星期,时分秒*/
    struct tm* current_time = localtime(&t);
    printf("current year is %d;current month is %d;current date of month is %d\r\n",
        1900 + current_time->tm_year,
        1 + current_time->tm_mon/*此month的范围为0-11*/,
        current_time->tm_mday);
    printf("current day of year is %d;current day in week is %d\r\n",
        current_time->tm_yday,/*当前日期是今年的第多少天[0,365] */
        current_time->tm_wday/*days since Sunday - [0,6] */);
    printf("time part %d:%d:%d \r\n",
        current_time->tm_hour,
        current_time->tm_min,
        current_time->tm_sec);
    printf("\t本地时间:%d-%d-%d %d:%d:%d\r\n",
        current_time->tm_year + 1900,
        current_time->tm_mon + 1,
        current_time->tm_mday,
        current_time->tm_hour,
        current_time->tm_min,
        current_time->tm_sec);
    /*格林威治时间*/
    struct tm* current_gmtime = gmtime(&t);
    printf("格林威治时间:%d-%d-%d %d:%d:%d\r\n",
        current_gmtime->tm_year + 1900,
        current_gmtime->tm_mon + 1,
        current_gmtime->tm_mday,
        current_gmtime->tm_hour,
        current_gmtime->tm_min,
        current_gmtime->tm_sec);
    system("pause");
    return 0;
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
time()函数
linux时间函数总结,特别有无时区转换
Linux中gmtime和localtime的区别 通过time()函数获取时间
Python中time模块详解
python工具模块介绍-time 时间访问和转换!
最新Python基础学习教程——时间模块详解大全之time模块
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服