0

I am implementing a cache in a proxy server. I am storing Expires: field from the response of request and next time if same request comes then i will compare the Expires time with current time. Now I am stuck here. how should I convert Expire time format to seconds and then take a difference? Is that any other way to get time difference ?

Format for Expire time is : Sat, 10 Nov 2012 07:05:26 GMT

Suppose I have two time, how can I compare two times in GMT?

4

2 回答 2

1

这是一个工作示例:

#include <stdio.h>
#include <time.h>

int main(int argc, char **argv) {
    struct tm t;
    time_t seconds;
    const char *expire = "Sat, 10 Nov 2012 07:05:25 GMT";
    strptime(expire, "%a, %d %B %Y %H:%M:%S", &t);
    seconds = mktime(&t);
    printf("%lu\n", seconds);
}

请注意,您的时间必须是 GMT 时间,因为 strptime() 忽略时区(至少在 Linux 上)。您可能还需要将您的语言环境设置为与您的输入时间相同的语言环境。

于 2012-10-11T07:41:48.340 回答
0

如果您的实现有它,您可以使用该strptime函数将该字符串转换为 a struct tm,然后将mktime转换为 a以便于比较。time_t

于 2012-10-11T07:17:38.400 回答