Unix/Linux环境C编程入门教程(28) 日期时间那些事儿
记得这个专题第一篇我们写过一个程序运行时间的程序,采用库函数提供的clock()模拟做程序测试。本篇介绍的函数也是和时间相关,但是没有clock的细致,而是提供的系统时间和日期。
1.asctime() ctime() gettimeofday() gmtime() localtime() mktime() settimeofday() time()函数介绍
|
asctime(将时间和日期以字符串格式表示) |
|
|
相关函数 |
time,ctime,gmtime,localtime |
|
表头文件 |
#include<time.h> |
|
定义函数 |
char * asctime(const struct tm * timeptr); |
|
函数说明 |
asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为:"Wed Jun 30 21:49:08 1993\n" |
|
返回值 |
若再调用相关的时间日期函数,此字符串可能会被破坏。此函数与ctime不同处在于传入的参数是不同的结构。 |
|
附加说明 |
返回一字符串表示目前当地的时间日期。 |
|
范例 |
#include <time.h> |
|
执行 |
Sat Oct 28 02:10:06 2000 |
|
|
|
|
ctime(将时间和日期以字符串格式表示) |
|
|
相关函数 |
time,asctime,gmtime,localtime |
|
表头文件 |
#include<time.h> |
|
定义函数 |
char *ctime(const time_t *timep); |
|
函数说明 |
ctime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为"Wed Jun 30 21 :49 :08 1993\n"。若再调用相关的时间日期函数,此字符串可能会被破坏。 |
|
返回值 |
返回一字符串表示目前当地的时间日期。 |
|
范例 |
#include<time.h> |
|
执行 |
Sat Oct 28 10 : 12 : 05 2000 |
|
|
|
|
gettimeofday(取得目前的时间) |
|
|
相关函数 |
time,ctime,ftime,settimeofday |
|
表头文件 |
#include <sys/time.h> |
|
定义函数 |
int gettimeofday ( struct timeval * tv , struct timezone * tz ) |
|
函数说明 |
gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中。 |
|
返回值 |
成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。 |
|
范例 |
#include<sys/time.h> |
|
执行 |
tv_sec: 974857339 |
|
|
|
|
gmtime(取得目前时间和日期) |
|
|
相关函数 |
time,asctime,ctime,localtime |
|
表头文件 |
#include<time.h> |
|
定义函数 |
struct tm*gmtime(const time_t*timep); |
|
函数说明 |
gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。 |
|
返回值 |
返回结构tm代表目前UTC 时间 |
|
范例 |
#include <time.h> |
|
执行 |
2000/10/28 Sat 8:15:38 |
|
|
|
|
localtime(取得当地目前时间和日期) |
|
|
相关函数 |
time, asctime, ctime, gmtime |
|
表头文件 |
#include<time.h> |
|
定义函数 |
struct tm *localtime(const time_t * timep); |
|
函数说明 |
localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。结构tm的定义请参考gmtime()。此函数返回的时间日期已经转换成当地时区。 |
|
返回值 |
返回结构tm代表目前的当地时间。 |
|
范例 |
#include<time.h> |
|
执行 |
2000/10/28 Sat 11:12:22 |
|
|
|
|
mktime(将时间结构数据转换成经过的秒数) |
|
|
相关函数 |
time,asctime,gmtime,localtime |
|
表头文件 |
#include<time.h> |
|
定义函数 |
time_t mktime(strcut tm * timeptr); |
|
函数说明 |
mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。 |
|
返回值 |
返回经过的秒数。 |
|
范例 |
/* 用time()取得时间(秒数),利用localtime() |
|
执行 |
time():974943297 |
|
|
|
|
settimeofday(设置目前时间) |
|
|
相关函数 |
time,ctime,ftime,gettimeofday |
|
表头文件 |
#include<sys/time.h> |
|
定义函数 |
int settimeofday ( const struct timeval *tv,const struct timezone *tz); |
|
函数说明 |
settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。注意,只有root权限才能使用此函数修改时间。 |
|
返回值 |
成功则返回0,失败返回-1,错误代码存于errno。 |
|
错误代码 |
EPERM 并非由root权限调用settimeofday(),权限不够。 |
|
|
|
|
time(取得目前的时间) |
|
|
相关函数 |
ctime,ftime,gettimeofday |
|
表头文件 |
#include<time.h> |
|
定义函数 |
time_t time(time_t *t); |
|
函数说明 |
此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。 |
|
返回值 |
成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。 |
|
范例 |
#include<time.h> |
|
执行 |
9.73E+08 |
2.小试牛刀
下面我们来看看 关于时间日期我们能做的一些常规的操作。
流程设计:
- 正确的获取到系统时间和日期
- 将获取出来的时间减去两分钟
- 再获取系统时间一次看是否设置有效
注意 root权限运行可执行程序
源代码:
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
int main()
{
// 星期天 星期一 二 三 四 五 六
const char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm *p;
struct timeval tv;
struct timezone tz;
time(&timep);
p=gmtime(&timep);
printf("%d年 %d月 %d日\n",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
//中国是东八区 所以我们在p->tm_hour + 8
printf("%s %d:%d:%d\n", wday[p->tm_wday], p->tm_hour+8, p->tm_min, p->tm_sec); gettimeofday(&tv,&tz);
printf("tv_sec:%d\n", tv.tv_sec) ;
printf("tv_usec: %d\n",tv.tv_usec); tv.tv_sec -=120;
printf("tv_sec:%d\n", tv.tv_sec) ; settimeofday(&tv,&tz);
//#include<sys/time.h>
//#include<unistd.h> // int settimeofday ( const struct timeval *tv,const struct timezone *tz);
/*timeval结构定义为:
struct timeval{
long tv_sec; 秒
long tv_usec; 微秒 timezone 结构定义为:
struct timezone{
int tz_minuteswest; 和Greenwich 时间差了多少分钟
int tz_dsttime; 日光节约时间的状态
};
*/
time_t timeq;
struct tm *q;
time(&timeq);
q=gmtime(&timeq);
printf("%d年 %d月 %d日\n",(1900+q->tm_year), (1+q->tm_mon),q->tm_mday);
//中国是东八区 所以我们在p->tm_hour + 8
printf("%s %d:%d:%d\n", wday[q->tm_wday], q->tm_hour+8, q->tm_min, q->tm_sec); return 0;
}
3.各平台的运行情况
在RHEL7上
在RHEL6上 我们不使用root用户执行可执行程序
在Solaris11上
在MAC上
好了我们效果演示完了,请大家自行试验RHEL6上用root用户执行的itcast。
Unix/Linux环境C编程入门教程(28) 日期时间那些事儿的更多相关文章
- Unix/Linux环境C编程入门教程(30) 字符串操作那些事儿
函数介绍 rindex(查找字符串中最后一个出现的指定字符) 相关函数 index,memchr,strchr,strrchr 表头文件 #include<string.h> 定义函数 c ...
- Unix/Linux环境C编程入门教程(26) 字符数字那些事儿
1.gcvt() strtod() strtol() strtoul() toascii() tolower() toupper函数介绍 gcvt(将浮点型数转换为字符串,取四舍五入) 相关函数 ec ...
- Unix/Linux环境C编程入门教程(23) 字符数字那些事儿
1.atoi 包含头文件: #include <stdlib.h> 函数原型: int atoi( const char *str ); 功能:将字符串str转换成一个整数并返回结果.参数 ...
- Unix/Linux环境C编程入门教程(29) 内存操作那些事儿
函数介绍 memccpy(拷贝内存内容) 相关函数 bcopy,memcpy,memmove,strcpy,strncpy 表头文件 #include<string.h> 定义函数 voi ...
- Unix/Linux环境C编程入门教程(32) 环境变量那些事儿
1. getenv() putenv()setenv()函数介绍 getenv(取得环境变量内容) 相关函数 putenv,setenv,unsetenv 表头文件 #include<stdli ...
- Unix/Linux环境C编程入门教程(19)Red Hat Entetprise Linux 7.0环境搭建
位架构,包括英特尔X-86_64.Power和s390.动态定时能力将降低内核内部中断数量,Open vSwitch 2.0功能可调节虚拟机之间的流量.RHEL 7中默认的文件系统是XFS,包含了一个 ...
- Unix/Linux环境C编程入门教程(5) Red Hat Enterprise Linux(RHEL)环境搭建
Unix/Linux版本众多,我们推荐Unix/Linux初学者选用几款典型的Unix/Linux操作系统进行学习. 通过./a.out ./Y.out执行出结果,证明C++程序编译成功,也就说明li ...
- Unix/Linux环境C编程入门教程(4) Debian Linux环境搭建
Unix/Linux版本众多,我们推荐Unix/Linux初学者选用几款典型的Unix/Linux操作系统进行学习. 1.广义的Debian是指一个致力于创建自由操作系统的合作组织及其作品,由于Deb ...
- Unix/Linux环境C编程入门教程(3) Oracle Linux 环境搭建
Unix/Linux版本众多,我们推荐Unix/Linux初学者选用几款典型的Unix/Linux操作系统进行学习. 2010年9月,Oracle Enterprise Linux发布新版内核--Un ...
随机推荐
- (转)使用scp命令在linux操作系统之间传递文件
一.关于scp scp是英文secure copy (remote file copy program)的简称,主要用于在两台主机之间通过网络拷贝文件.scp使用ssh协议进行数据传递,其认证方式和安 ...
- tomcat 项目部署问题
我本地Tomcat版本:Apache Tomcat/8.0.3.0 服务器端:Apache Tomcat/6.0.37 JVM都是:1.7.0_40-b43 之前项目运行正常,在我更新了一些模块后,重 ...
- logstash grok 解析Nginx
log_format main '$remote_addr [$time_local] "$request" ' '$request_body $status $body_byte ...
- 动态绑定ReportViewer控件之经验总结
以上两篇文章已经很丰富了,但是照做一遍不行,检查了N遍还是不行,就是找不出问题原因,总是提示“尚未为数据源“DataSet1_DataTable1”提供数据源实例.”这主要是说在为ReportView ...
- Android性能优化典范【转】
2015年伊始,Google发布了关于Android性能优化典范的专题,一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有关 ...
- LeeCode-Pow(x, n)
Implement pow(x, n). double myPow(double x, int n) { ) return 1.0; ) return 1.0/pow(x,-n); ); }
- Installing — pylibmc 1.2.3 documentation
Installing - pylibmc 1.2.3 documentation libmemcached
- Unity 为自己组件添加公共方法
为什么需要跟你的组件添加公共方法呢? 留一条后路嘛,万一你那天想起要给全部的组件添加一个方法. 此时我只能告诉你慢慢修改吧累死你 子组件:A ,父组件:B继承方式: A -> B –> ...
- m元素集合的n个元素子集
理论: 假设有5个元素的集点,取出3个元素的可能子集如下:{1 2 3}.{1 2 4 }.{1 2 5}.{1 3 4}.{1 3 5}.{1 4 5}.{2 3 4}.{2 3 5}.{2 4 5 ...
- android笔试题集2
1.请谈一下Android系统的架构.答:Android系统采用了分层架构,从高层到低层分别是应用程序层.应用程序框架层.系统运行库层和linux核心层. 2.谈谈android大众常用的五种布局.答 ...