c日期格式化操作之date

#include <time.h>
#include "date.h"
#include "gettimestamp.h"
#include "logger.h"
#include <time.h>
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#ifdef DMALLOC
#include "dmalloc.h"
#endif

typedef struct {
    time_t timestamp;
    char *format;
    char *cache;
} Date;

static const char *date_determine_format(const char *date_string);

Date *date_construct(const char *date_string) {
    Date *date = NULL;

    date = date_construct_from_timestamp(get_timestamp(date_string));

    date_set_format(date, date_determine_format(date_string));

    return date;
}

Date *date_construct_from_timestamp(time_t timestamp) {

    Date *date = NULL;

    setlocale(LC_TIME, "C");

    date = calloc(1, sizeof(Date));

    date_set_format(date, NULL);
    date->timestamp = timestamp;
    
    return date;
}

void date_destroy(Date *date) {

    if (date == NULL) {
        return;
    }

    free(date);

}

void date_set_format(Date *date, const char *format) {

    if (date == NULL) {
        return;
    }

    if (date->format != NULL) {
        free(date->format);
    }

    if (format != NULL) {
        date->format = strdup(format);
    }

    if (date->cache != NULL) {
        free((char *) date->cache);
        date->cache = NULL;
    }

}

const char *date_get_format(Date *date) {

    if (date == NULL) {
        return NULL;
    }

    if (date->format == NULL) {
        date_set_format(date, date_determine_format(NULL));
    }


    return date->format;
}

const char *date_get_value(Date *date) {
    const struct tm *tm = NULL;
    char *date_string = NULL;
    size_t retval = 0;
    time_t timestamp = 0;

    if (date == NULL) {
        return NULL;
    }

    if (date->cache != NULL) {
        return date->cache;
    }

    date_string = malloc(1024 * sizeof(char));

    timestamp = date_get_timestamp(date);
    tm = gmtime(&timestamp);

    retval = strftime(date_string, 1024, date_get_format(date), tm);

    if (retval == 0) {
        free(date_string);
        return NULL;
    }

    if (date->cache != NULL) {
        free(date->cache);
        date->cache = date_string;
    }

    return date_string;
}

time_t date_get_timestamp(Date *date) {

    if (date == NULL) {
        return 0;
    }

    return date->timestamp;

}

int date_compare(Date *date1, Date *date2) {

    if (date1 == NULL && date2 == NULL) {
        return 0;
    }

    if (date1 == NULL) {
        return -1;
    }

    if (date2 == NULL) {
        return 1;
    }

    return date_get_timestamp(date2) - date_get_timestamp(date1);
}

static const char *date_determine_format(const char *date_string) {

    static char format[128] = {0};


    /*
     * FIXME: Not robust... Assumes one of three formats
     */
    if (date_string == NULL) {              // Assume RFC 822
        strcpy(format, "%a, %d %b %Y %H:%M:%S GMT");
    } else if (date_string[3] == ',') {     // Assume RFC 822
        strcpy(format, "%a, %d %b %Y %H:%M:%S GMT");
    } else if (date_string[3] == ' '){      // Assume RFC 850
        strcpy(format, "%A, %d-%b-%y %H:%M:%S GMT");
    } else {                                // Assume asctime() format
        strcpy(format, "%a, %b %d %H:%M:%S %Y");
    }

    return format;
}
原文地址:https://www.cnblogs.com/fx2008/p/2193009.html