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);
//不同的版本函数名称可能有些不一样//ubuntu 10.10 上的函数是timersub(&start,&stop,&diff);printf("总计用时:%d毫秒/n",diff.tv_usec); } int tim_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
//int timersub( struct timeval *x, struct timeval *y,struct timeval *result)
{ 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 语言 获取系统时间信息
比如获取当前年份: /* 获取当前系统时间 暂时不使用 int iyear = 0; int sysyear = 0; time_t now; ...
- Linux sysinfo获取系统相关信息
Linux中,可以用sysinfo来获取系统相关信息. #include <stdio.h> #include <stdlib.h> #include <errno.h& ...
- linux下获取系统时间 和 时间偏移
获取linux时间 并计算时间偏移 void getSystemTimer(void){#if 0 char *wdate[]={"Sun","Mon",&q ...
- linux中获取系统时间的几种方法
asctime(将时间和日期以字符串格式表示) 相关函数 time,ctime,gmtime,localtime 表头文件 #include<time.h> 定义函数 char * asc ...
- linux中获取系统时间 gettimeofday函数
linux的man页中对gettimeofday函数的说明中,有这样一个说明: $ man gettimeofday DESCRIPTION The functions gettimeof ...
- linux c 获取系统时间
#include <time.h> main() { time_t timep; time (&timep); printf(“%s”,asctime(gmtime(&ti ...
- c++ 如何获取系统时间 - zjnig711的信息仓库 - 博客频道 - CSDN.NET
c++ 如何获取系统时间 - zjnig711的信息仓库 - 博客频道 - CSDN.NET c++ 如何获取系统时间 分类: C/C++ 2008-05-08 22:15 14115人阅读 评论(5 ...
- Linux驱动中获取系统时间
最近在做VoIP方面的驱动,总共有16个FXS口和FXO口依次初始化,耗用的时间较多.准备将其改为多线程,首先需要确定哪个环节消耗的时间多,这就需要获取系统时间. #include <linux ...
- Android获取系统时间方法的总结
Android获取系统时间方法的方法有很多种,常用的有Calendar.Date.currentTimeMills等方法. (1)Calendar Calendar获取系统时间首先要用Calendar ...
随机推荐
- Spark(七) -- Scala快速入门
Scala作为Spark的开发语言,想要成为Spark高手,精通Scala是必须要走的一条路 然后一门语言并不是你想精通就能够精通的,更何况是Scala这种面向对象又面向函数的编程语言,个人觉得其学习 ...
- 最短路径——Floyd,Dijkstra(王道)
题目描述: 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线 ...
- Kinect 1.8 体感开发,手势,姿态(Pose) 捕捉判断方法以及一些辅方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- idea tomcat 怎样出现update classes and resources
idea Tomcat 出现update classes and resources 出现热加载 正确配置应该是这个 在 Deployment (调度,部署) 中点击 + 选择war explored ...
- 谋哥:玩App怎么赚钱(三)
谋哥每天坚持写文章,如今写作速度是越来越快了,当然这样也能节省点时间.只是坚持每天写,确实须要极大的耐力和毅力,由于偶然事件会影响你心情和灵感.只是我一直相信秦刚老师(微信/QQ1111884 )说的 ...
- Android平台Camera实时滤镜实现方法探讨(十)--代码地址以及简单介绍(20160118更新)
简单做了个相机和图片编辑模块,时间原因非常多功能还没有做.尚有BUG,见谅,将在以后抽时间改动 代码地址 PS:请点个Star^-^ --------------------------------- ...
- Selenium webdriver Java 操作chrome 浏览器
Step1: 下载chromedriver. 下载路径: http://chromedriver.storage.googleapis.com/index.html 选择一个合适的下载即可.我下载的是 ...
- 微信小程序 - 文字走马灯
转载于csdn maid_04,总之多谢了!节省了不少时间呢 最近在做一个类似uu跑腿的项目,时间也特别紧,搞完以后再继续贴地图代码(高德.腾讯) 以下代码拷贝即可用,拿走谢谢上面的人吧(~.~) w ...
- 让C#可以像Javascript一样操作Json DynamicJson
http://blog.csdn.net/lisenyang/article/details/52535314
- Spring Boot整合shiro-登录认证和权限管理
原文地址:http://www.ityouknow.com/springboot/2017/06/26/springboot-shiro.html 这篇文章我们来学习如何使用Spring Boot集成 ...