moved timetoa() from asn1.c to types.c

This commit is contained in:
Andreas Steffen 2006-05-29 07:02:12 +00:00
parent a8a1fa1c03
commit c2e7442fc1
4 changed files with 53 additions and 24 deletions

View File

@ -17,16 +17,11 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include "types.h"
#include "asn1.h" #include "asn1.h"
#include <utils/logger_manager.h> #include <utils/logger_manager.h>
/* Names of the months */
static const char* months[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
/* some common prefabricated ASN.1 constants */ /* some common prefabricated ASN.1 constants */
static u_char ASN1_INTEGER_0_str[] = { 0x02, 0x00 }; static u_char ASN1_INTEGER_0_str[] = { 0x02, 0x00 };
static u_char ASN1_INTEGER_1_str[] = { 0x02, 0x01, 0x01 }; static u_char ASN1_INTEGER_1_str[] = { 0x02, 0x01, 0x01 };
@ -214,23 +209,6 @@ bool is_printablestring(chunk_t str)
return TRUE; return TRUE;
} }
/**
* Display a date either in local or UTC time
*/
void timetoa(char *buf, size_t buflen, const time_t *time, bool utc)
{
if (*time == 0)
snprintf(buf, buflen, "--- -- --:--:--%s----", (utc)?" UTC ":" ");
else
{
struct tm *t = (utc)? gmtime(time) : localtime(time);
snprintf(buf, buflen, "%s %02d %02d:%02d:%02d%s%04d",
months[t->tm_mon], t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec,
(utc)?" UTC ":" ", t->tm_year + 1900);
}
}
/** /**
* Converts ASN.1 UTCTIME or GENERALIZEDTIME into calender time * Converts ASN.1 UTCTIME or GENERALIZEDTIME into calender time
*/ */

View File

@ -133,6 +133,5 @@ extern u_char* build_asn1_object(chunk_t *object, asn1_t type, size_t datalen);
extern chunk_t asn1_integer_from_mpz(const mpz_t value); extern chunk_t asn1_integer_from_mpz(const mpz_t value);
extern chunk_t asn1_simple_object(asn1_t tag, chunk_t content); extern chunk_t asn1_simple_object(asn1_t tag, chunk_t content);
extern chunk_t asn1_wrap(asn1_t type, const char *mode, ...); extern chunk_t asn1_wrap(asn1_t type, const char *mode, ...);
extern chunk_t timetoasn1(const time_t *time, asn1_t type);
#endif /* _ASN1_H */ #endif /* _ASN1_H */

View File

@ -21,6 +21,7 @@
*/ */
#include <string.h> #include <string.h>
#include <time.h>
#include "types.h" #include "types.h"
@ -44,6 +45,19 @@ mapping_t status_m[] = {
{MAPPING_END, NULL} {MAPPING_END, NULL}
}; };
#define UNDEFINED_TIME 0
/**
* @brief Display a date either in local or UTC time
*
* @param buf buffer where displayed time will be written
* @param buflen buffer length
* @param time time to be displayed
* @param utc UTC (TRUE) or local time (FALSE)
*
*/
void timetoa(char *buf, size_t buflen, const time_t *time, bool utc);
/** /**
* Empty chunk. * Empty chunk.
*/ */
@ -138,3 +152,29 @@ void *clalloc(void * pointer, size_t size)
return (data); return (data);
} }
/*
* Names of the months used by timetoa()
*/
static const char* months[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
/*
* Described in header file
*/
void timetoa(char *buf, size_t buflen, const time_t *time, bool utc)
{
if (*time == UNDEFINED_TIME)
snprintf(buf, buflen, "--- -- --:--:--%s----", (utc)?" UTC ":" ");
else
{
struct tm *t = (utc)? gmtime(time) : localtime(time);
snprintf(buf, buflen, "%s %02d %02d:%02d:%02d%s%04d",
months[t->tm_mon], t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec,
(utc)?" UTC ":" ", t->tm_year + 1900);
}
}

View File

@ -193,5 +193,17 @@ void chunk_to_hex(char *buf, size_t buflen, chunk_t chunk);
*/ */
void *clalloc(void *pointer, size_t size); void *clalloc(void *pointer, size_t size);
#define UNDEFINED_TIME 0
/**
* @brief Display a date either in local or UTC time
*
* @param buf buffer where displayed time will be written to
* @param buflen buffer length
* @param time time to be displayed
* @param utc UTC (TRUE) or local time (FALSE)
*
*/
void timetoa(char *buf, size_t buflen, const time_t *time, bool utc);
#endif /*TYPES_H_*/ #endif /*TYPES_H_*/