Linux时间编程

简介: Linux时间编程

第1关:Linux获取时间

/*************************************************************************
    > File Name: GetTimeTest.c
    > Author: ma6174
    > Mail: ma6174@163.com 
    > Created Time: Tue 17 Apr 2018 08:42:30 PM CST
 ************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <time.h>
typedef struct _localtimestruct
{
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;
}localtimestruct;
time_t gettimesecond (void)
{
    /*************Begin***********/
    time_t seconds = time (NULL);
    return seconds;
    /**************End************/
}
void getlocaltv (localtimestruct *ltinfo)
{
    /*************Begin***********/
    time_t current = time(NULL);
    struct tm *tv = localtime(&current);
    ltinfo->year = tv->tm_year + 1900;
    ltinfo->month = tv->tm_mon + 1;
    ltinfo->day = tv->tm_mday;
    ltinfo->hour = tv->tm_hour;
    ltinfo->minute = tv->tm_min;
    ltinfo->second = tv->tm_sec;
    /**************End************/
}


第2关:Linux时间相互转换

/*************************************************************************
    > File Name: TimeTransTest.c
    > Author: ma6174
    > Mail: ma6174@163.com 
    > Created Time: Tue 17 Apr 2018 10:13:59 PM CST
 ************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
typedef struct _localtimestruct
{
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;
}localtimestruct;
void func (void);
void timetrans (time_t current, char *time_str, localtimestruct *ltinfo, struct tm *tm_time)
{
    /*************Begin***********/
    if (NULL != time_str)
    {
            memcpy (time_str, ctime(&current), strlen(ctime(&current)));
    }
    if(NULL != tm_time)
    {
            memcpy(tm_time, gmtime(&current), sizeof(struct tm));
            if (NULL != ltinfo)
            {
                    ltinfo->year = tm_time->tm_year + 1900;
                    ltinfo->month = tm_time->tm_mon + 1;
                    ltinfo->day = tm_time->tm_mday;
                    ltinfo->hour = tm_time->tm_hour;
                    ltinfo->minute = tm_time->tm_min;
                    ltinfo->second = tm_time->tm_sec;
            }
    }
    /**************End************/ 
}
long getdifftimeval (void)
{
    /*************Begin***********/
    struct timeval start;
    struct timeval end;
    gettimeofday(&start, NULL);
    func();
    gettimeofday(&end, NULL);
    long timeval_s = (end.tv_sec * 1000 * 1000 + end.tv_usec) - (start.tv_sec * 1000 * 1000 + start.tv_usec);
    return timeval_s;
    /**************End************/
}


第3关:Linux 时间定时器

/*************************************************************************
    > File Name: TimerTest.c
    > Author: ma6174
    > Mail: ma6174@163.com 
    > Created Time: Thu 19 Apr 2018 02:58:54 PM CST
 ************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
void func (void);
static int count = 0;//the number of calling loopevent 
void loopevent (int signo)
{
    /************Begin************/
    if(SIGPROF == signo)
    {
        func ();
        if(4 > count)
        {
            count++;
        }
        else
        {
            struct itimerval new_it;
            new_it.it_interval.tv_sec = 0;
            new_it.it_interval.tv_usec = 0;
            new_it.it_value.tv_sec = 0;
            new_it.it_value.tv_usec = 0;
            setitimer (ITIMER_PROF, &new_it, NULL);
        }
    }
    /*************End*************/
}
void setlocaltimer (void)
{
    /************Begin************/
    struct itimerval new_it;
    struct itimerval old_it;
    struct sigaction act;
    /*register signal function*/
    act.sa_handler = loopevent;
    act.sa_flags = 0;
    sigemptyset (&act.sa_mask);
    sigaction (SIGPROF, &act, NULL);
    new_it.it_interval.tv_sec = 1;
    new_it.it_interval.tv_usec = 0;
    new_it.it_value.tv_sec = 3;
    new_it.it_value.tv_usec = 0;
    setitimer (ITIMER_PROF, &new_it, &old_it);
    /*************End*************/
}
目录
相关文章
|
21天前
|
Linux C语言
Linux c编程(20210608)
Linux c编程(20210608)
|
19天前
|
网络协议 Linux Python
Python网络编程基础(Socket编程)epoll在Linux下的使用
【4月更文挑战第12天】在上一节中,我们介绍了使用`select`模块来实现非阻塞IO的方法。然而,`select`模块在处理大量并发连接时可能会存在性能问题。在Linux系统中,`epoll`机制提供了更高效的IO多路复用方式,能够更好地处理大量并发连接。
|
2天前
|
存储 Unix Linux
【Linux系统编程】基础指令(三)
【Linux系统编程】基础指令(三)
|
2天前
|
Linux
【Linux系统编程】基础指令(二)(下)
【Linux系统编程】基础指令(二)
|
2天前
|
Linux C语言
【Linux系统编程】基础指令(二)(上)
【Linux系统编程】基础指令(二)
|
2天前
|
Linux
【Linux系统编程】基础指令(一)(下)
【Linux系统编程】基础指令(一)
|
2天前
|
人工智能 Unix Linux
【Linux系统编程】基础指令(一)(上)
【Linux系统编程】基础指令(一)
|
2天前
|
Unix 大数据 Linux
【Linux系统编程】Linux背景知识
【Linux系统编程】Linux背景知识
|
2天前
|
安全 Linux Shell
Linux:探索开源之魅与编程之道
Linux:探索开源之魅与编程之道
12 4
|
6天前
|
消息中间件 关系型数据库 MySQL
Linux:开源之魅与编程之道
Linux:开源之魅与编程之道
14 1