当前位置: 技术问答>linux和unix
linux如何比对两个时间差
来源: 互联网 发布时间:2016-05-16
本文导语: 时间一: 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...
时间一:
Thu Feb 5 13:12:18 EST 2009
时间二:
Fri Feb 6 13:11:18 EST 2009
如何计算时间差(结果为小时数)
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;
}
#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
[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