时间编程

1、时间存储类型

(1)存储日历时间类型(日历时间是指自1970年1月1号0点到现在的所经历的秒数)

time_t

(2)存储时间结构体

struct tm {
    int tm_sec;         /* 秒,范围从 0 到 59 */
    int tm_min;         /* 分,范围从 0 到 59 */
    int tm_hour;        /* 小时,范围从 0 到 23 */
    int tm_mday;        /* 一月中的第几天,范围从 1 到 31 */
    int tm_mon;         /* 月份,范围从 0 到 11 */
    int tm_year;        /* 自 1900 起的年数 */
    int tm_wday;        /* 一周中的第几天,范围从 0 到 6 */
    int tm_yday;        /* 一年中的第几天,范围从 0 到 365 */
    int tm_isdst;       /* 夏令时 */   
};

(3)存储时间秒数和微秒数的结构体

struct timeval{
    int tv_sec;         /* 秒数 */
    int tv_usec;        /* 微妙数 */
};

2、时间类型

(1)世界时间

Coordinateed Universal Time (UTC) : 世界标准时间,也就是大家熟知的格林威治标准时间(Greenwich Mean Time, GMT).

(2)日历时间

日历时间是指从一个标准时间点(1970年1月1日0点)到此时经过的秒数来表达的时间。


3、日历时间

time_t time(char *now);

若 now 为非NULL,则将日历时间保存在 now, 否则做为返回值。


4、时间转换

struct tm *gmtime(const time_t *now);

功能:将日历时间作为参数传入,并将日历时间转换为格林威治标准时间,并保存至tm结构体。

struct tm *localtime(const time_t *now);
功能:将日历时间转换为本地时间,并保存至tm结构体。


5、时间显示

char *asctime(const struct tm *now);

功能:将tm结构的时间转换为字符串形式。如:星期 月 日 小时:分:秒 年

char *ctime(const time_t *now);

功能:将日历时间转换为本地时间的字符串形式。


6、获取时间

int gettimeofday(struct timeval *tv, struct timezone *tz);

功能:获取从今日凌晨到现在的时间差,常用于计算时间耗时。(一般timezone 为 NULL 即可)


7、延时执行

unsigned int sleep(unsigned int sec);

功能:使程序睡眠sec秒。

void usleep(unsigned long usec);

功能:使程序睡眠usec微秒。


8、demo

#include <stdio.h>
#include <time.h>
#include <sys/time.h>

int main(int argc, const char *argv[])
{
    int i, j, sum;
    time_t now;
    struct tm *local;
    struct tm *world;
    struct timeval timeBegin;
    struct timeval timeEnd;
    float timeUse;
    char *timelt;
    char *timewd;

    now = time(NULL);             //获取日历时间
    printf("now = %ld\n", now);

    local = localtime(&now);      //将日历时间转换成当地时间的tm结构存储
    printf("local : %d:%d:%d\n", local->tm_hour, local->tm_min, local->tm_sec);

    world = gmtime(&now);         //将日历时间转换成世界时间的tm结构存储
    printf("world : %d:%d:%d\n", world->tm_hour, world->tm_min, world->tm_sec);

    timelt = ctime(&now);         //将日历时间直接转换成本地时间的字符从形式
    printf("local time : %s\n", timelt);

    timewd = asctime(world);      //我以为是将世界时间的tm格式转换成世界时间的字符串形式,可是结果却是当地时间的字符串形式
    printf("world time : %s\n", timewd);

    timewd = asctime(local);      //将tm格式的时间转换成当地时间的字符串形式
    printf("local time : %s\n", timewd);

    /* 计算双重for循环的运算时间,即从开始到结束的时间差 */
    gettimeofday(&timeBegin, NULL);

    for (i = 0; i < 5; i++){
        for (j = 0; j < 100000000; j++){
            sum += 1;
        }
    }

    gettimeofday(&timeEnd, NULL);

    timeUse = ((timeEnd.tv_sec - timeBegin.tv_sec) * 1000000 + (timeEnd.tv_usec - timeBegin.tv_usec)) / 1000000;

    printf("time use : %f s ", timeUse);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值