linux 时间怎么求差值_linux如何比对两个时间差

时间一:

Thu Feb  5 13:12:18 EST 2009

时间二:

Fri Feb  6 13:11:18 EST 2009

如何计算时间差(结果为小时数)

|

#include

#include

int

main ()

{

struct tm tm, tm2;

char buf[255];

time_t t1, t2;

strptime ("Thu Feb 5 13:12:18 EST 2009", "%a %b %d %H:%M:%S EST %Y", &tm);

strptime ("Fri Feb 6 13:11:18 EST 2009", "%a %b %d %H:%M:%S EST %Y", &tm2);

t1 = mktime(&tm);

t2 = mktime(&tm2);

printf("TIME: %ld, %ld, %ldn", t2, t1, t2-t1);

return 0;

}

|

man一下就知道了

|

mktime

[convert time to arithmetic representation]

SYNOPSIS #include

time_t mktime(struct tm *timp);

DESCRIPTION mktime assumes the time at timp is a local time, and converts its

representation from the traditional representation defined by struct tm into a

representation suitable for arithmetic.

localtime is the inverse of mktime.

RETURNS If the contents of the structure at timp do not form a valid calendar time

representation, the result is -1. Otherwise, the result is the time, converted to a

time_t value.

COMPLIANCE ANSI C requires mktime.

mktime requires no supporting OS subroutines.

strftime

[flexible calendar time formatter]

SYNOPSIS #include

size_t strftime(char *s, size_t maxsize,

const char *format, const struct tm *timp);

DESCRIPTION strftime converts a struct tm representation of the time (at timp) into a

string, starting at s and occupying no more than maxsize characters.

You control the format of the output using the string at format. *format can

contain two kinds of specifications: text to be copied literally into the

formatted string, and time conversion specifications.

Time conversion specifications are two-character sequences beginning with %

(use %% to include a percent sign in the output). Each defined conversion

specification selects a field of calendar time data from *timp, and converts it

to a string; see Table 8: “Time conversion character sequences” on page 158

for more details of the character sequences for conversion.

Mon Apr 01 13:13:13 1992

Table 8: Time conversion character sequences

%a An abbreviation for the day of the week.

%A The full name for the day of the week.

%b An abbreviation for the month name.

%B The full name of the month.

%c A string representing the complete date and time, as in the following

example:

Table 9: Representations of time

%d The day of the month, formatted with two digits.

%H The hour (on a 24-hour clock), formatted with two digits.

%I The hour (on a 12-hour clock), formatted with two digits.

%j The count of days in the year, formatted with three digits (from 001

to 366).

%m The month number, formatted with two digits.

%M The minute, formatted with two digits.

%p Either AM or PM as appropriate.

%S The second, formatted with two digits

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值