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 ...
随机推荐
- C# 如何查看源程序的IL代码
1.打开microsoft visual studio 2008 / visual studio tools / visual studio 2008 命令提示 ,并输入ilda ...
- 【Mysql 调用存储过程,输出参数的坑】
玛德,数据行都返回过来了,就是没有输出参数!!! 扒官方设计文档:这么一段 雷死人了!!! When a stored procedure is called using MySqlCommand.E ...
- Linux和windows动态库
转载:http://www.cnblogs.com/chio/archive/2008/11/13/1333119.html 态链接库技术实现和设计程序常用的技术,在Windows和Linux系 统中 ...
- 2015第10周三jquery ui position
jQuery UI API - .position() 所属类别 方法重载(Method Overrides) | 方法(Methods) | 实用工具(Utilities) 用法 描述:相对另一个元 ...
- HDU 3634 City Planning (离散化)
City Planning Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- Mod_python: The Long Story
mod_python: the long story - Grisha Trubetskoy Mod_python: The Long Story Oct 25th, 2013 | Comments ...
- JAVA I/O流 之入门
I/O流分类: 根据处理的数据类型不同 字节流 字符流 根据流向不同 输入流 输出流 根据功能不同 节点流:直接与数据源相连,读入或读出. 处理流:直接使用节点流,读写不方便,为了更快的读写文件,才有 ...
- cocos2dx lua 学习笔记(二)
安装开发环境 sublime - http://www.sublimetext.com/2 package control - http://packagecontrol.io/installatio ...
- Unity 鼠标点击左右移动,人物跟随旋转
上代码: using UnityEngine; using System.Collections; public class Test : MonoBehaviour { private Vector ...
- Ubuntu亮度无法调节或调节无法保存的问题
装了搜狗输入法之后,系统设置里面的很多软件都没有了.以前屏幕太亮在电源里面可以调节,现在不行了.没办法,只能找其他的办法了. 在网上查了很多资料,经自己的实验,找到了一个成功的方法. 首先进入 /sy ...