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语言的实现方法:
1. 可以用 localtime 函数分别获取年月日时分秒的数值。
#include<time.h> //C语言的头文件
#include<stdio.h> //C语言的I/O
void main()
{
time_t now; //实例化time_t结构
struct tm *timenow; //实例化tm结构指针
time(&now);
//time函数读取现在的时间(国际标准时间非北京时间),然后传值给now
timenow = localtime(&now);
//localtime函数把从time取得的时间now换算成你电脑中的时间(就是你设置的地区)
printf("Local time is %s/n",asctime(timenow));
//上句中asctime函数把时间转换成字符,通过printf()函数输出
}
注释:time_t是一个在time.h中定义好的结构体。而tm结构体的原形如下:
struct tm
{
int tm_sec;//seconds 0-61
int tm_min;//minutes 1-59
int tm_hour;//hours 0-23
int tm_mday;//day of the month 1-31
int tm_mon;//months since jan 0-11
int tm_year;//years from 1900
int tm_wday;//days since Sunday, 0-6
int tm_yday;//days since Jan 1, 0-365
int tm_isdst;//Daylight Saving time indicator
};
2. 对某些需要较高精准度的需求,Linux提供了gettimeofday()。
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
int main(int argc, char **argv)
{
struct timeval start,stop,diff;
gettimeofday(&start,0);
//做你要做的事...
gettimeofday(&stop,0);
tim_subtract(&diff,&start,&stop);
printf("总计用时:%d毫秒/n",diff.tv_usec);
}
int tim_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
{
int nsec;
if ( x->tv_sec > y->tv_sec )
return -1;
if ((x->tv_sec==y->tv_sec) && (x->tv_usec>y->tv_usec))
return -1;
result->tv_sec = ( y->tv_sec-x->tv_sec );
result->tv_usec = ( y->tv_usec-x->tv_usec );
if (result->tv_usec<0)
{
result->tv_sec--;
result->tv_usec+=1000000;
}
return 0;
}
Linux C 语言 获取系统时间信息的更多相关文章
- Linux C 获取系统时间信息
比如获取当前年份: /* 获取当前系统时间 暂时不使用 ; ; time_t now; struct tm *timenow; time(&now); timeno ...
- Linux驱动中获取系统时间
最近在做VoIP方面的驱动,总共有16个FXS口和FXO口依次初始化,耗用的时间较多.准备将其改为多线程,首先需要确定哪个环节消耗的时间多,这就需要获取系统时间. #include <linux ...
- C语言 获取系统时间与睡眠时间函数
摘要: 以ms为单位,获取系统时间.睡眠或延迟时间函数的使用方法. #include<stdio.h> #include <time.h> #include <sys/t ...
- linux下C获取系统时间的方法
asctime(将时间和日期以字符串格式表示) 相关函数 time,ctime,gmtime,localtime 表头文件 #include 定义函数 char * asctime(const ...
- C语言 - 获取系统时间 以年月日时分秒的形式输出
ESP32需要给下位机通过UART发送时间戳,形式是年月日时分秒的十六进制数据包. #include <stdio.h> #include <time.h> int main( ...
- Linux编程(获取系统时间)
#include <stdio.h> #include <time.h> int main() { time_t now; struct tm *w; time(&no ...
- Linux sysinfo获取系统相关信息
Linux中,可以用sysinfo来获取系统相关信息. #include <stdio.h> #include <stdlib.h> #include <errno.h& ...
- c++ 如何获取系统时间 - zjnig711的信息仓库 - 博客频道 - CSDN.NET
c++ 如何获取系统时间 - zjnig711的信息仓库 - 博客频道 - CSDN.NET c++ 如何获取系统时间 分类: C/C++ 2008-05-08 22:15 14115人阅读 评论(5 ...
- Android获取系统时间方法的总结
Android获取系统时间方法的方法有很多种,常用的有Calendar.Date.currentTimeMills等方法. (1)Calendar Calendar获取系统时间首先要用Calendar ...
随机推荐
- ROW_NUMBER() OVER函数的基本用法用法
ROW_NUMBER() OVER函数的基本用法用法 转自:http://www.cnblogs.com/icebutterfly/archive/2009/08/05/1539657.html 语法 ...
- php之文件上传类代码
/* 单个文件上传 功能 上传文件 配置允许的后缀 配置允许的大小 获取文件后缀 判断文件的后缀 报错 */ class UpTool{ protected $allowExt = 'jpg,jpeg ...
- php学习之基础语法
这些语法都是在学习视频的过程中整理出来的,有些很简单的语法可能就没有整理了,只是记录了自己看来比较重要的语法内容. 1.变量使用 $ 声明 ,变量区分大小写 变量的类型: 4种标量类 ...
- YII Query Builder
今天遇到一个Query Builder 联合查询问题: 查询关联表某个字段的总数
- Python 异常处理--raise函数用法
raise语句手工引发一个异常: "raise" [expression ["," expression ["," expression]] ...
- 安卓SDK Manager自动管理各种包
安卓ADT不能自动从google下载sdk等各种工具 修改hosts文件下载成功 210.242.125.89 dl-ssl.google.com 210.242.125.89 dl.google ...
- 3、MyBatis.Net学习笔记之增删改
增删改之前先说一下笔记1里提到的一个无法创建ISqlMapper对象的问题. <resultMaps> <resultMap id="FullResultMap" ...
- python 性能鸡汤
转载自:http://www.oschina.net/question/1579_45822 1:使用内建函数input() int() isinstance() issubclass() iter( ...
- ROR入门之旅
mac上为了不在登录画面看到其他账户,我禁用了root账户,而每次用Terminal的时候,先获得sudo账户的权限: sudo -s mac本身就安装有ruby ruby -v 查看当前安装的rub ...
- RMQ with Shifts
uva12299:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...