Linux下的时间

1,time_t 类型。一个整数,表示从1970-01-01到现在的秒数
获得其值的方法:
#include<time.h>
time_t time(time_t *t);

如果t不为空则把的得到的结果填入t所指的内存中去
2,struct timeval .表示从1970-01-01到现在经历的时间。可以精确到微秒

#include<time.h>
struct timeval{
    long tv_sec; /*秒*/
    long tv_usec; /*微秒*/
    };
  struct timezone{
    int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/
    int tz_dsttime; /*日光节约时间的状态*/
};

获取方法:
#include <sys/time.h>
#include <unistd.h>
int gettimeofday ( struct timeval * tv , struct timezone * tz )
int settimeofday(const struct timeval *tv, const struct timezone *tz);

3,struct tm 详细的日期和时间
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};

目前没有发现有直接过去该时间值的系统调用。
4,各个时间方式直接的转换
时间转string:
char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);
char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);

time_t与struct tm互转:
struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);
time_t mktime(struct tm *tm);

其他转换:
size_t strftime(char *s, size_t max, const char *format,const struct tm *tm); //根据format和tm生成时间字符串
char *strptime(const char *s, const char *format, struct tm *tm); //根据s和format生成tm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值