zoukankan      html  css  js  c++  java
  • 【编程骚操作】C++ 获取系统时间!

    实现这个功能的方法有很多,这里我们看一下最常用的一种方式。

    获取系统的时间  time.cpp:

    #include <iostream>
    
    #include <time.h>
    
    #include <string>
    
    int main()
    
    {
    
        std::string s;
    
        char stime[256] = {0};
    
        time_t now_time;
    
        time(&now_time);
    
        s = ctime(&now_time);
    
        std::cout << s << std::endl;
    
        return 0;
    
    }

    通过编译,g++ -time.cpp -o time ,运行./time,后可以获得系统时间。

    然后通过函数 strftime() 可以选择自己想要输出的格式,

    如,只是输出时,分,秒:

    #include <iostream>
    
    #include <time.h>
    
    #include <string>
    
    int main()
    
    {
    
        std::string s;
    
        char stime[256] = {0};
    
        time_t now_time;
    
        time(&now_time);
    
        strftime(stime,sizeof(stime),"%H:%M:%S",localtime(&now_time));
    
        s = stime + '';
    
        std::cout << s << std::endl;
    
        return 0;
    
    }

     

    不管你是转行也好,初学也罢,进阶也可,如果你想学编程~

    【值得关注】我的 C/C++编程学习交流俱乐部!【点击进入】

    问题答疑,学习交流,技术探讨,还有超多编程资源大全,零基础的视频也超棒~

  • 相关阅读:
    HackerRank
    HackerRank
    HackerRank
    LeetCode "Bitwise AND of Numbers Range"
    HackerRank
    HackerRank
    LeetCode "Binary Tree Right Side View"
    HihoCoder
    HihoCoder
    HackerRank
  • 原文地址:https://www.cnblogs.com/huya-edu/p/14317659.html
Copyright © 2011-2022 走看看