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 ...
 
随机推荐
- (转)Vue.use源码分析
			
我想有过vue开发经验的,对于vue.use并不陌生.当使用vue-resource或vue-router等全局组件时,必须通过Vue.use方法引入,才起作用.那么vue.use在组件引入之前到底做 ...
 - Eclipse+Maven远程部署项目到Tomcat中
			
使用maven的自动部署功能可以很方便的将maven工程自动打包并且部署到远程tomcat服务器,省去一些繁琐的操作,节省大量时间. 我使用的tomcat版本是8.5,tomcat7和tomcat8都 ...
 - 怎样优化cocos2d/x程序的内存使用和程序大小
			
再次感谢原创者:Steffen Itterheim.原创博客原文地址: http://www.learn-cocos2d.com/2012/11/optimize-memory-usage-bundl ...
 - 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何让不同的PLC程序分线程运行 TC3
			
右击Tasks,添加一个新的Task 可以为这个线程设置自定义的扫描周期 然后在项目上右击添加Referenced Task 在TaskSub1上右击添加现有项,把之气写好的PRG程序绑定 ...
 - MPJoystick
			
using UnityEngine; /** * File: MPJoystick.cs * Author: Chris Danielson of (monkeyprism.com) * // USE ...
 - 修改注册表实现Windows自动登陆
			
昨天再修一条case时无意间发现这个case竟然要重启机器,并且要用指定的账户自动登陆Windows.然后就发现了,简单的修改下注册表就可以完成自动登陆了. 首先,在“run”里输入“regedit” ...
 - 访问vector元素方法的效率比较(转)
			
LInux下: gcc 4.47,red hat6 #include<iostream> #include<vector> #include<time.h> usi ...
 - VC 使用json cpp 静态库 问题解决
			
release使用 json 静态库 提示 fatal error C1083: 无法打开编译器生成的文件:“../../build/vs71/release/lib_json\json_writer ...
 - 转:介绍一个好用的抓取dump的工具-ProcDump
			
介绍一个好用的抓取dump的工具-ProcDump Procdump是一个轻量级的Sysinternal团队开发的命令行工具, 它的主要目的是监控应用程序的CPU异常动向, 并在此异常时生成crash ...
 - 使用apxs安装apache模块
			
使用apxs安装apache模块 ---by石锅拌饭 1.缘由 前几天迁移系统.发现配置了fastcgi的一个脚本下载文件总是提示类似Connection reset ...