Linux系统编程_5_获取系统时间

Linux环境中时间编程函数:

比较常用的是ctime与localtime

char *ctime(const time_t *timep);

char *ctime_r(const time_t *timep, char *buf);

struct tm *localtime(const time_t *timep);

struct tm *localtime_r(const time_t *timep, struct tm *result);

Linux环境中时间编程结构体:

struct tm
 {
               int tm_sec;         /* seconds */
               int tm_min;         /* minutes */
               int tm_hour;        /* hours */
               int tm_mday;        /* day of the month */
               int tm_mon;         /* month */
               int tm_year;        /* year */
               int tm_wday;        /* day of the week */
               int tm_yday;        /* day in the year */
               int tm_isdst;       /* daylight saving time */
 };

Linux系统中获取系统当前时间的两种方法比较:

#include <stdio.h>
#include <time.h>

int main()
{
    time_t nTime;
    struct tm *stTime;
    char *str;

    str = ctime(&nTime);
    printf("Time: %s\n", str);

    time(&nTime);
    stTime = localtime(&nTime);
    printf("Current Time: %d:%d:%d,%d:%d:%d\n", stTime->tm_year+1900, stTime->tm_mon, stTime->tm_mday,
            stTime->tm_hour, stTime->tm_min, stTime->tm_sec);

    return 0;
}

打印:

Time: Sun Apr  7 04:47:40 1974

Current Time: 2014:10:15,15:39:44

注意,两种方法输出时间不一样

时间: 2024-12-24 15:54:46

Linux系统编程_5_获取系统时间的相关文章

Linux下用C获取当前时间

Linux下用C获取当前时间,具体如下: 代码(可以把clock_gettime换成time(NULL)) ? 1 2 3 4 5 6 7 8 9 10 void getNowTime() {  timespec time;  clock_gettime(CLOCK_REALTIME, &time); //获取相对于1970到现在的秒数  tm nowTime;  localtime_r(&time.tv_sec, &nowtime);  char current[1024];  

VC++编程中获取系统时间

<span style="white-space:pre"> </span>总结了在程序中如何获得系统时间的方法 void CGetSystenTimeDlg::OnBnClickedGettimeButton() { // TODO: 在此添加控件通知处理程序代码 //方法一 使用MFC的CTime类 CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime(); str=tm.Format("

Linux设备驱动程序 之 获取当前时间

墙上时间 内核一般通过jiffies来获取当前时间,该数值表示的是最近一次系统启动到当前的时间间隔,它和设备驱动程序无关,因为它的声明期只限于系统的运行期:但是驱动程序可以用jiffies来计算不同事件之间的间隔:通常,利用jiffies值来测量时间间隔已经足够了,如果要测量更短的时间差,只能使用处理器特定的寄存器了,但这会带来严重的兼容性问题 驱动程序一般不需要知道墙钟时间,通常只有cron和syslogd这样的用户程序才使用,对真实世界时间的处理最好留给用户空间,C函数库提供了更好的支持:这

Linux编程(获取系统时间)

#include <stdio.h> #include <time.h> int main() { time_t now; struct tm *w; time(&now); w=localtime(&now); printf("%04d/%02d/%02d\n%02d:%02d:%02d\n",w->tm_year+1900, w->tm_mon+1,w->tm_mday,w->tm_hour,w->tm_min,

【Linux环境编程】获取网卡的实时网速

在windows下面,我们可以看到360或者是qq安全卫士的"安全球",上面显示实时的网速情况.那么在linux里面如何获取网卡的实时网速?其实原理很简单,读取需要获取网速的网卡在某段时间dT内流量的变化dL,那么实时网速就出来了,Speed = dL / dt. linux在ifaddrs.h中提供了函数: /* Create a linked list of `struct ifaddrs' structures, one for each network interface on

Linux/UNIX编程:获取指定用户所有正在运行的进程ID和进程名

先用系统函数 `getpwnam` 获得指定用户名的 UID,然后遍历 /proc/ 中所有 PID 目录,如果 /proc/PID/status 中的 UID 是输入用户名对应的 UID 则输出该 status 文件中的进程名,进程ID就是目录名. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <dirent.h> #

Linux驱动中获取系统时间

最近在做VoIP方面的驱动,总共有16个FXS口和FXO口依次初始化,耗用的时间较多.准备将其改为多线程,首先需要确定哪个环节消耗的时间多,这就需要获取系统时间. #include <linux/time.h> /*头文件*/ struct timeval time_now; unsigned long int time_num;//获取的时间 do_gettimeofday(&time_now); time_num = time_now.tv_sec*1000+time_now.tv

Linux C 语言 获取系统时间信息

比如获取当前年份:        /* 获取当前系统时间 暂时不使用        int iyear = 0;        int sysyear = 0;        time_t now;        struct tm *timenow;        time(&now);        timenow = localtime(&now);        sysyear = timenow->tm_year+1900;        */linux下获取系统时间的方法

Linux C 获取系统时间信息

比如获取当前年份:               /* 获取当前系统时间 暂时不使用 int iyear = 0; int sysyear = 0; time_t now; struct tm *timenow; time(&now); timenow = localtime(&now); sysyear = timenow->tm_year+1900; */ linux下获取系统时间的方法 可以用 localtime 函数分别获取年月日时分秒的数值. Linux下获得系统时间的C语言