Linux下的时间函数

0. 写在前面

最近在做一个项目,频繁的需要使用到获取时间,包括相对时间、绝对时间等,发现C/C++和linux本身都提供了很多的API接口,看的眼花缭乱的,下面针对这些情况,我统一的做一下分类讲述。

1. 基本概念

  • 时间精度:

所谓时间精度,也成为时间颗粒,是指系统/函数所能给出的时间的最小单位;通常内核和应用层给出的时间精度各不相同:

  • 内核:tick(节拍)即CPUP的时钟定时器的一次终端;Jiffies用来记录自系统启动以来产生的节拍的总数,因此最小的时间颗粒为 Jiffies/Hz

2. Linux API

2.1 获取相对时间 (即系统起到后到目前的时间段)

1). sysinfo()

函数原型

#include     //头文件
int sysinfo(struct sysinfo *info);  //函数原型

结构体类型

struct sysinfo {  //结构体数据类型
           long uptime;               /* Seconds since boot */
           unsigned long loads[3];     /* 1, 5, and 15 minute load averages */
           unsigned long totalram;    /* Total usable main memory size */
           unsigned long freeram;      /* Available memory size */
           unsigned long sharedram;   /* Amount of shared memory */
           unsigned long bufferram;   /* Memory used by buffers */
           unsigned long totalswap;   /* Total swap space size */
           unsigned long freeswap;    /* swap space still available */
           unsigned short procs;      /* Number of current processes */
           char _f[22];               /* Pads structure to 64 bytes */
};

实例演示

#include 
#include 
#include 
#include 
#include 
#include 
int main(void)
{
    struct sysinfo info;
    char run_time[128];
    if (sysinfo(&info)) {
        fprintf(stderr, "Failed to get sysinfo, errno:%u, reason:%s\n",errno, strerror(errno));
        return -1;
    }
    long timenum=info.uptime;
    printf("The timenum=%ld\n", timenum);
    int runday=timenum/86400;
    int runhour=(timenum%86400)/3600;
    int runmin=(timenum%3600)/60;
    int runsec=timenum%60;
    bzero(run_time, 128);
    sprintf(run_time,"系统已运行:%d天%d时%d分%d秒",runday,runhour,runmin,runsec);
    printf("--->%s\n",run_time);
    return 0; 
}

运行结果

The timenum=127432
--->系统已运行:1天11时23分52秒

3. C lib

4. C++ STL

#include
#include 
#include 
#include 
#include 
#include 
using namespace std;

class SimpleTime
{
public:
    typedef std::chrono::duration > macrosec_type;    
    typedef std::chrono::duration millisec_type;    
    typedef std::chrono::duration seconds_type;    
    typedef std::chrono::duration > minute_type;    
    typedef std::chrono::duration > hour_type;    
    typedef std::chrono::duration > day_type;    

public:
    SimpleTime(){ start(); }

    //sleep milliseconds / seconds
    void sleep_msec(double num){
        long long _num = static_cast(round(num)); 
        std::this_thread::sleep_for(std::chrono::milliseconds(_num));
    }
    void sleep_sec(double num) { double _num=num*1e3*1.0; sleep_msec(_num); }

    //Get the point time
    std::string get_date_string(){
        std::chrono::system_clock::time_point today = std::chrono::system_clock::now();
        time_t tt = std::chrono::system_clock::to_time_t(today);
        return ctime(&tt);
    }
    int get_timezone(){
        int timezone = 0;
        time_t t1, t2;
        struct tm *tm_local, *tm_utc;
    
        time(&t1);//返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数
        tm_utc = gmtime(&t1);//返回的时间日期未经时区转换,格林尼治时间
        t2 = mktime(tm_utc);//转换为秒
        timezone = (t1 - t2) / 3600;
    
        return timezone;
    }

    //Get the milliseconds/seconds from epoch
    uint64_t since_epoch_msec(){
        std::chrono::time_point tp = \
            std::chrono::time_point_cast(std::chrono::system_clock::now());
        return (tp.time_since_epoch().count());
    }
    uint64_t since_epoch_sec(){
        return (static_cast(round(since_epoch_msec()/1000)));
    }
    
    //Get the duration sinch system startup
    uint64_t elapsed_usec(){
        return static_cast(std::chrono::duration_cast(std::chrono::steady_clock::now() - m_start).count());
    }   
    uint64_t elapsed_msec(){
        return static_cast(round(elapsed_usec()/1000));
    }   
    uint64_t elapsed_sec(){
        return static_cast(round(elapsed_usec()/1000000));
    }

    //restart the startup time 
    void start(){ m_start = std::chrono::steady_clock::now(); }

private:
    std::chrono::steady_clock::time_point m_start;
};

void sp(int j)
{
    SimpleTime tm;
    for(int i=1; i<100; i++)
    {
        tm.sleep_msec(2.5e3);
        cout<

你可能感兴趣的:(Linux下的时间函数)