[应用相关] 时间编程一些知识

[复制链接]
675|14
 楼主| junpeng324 发表于 2019-3-18 21:06 | 显示全部楼层 |阅读模式
1.时间类型:
01.世界标准时间(格林威治标准时间):Coordinated Universal Time(UTC)
02.日历时间(Calendar Time)

 楼主| junpeng324 发表于 2019-3-18 21:06 | 显示全部楼层
2.时间获取:
01. time_t  time(time_t *tloc)  #include<time.h>
功能:获取日历时间,即从1970年1月1日0点到现在所经历的秒数。
 楼主| junpeng324 发表于 2019-3-18 21:07 | 显示全部楼层
3.时间转换
struct tm *gmtime(const time_t*timep)
功能:将日历时间转化为格林威治标准时间,并保存至TM结构
Struct tm *localtime(const time_t *timep)
功能:将日历时间转化为本地时间,并保存至TM结构
 楼主| junpeng324 发表于 2019-3-18 21:07 | 显示全部楼层
4.时间保存
struct tm{
int tm_sec;     // 秒
int tm_min;     // 分
int tm_hour;    //小时
int tm_mday;   //本月第几日
int tm_mon;    //本年第几月
int tm_year;    //哪一年= tm_year+1900
int tm_wday;     // 本周第几日
int tm_yday;      // 本年第几日
int tm_isdst;      // 日光节约时间
};
 楼主| junpeng324 发表于 2019-3-18 21:07 | 显示全部楼层
时间获取例子:time1.c
#include<stdio.h>
#include<time.h>
int main()
{
  ttruct tm *logcal;
  time_t t;
t=time(NULL);
local =localtime(&t);
printf(“local time is: %d,%d,%d\n”,local->tm_hour,local->tm_min,local->tm_sec);
return 0;
}
 楼主| junpeng324 发表于 2019-3-18 21:08 | 显示全部楼层
运行效果如下:
194125c8f98306d060.png
 楼主| junpeng324 发表于 2019-3-18 21:08 | 显示全部楼层
5.时间显示
char *asctime(const struct tm *tm)
功能:将tm格式的时间转化为字符串,如:Sat Jul 30 08:43:03 2005
char *ctime(const time_t *timep)
功能:将日历时间转化为本地时间的字符串形式
 楼主| junpeng324 发表于 2019-3-18 21:08 | 显示全部楼层
例:time2.c
#include<stdio.h>
#include<time.h>
int main()
{
struct tm *ptr;
time_t lt;
lt=time(NULL);
ptr=gmtime(&lt);
printf(asctime(ptr));
printf(ctime(&lt));
return 0;
}
 楼主| junpeng324 发表于 2019-3-18 21:09 | 显示全部楼层
运行效果如下图
241675c8f9872056b2.png
 楼主| junpeng324 发表于 2019-3-18 21:09 | 显示全部楼层
Int gettimeofday(struct timeval *tv,struct timezone *tz)
功能:获取从今日凌晨到现在的时间差,常用于计算时间耗时。
Struct timeval{
  Int  tv_sec;   // 秒
  Int  tv_usec;  //微秒数
};
 楼主| junpeng324 发表于 2019-3-18 21:37 | 显示全部楼层
6.延时执行
  unsigned int sleep(unsigned int seconds)
  功能:是程序睡眠seconds秒
  void usleep(unsigned long usec)
  功能:使程序睡眠usec微秒
磨砂 发表于 2019-4-8 11:45 | 显示全部楼层
感谢楼主分享
晓伍 发表于 2019-4-8 12:45 | 显示全部楼层
怎么标定当前时间呢
八层楼 发表于 2019-4-8 12:49 | 显示全部楼层
应该是联网解决
观海 发表于 2019-4-8 12:52 | 显示全部楼层
函数体是自己写的吗
您需要登录后才可以回帖 登录 | 注册

本版积分规则

37

主题

1130

帖子

8

粉丝
快速回复 在线客服 返回列表 返回顶部