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*************/
}
目录
打赏
0
0
0
0
4
分享
相关文章
如何在阿里云的linux上搭建Node.js编程环境?
本指南介绍如何在阿里云Linux服务器(Ubuntu/CentOS)上搭建Node.js环境,包含两种安装方式:包管理器快速安装和NVM多版本管理。同时覆盖全局npm工具配置、应用部署示例(如Express服务)、PM2持久化运行、阿里云安全组设置及外部访问验证等步骤,助你完成开发与生产环境的搭建。
|
1月前
|
Linux编程: 在业务线程中注册和处理Linux信号
本文详细介绍了如何在Linux中通过在业务线程中注册和处理信号。我们讨论了信号的基本概念,并通过完整的代码示例展示了在业务线程中注册和处理信号的方法。通过正确地使用信号处理机制,可以提高程序的健壮性和响应能力。希望本文能帮助您更好地理解和应用Linux信号处理,提高开发效率和代码质量。
57 17
|
1月前
|
Linux编程: 在业务线程中注册和处理Linux信号
通过本文,您可以了解如何在业务线程中注册和处理Linux信号。正确处理信号可以提高程序的健壮性和稳定性。希望这些内容能帮助您更好地理解和应用Linux信号处理机制。
63 26
Linux shell编程学习笔记30:打造彩色的选项菜单
Linux shell编程学习笔记30:打造彩色的选项菜单
嵌入式Linux系统编程 — 5.3 times、clock函数获取进程时间
在嵌入式Linux系统编程中,`times`和 `clock`函数是获取进程时间的两个重要工具。`times`函数提供了更详细的进程和子进程时间信息,而 `clock`函数则提供了更简单的处理器时间获取方法。根据具体需求选择合适的函数,可以更有效地进行性能分析和资源管理。通过本文的介绍,希望能帮助您更好地理解和使用这两个函数,提高嵌入式系统编程的效率和效果。
156 13
深入理解Linux系统下的Shell脚本编程
【10月更文挑战第24天】本文将深入浅出地介绍Linux系统中Shell脚本的基础知识和实用技巧,帮助读者从零开始学习编写Shell脚本。通过本文的学习,你将能够掌握Shell脚本的基本语法、变量使用、流程控制以及函数定义等核心概念,并学会如何将这些知识应用于实际问题解决中。文章还将展示几个实用的Shell脚本例子,以加深对知识点的理解和应用。无论你是运维人员还是软件开发者,这篇文章都将为你提供强大的Linux自动化工具。
Linux shell编程学习笔记82:w命令——一览无余
Linux shell编程学习笔记82:w命令——一览无余
|
6月前
|
Linux系统编程:掌握popen函数的使用
记得在使用完 `popen`打开的流后,总是使用 `pclose`来正确关闭它,并回收资源。这种做法符合良好的编程习惯,有助于保持程序的健壮性和稳定性。
284 6
|
6月前
|
Linux系统编程:掌握popen函数的使用
记得在使用完 `popen`打开的流后,总是使用 `pclose`来正确关闭它,并回收资源。这种做法符合良好的编程习惯,有助于保持程序的健壮性和稳定性。
241 3
敏捷与瀑布的对决:解析Xamarin项目管理中如何运用敏捷方法提升开发效率并应对市场变化
【8月更文挑战第31天】在数字化时代,项目管理对软件开发至关重要,尤其是在跨平台框架 Xamarin 中。本文《Xamarin 项目管理:敏捷方法的应用》通过对比传统瀑布方法与敏捷方法,揭示敏捷在 Xamarin 项目中的优势。瀑布方法按线性顺序推进,适用于需求固定的小型项目;而敏捷方法如 Scrum 则强调迭代和增量开发,更适合需求多变、竞争激烈的环境。通过详细分析两种方法在 Xamarin 项目中的实际应用,本文展示了敏捷方法如何提高灵活性、适应性和开发效率,使其成为 Xamarin 项目成功的利器。
85 1