打印
[应用相关]

时间编程一些知识

[复制链接]
375|14
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
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;      // 日光节约时间
};

使用特权

评论回复
5
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;
}

使用特权

评论回复
6
junpeng324|  楼主 | 2019-3-18 21:08 | 只看该作者
运行效果如下:

使用特权

评论回复
7
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)
功能:将日历时间转化为本地时间的字符串形式

使用特权

评论回复
8
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;
}

使用特权

评论回复
9
junpeng324|  楼主 | 2019-3-18 21:09 | 只看该作者
运行效果如下图

使用特权

评论回复
10
junpeng324|  楼主 | 2019-3-18 21:09 | 只看该作者
Int gettimeofday(struct timeval *tv,struct timezone *tz)
功能:获取从今日凌晨到现在的时间差,常用于计算时间耗时。
Struct timeval{
  Int  tv_sec;   // 秒
  Int  tv_usec;  //微秒数
};

使用特权

评论回复
11
junpeng324|  楼主 | 2019-3-18 21:37 | 只看该作者
6.延时执行
  unsigned int sleep(unsigned int seconds)
  功能:是程序睡眠seconds秒
  void usleep(unsigned long usec)
  功能:使程序睡眠usec微秒

使用特权

评论回复
12
磨砂| | 2019-4-8 11:45 | 只看该作者
感谢楼主分享

使用特权

评论回复
13
晓伍| | 2019-4-8 12:45 | 只看该作者
怎么标定当前时间呢

使用特权

评论回复
14
八层楼| | 2019-4-8 12:49 | 只看该作者
应该是联网解决

使用特权

评论回复
15
观海| | 2019-4-8 12:52 | 只看该作者
函数体是自己写的吗

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

37

主题

1130

帖子

8

粉丝