时间函数

结构tm的定义为
#ifndef _TM_DEFINED
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代表一月) - 取值区间为[0,11] */
        int tm_year;    /* 年份,其值等于实际年份减去1900 */
        int tm_wday;    /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,       以此类推 */
        int tm_yday;    /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
        int tm_isdst;   /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
};
#define _TM_DEFINED
#endif
1. ctime(将时间和日期以字符串格式表示)---asctime 
定义函数  char *ctime(const time_t *timep);
表头文件  #include<time.h>
函数说明  ctime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为“Wed Jun 30 21 :49 :08 1993/n”。若再调用相关的时间日期函数,此字符串可能会被破坏。
 
返回值  返回一字符串表示目前当地的时间日期。
程序例:
#include<time.h>
#include<stdio.h>
void main()
{
    time_t timep;
    time (&timep);
    printf("%s",ctime(&timep));
}
2. gmtime(取得目前时间和日期) 
表头文件  #include<time.h>
定义函数  struct tm*gmtime(const time_t*timep);
函数说明  gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。
程序例程:
#include <time.h>
main()
{
    char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
    time_t timep;
    struct tm *p;
   time(&timep);
    p=gmtime(&timep);
    printf("%d/%d/%d ",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
    printf("%s %d:%d:%d/n", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}

3. time
用法:time_t time(time_t *t)
功能:此函数返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t并非空指针,此函数也会将返回值存到t指针所指的内存。
返回值:成功,返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。
程序例:
#include<stdio.h>
#include<time.h>
void main()
{
    int seconds= time((time_t*)NULL);
    printf("%d/n",seconds);
}

4. localtime
用 法: struct tm *localtime(const time_t *clock);
功 能: localtime()将参数clock所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。结构tm的定义请参考gmtime()。此函数返回的时间日期已经转换成当地时区。

程序例:

#include<time.h>
main()
{
    char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
    time_t timep;
    struct tm *p;
    time(&timep);
    p=localtime(&timep); /*取得当地时间*/
    printf ("%d/%d/%d ", (1900+p->tm_year),(1+p->tm_mon), p->tm_mday);
    printf("%s %d:%d:%d/n", wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);
}

4. asctime
用  法: char *asctime(const struct tm *tblock);
功  能: asctime()将参数tblock所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为:Wed Jun 30 21:49:08 1993
程序例:
#include <stdio.h>
#include <string.h>
#include <time.h>

int main(void)
{
   struct tm t;
   char str[80];

   /* sample loading of tm structure  */

   t.tm_sec    = 1;  /* Seconds */
   t.tm_min    = 30; /* Minutes */
   t.tm_hour   = 9;  /* Hour */
   t.tm_mday   = 22; /* Day of the Month  */
   t.tm_mon    = 11; /* Month (0-11)*/
   t.tm_year   = 56; /* Year - does not include century */
   t.tm_wday   = 4;  /* Day of the week  */
   t.tm_yday   = 0;  /* Does not show in asctime  */
   t.tm_isdst  = 0;  /* Is Daylight SavTime; does not show in asctime */

   /* converts structure to null terminated
   string */

   strcpy(str, asctime(&t));
   printf("%s/n", str);

   return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值