Back to index

avfs  1.0.1
Defines | Functions | Variables
dates.c File Reference
#include <sys/types.h>
#include <time.h>
#include <stdio.h>
#include "ne_alloc.h"
#include "dates.h"

Go to the source code of this file.

Defines

#define RFC1123_FORMAT   "%3s, %02d %3s %4d %02d:%02d:%02d GMT"
#define RFC1036_FORMAT   "%s, %2d-%3s-%2d %2d:%2d:%2d GMT"
#define ASCTIME_FORMAT   "%3s %3s %2d %2d:%2d:%2d %4d"

Functions

char * rfc1123_date (time_t anytime)
time_t rfc1123_parse (const char *date)
time_t rfc1036_parse (const char *date)
time_t asctime_parse (const char *date)

Variables

static const char * rfc1123_weekdays [7]
static const char * short_months [12]

Define Documentation

#define ASCTIME_FORMAT   "%3s %3s %2d %2d:%2d:%2d %4d"

Definition at line 52 of file dates.c.

#define RFC1036_FORMAT   "%s, %2d-%3s-%2d %2d:%2d:%2d GMT"

Definition at line 50 of file dates.c.

#define RFC1123_FORMAT   "%3s, %02d %3s %4d %02d:%02d:%02d GMT"

Definition at line 48 of file dates.c.


Function Documentation

time_t asctime_parse ( const char *  date)

Definition at line 126 of file dates.c.

{
    struct tm gmt = {0};
    int n;
    static char wkday[4], mon[4];
    n = sscanf(date, ASCTIME_FORMAT,
              wkday, mon, &gmt.tm_mday, 
              &gmt.tm_hour, &gmt.tm_min, &gmt.tm_sec,
              &gmt.tm_year);
    /* portable to check n here? */
    for (n=0; n<12; n++)
       if (strcmp(mon, short_months[n]) == 0)
           break;
    /* tm_mon comes out as 12 if the month is corrupt, which is desired,
     * since the mktime will then fail */
    gmt.tm_mon = n;
    return mktime(&gmt);
}
time_t rfc1036_parse ( const char *  date)

Definition at line 103 of file dates.c.

{
    struct tm gmt = {0};
    int n;
    static char wkday[10], mon[4];
    /* RFC850/1036 style dates: Sunday, 06-Nov-94 08:49:37 GMT */
    n = sscanf(date, RFC1036_FORMAT,
              wkday, &gmt.tm_mday, mon, &gmt.tm_year,
              &gmt.tm_hour, &gmt.tm_min, &gmt.tm_sec);
    /* portable to check n here? */
    for (n=0; n<12; n++)
       if (strcmp(mon, short_months[n]) == 0)
           break;
    /* tm_mon comes out as 12 if the month is corrupt, which is desired,
     * since the mktime will then fail */
    gmt.tm_mon = n;
    return mktime(&gmt);
}
char* rfc1123_date ( time_t  anytime)

Definition at line 64 of file dates.c.

                                   {
    struct tm *gmt;
    char *ret;
    gmt = gmtime(&anytime);
    if (gmt == NULL)
       return NULL;
    ret = ne_malloc(29 + 1); /* dates are 29 chars long */
/*  it goes: Sun, 06 Nov 1994 08:49:37 GMT */
    snprintf(ret, 30, RFC1123_FORMAT,
             rfc1123_weekdays[gmt->tm_wday], gmt->tm_mday, 
             short_months[gmt->tm_mon], 1900 + gmt->tm_year, 
             gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
    
    return ret;
}
time_t rfc1123_parse ( const char *  date)

Definition at line 82 of file dates.c.

{
    struct tm gmt = {0};
    static char wkday[4], mon[4];
    int n;
/*  it goes: Sun, 06 Nov 1994 08:49:37 GMT */
    n = sscanf(date, RFC1123_FORMAT,
           wkday, &gmt.tm_mday, mon, &gmt.tm_year, &gmt.tm_hour,
           &gmt.tm_min, &gmt.tm_sec);
    /* Is it portable to check n==7 here? */
    gmt.tm_year -= 1900;
    for (n=0; n<12; n++)
       if (strcmp(mon, short_months[n]) == 0)
           break;
    /* tm_mon comes out as 12 if the month is corrupt, which is desired,
     * since the mktime will then fail */
    gmt.tm_mon = n;
    return mktime(&gmt);
}

Variable Documentation

const char* rfc1123_weekdays[7] [static]
Initial value:
 { 
    "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" 
}

Definition at line 54 of file dates.c.

const char* short_months[12] [static]
Initial value:
 { 
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
}

Definition at line 57 of file dates.c.