linux时间编程学习

来源:互联网 发布:matlab 矩阵转置 编辑:程序博客网 时间:2024/05/01 03:30
/*************************************************************************
> File Name: time.c
> Author: 
> Mail: 
> Created Time: 2017年04月02日 星期日 10时18分26秒
 ************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>//时间函数库
int main()
{
    /*time_t    是长整型*/ 
    time_t *t=(time_t *)malloc(sizeof(t));
    struct tm *p=(struct tm *)malloc(sizeof(p));
    //原型是time_t time(timet);返回一个1970年1月1日到系统时间总共多少秒;
   /*两种调用方法
           time_t *t;
    第一种:*t = time((time_t *)NULL);
    第二种:time(t);
    */  
    time(t);
    printf("%ld\n",*t);
    /*原型:struct tm *gmtime (const time_t *timep);
    功能:将time_t表示的时间转换为格林尼治标准时间,保存到tm结构体中
    */ 
    p=gmtime(t);
    printf("gmtime is hour is %d\n",p->tm_hour);
    /**
    * 原型:struct tm *gmtime(const time_t timep);
    * 功能:返回tm格式的当地时间,与gmtime区别是gmtime返回UTC时间
    * */
    p = localtime(t);
    printf("Local hour is %d\n",p->tm_hour);
    /**
    *原型:char *ctime(const time_t *timep);
    *功能:将time_t格式时间转化后以字符串形式返回
    * */
    printf("ctime is %s",ctime(t));
    /**
    * 函数原型:struct tm asctime(struct *tm);
    * 函数功能:与ctime功能一样的,只是参数是tm结构体指针
    * */
    printf("asctime is %s",asctime(p));
    /**
    * 获取精确时间函数:
    * int gettimeofday(struct timeval *tv,struct timezone *tz)
    * 结构体在time.h中声明了
    * satruct timeva{
    *   long tv_sec;      //当前时间秒数
    *   long tv_usec;     //当前时间微秒数
    * }
    *
    * struct timezone{
    *   int tz_minutesswest;        //与utc时间相差的分钟数
    *   int tz_dsttime;             //与夏令时相差的分钟数
    * }
    * */ 
    free(t);
    //free(p);//释放这个就出现段错误


     return 0;
}
0 0