added asn_time2UT() function

This commit is contained in:
Lev Walkin 2004-08-07 03:51:43 +00:00
parent 65c91d6cc0
commit 0aca385502
3 changed files with 56 additions and 22 deletions

View File

@ -44,7 +44,7 @@ UTCTime_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
time_t tloc;
errno = EPERM; /* Just an unlikely error code */
tloc = asn_UT2time(st, 0);
tloc = asn_UT2time(st, 0, 0);
if(tloc == -1 && errno != EPERM) {
_ASN_ERRLOG("%s: Invalid time format: %s",
td->name, strerror(errno));
@ -68,11 +68,11 @@ UTCTime_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
int ret;
errno = EPERM;
if(asn_UT2time(st, &tm) == -1 && errno != EPERM)
if(asn_UT2time(st, &tm, 1) == -1 && errno != EPERM)
return cb("<bad-value>", 11, app_key);
ret = snprintf(buf, sizeof(buf),
"%04d-%02d-%02d %02d:%02d%02d",
"%04d-%02d-%02d %02d:%02d%02d (GMT)",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec);
assert(ret > 0 && ret < (int)sizeof(buf));
@ -83,7 +83,7 @@ UTCTime_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
}
time_t
asn_UT2time(const UTCTime_t *st, struct tm *_tm) {
asn_UT2time(const UTCTime_t *st, struct tm *_tm, int as_gmt) {
char buf[17+2]; /* "AAMMJJhhmmss+hhmm" = 17, + 2 = 19 */
GeneralizedTime_t gt;
@ -106,5 +106,20 @@ asn_UT2time(const UTCTime_t *st, struct tm *_tm) {
gt.buf[1] = 0x30;
}
return asn_GT2time(&gt, _tm);
return asn_GT2time(&gt, _tm, as_gmt);
}
UTCTime_t *
asn_time2UT(UTCTime_t *opt_ut, const struct tm *tm, int force_gmt) {
GeneralizedTime_t *gt = (GeneralizedTime_t *)opt_ut;
gt = asn_time2GT(gt, tm, force_gmt);
if(gt == 0) return 0;
assert(gt->size >= 2);
gt->size -= 2;
memmove(gt->buf, gt->buf + 2, gt->size + 1);
return (UTCTime_t *)gt;
}

View File

@ -19,8 +19,12 @@ asn_struct_print_f UTCTime_print;
* Some handy helpers. *
***********************/
/* On error returns -1 and errno set to EINVAL */
struct tm; /* <time.h> */
time_t asn_UT2time(const UTCTime_t *, struct tm *_optional_tm4fill);
/* See asn_GT2time() in GeneralizedTime.h */
time_t asn_UT2time(const UTCTime_t *, struct tm *_optional_tm4fill, int as_gmt);
/* See asn_time2GT() in GeneralizedTime.h */
UTCTime_t *asn_time2UT(UTCTime_t *__opt_ut, const struct tm *, int force_gmt);
#endif /* _UTCTime_H_ */

View File

@ -5,7 +5,7 @@
#include "../constraints.c"
static void
check(char *time_str, time_t sample) {
check(char *time_str, time_t sample, int as_gmt) {
UTCTime_t gt;
struct tm tm;
time_t tloc;
@ -13,7 +13,7 @@ check(char *time_str, time_t sample) {
gt.buf = time_str;
gt.size = strlen(time_str);
tloc = asn_UT2time(&gt, &tm);
tloc = asn_UT2time(&gt, &tm, as_gmt);
printf("[%s] -> %ld == %ld\n", time_str, (long)tloc, (long)sample);
if(tloc != -1)
printf("\t%d-%d-%dT%02d:%02d:%02d %ld\n",
@ -26,29 +26,44 @@ check(char *time_str, time_t sample) {
tm.tm_gmtoff
);
assert(tloc == sample);
assert(tloc == -1 || as_gmt == 0 || tm.tm_gmtoff == 0);
if(as_gmt) check(time_str, sample, as_gmt);
}
int
main(int ac, char **av) {
check("0401250", -1);
check("0401250930", -1); /* "Z" or "(+|-)hhmm" required */
check("04012509300", -1);
check("040125093000-", -1);
check("040125093007-0", -1);
check("040125093007-080", -1);
check("0401250930.01Z", -1);
check("0401250", -1, 0);
check("0401250930", -1, 0); /* "Z" or "(+|-)hhmm" required */
check("04012509300", -1, 0);
check("040125093000-", -1, 0);
check("040125093007-0", -1, 0);
check("040125093007-080", -1, 0);
check("0401250930.01Z", -1, 0);
check("040125093007Z", 1075023007);
check("040125093007+00", 1075023007);
check("040125093007-0800", 1075051807);
check("040125093007Z", 1075023007, 0);
check("040125093007+00", 1075023007, 0);
check("040125093007-0800", 1075051807, 0);
if(ac > 1) {
/* These will be valid only inside PST time zone */
check("040125093007", 1075051807);
check("040125093000,01", 1075051800);
check("040125093000,1234", 1075051800);
check("040125093007", 1075051807, 0);
check("040125093000,01", 1075051800, 0);
check("040125093000,1234", 1075051800, 0);
}
return 0;
}
/*
* Dummy function.
*/
der_enc_rval_t
OCTET_STRING_encode_der(asn1_TYPE_descriptor_t *td, void *ptr, int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb, void *app_key) {
der_enc_rval_t erval;
return erval;
}