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 ...
随机推荐
- 获取IP城市
新浪的接口 : http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 多地域测试方法:http://int.dpool.sina. ...
- Oracle问题解决(远程登录失败)
远程机: 安装 Oracle 的计算机: 本地机: 访问远程机 Oracle 的计算机. 一.问题描述 远程机安装 Oracle 成功. 本地机配置 InstantClient 后, PLSql De ...
- jQuery折叠风琴
这个效果用jQuery做是很方便的,根本不需要搞得很复杂. 网上有些效果DEMO不仅用了jQuery还引用一两个封装文件,真是匪夷所思. 最初想到了toggle: jQuery(document).r ...
- Dungeons and Candies
Zepto Code Rush 2014:http://codeforces.com/problemset/problem/436/C 题意:k个点,每个点都是一个n * m的char型矩阵.对与每个 ...
- GPIO模拟串口注意是事项
GPIO模拟串口需要注意的事项如下:(程序见我的博客第一篇) 1.由于串口是异步通信,则串口发送必须满足宽度要求. (1)假设串口的波特率是9600bps(1s传输9600个bit),则传输1bit需 ...
- Delphi控件的透明与不透明(要挨个解释一下原因),对InvalidateControl的关键理解
procedure TForm1.Button3Click(Sender: TObject);begin if (csOpaque in ControlStyle) then ShowMessage( ...
- Spring: DispacherServlet和ContextLoaderListener中的WebApplicationContext的关系
在Web容器(比如Tomcat)中配置Spring时,你可能已经司空见惯于web.xml文件中的以下配置代码: <context-param> <param-name>cont ...
- 【HDOJ】3419 The Three Groups
记忆化搜索. /* 3419 */ #include <cstdio> #include <cstring> #include <cstdlib> #define ...
- bzoj3043
这道题完全没想出来,引自 http://blog.csdn.net/willinglive/article/details/38419573的题解 对于带有“将一段区间内的每个数全部加上某个值”这种操 ...
- 【转】Any way to implement BLE notifications in Android-L preview----不错
原文网址:http://stackoverflow.com/questions/24865120/any-way-to-implement-ble-notifications-in-android-l ...