ASN1_TIME_SET(3ossl)

okay bestie, sit down, hydrate, because we're doing ASN1_TIME_SET(3ossl) โ€” that's the OpenSSL manual page for the ASN.1 Time functions ๐Ÿ’– and it's a whole family reunion, like 27 functions deep. ASN.1 time is the thing certificates use to say "valid from here to there", so if you've ever wondered how OpenSSL wrangles timestamps, this is the vibe. no cap, this page is iconic.


๐ŸŒธ NAME ๐ŸŒธ

ASN1_TIME_set, ASN1_UTCTIME_set, ASN1_GENERALIZEDTIME_set, ASN1_TIME_adj, ASN1_UTCTIME_adj, ASN1_GENERALIZEDTIME_adj, ASN1_TIME_check, ASN1_UTCTIME_check, ASN1_GENERALIZEDTIME_check, ASN1_TIME_set_string, ASN1_UTCTIME_set_string, ASN1_GENERALIZEDTIME_set_string, ASN1_TIME_set_string_X509, ASN1_TIME_normalize, ASN1_TIME_to_tm, ASN1_TIME_print, ASN1_TIME_print_ex, ASN1_UTCTIME_print, ASN1_GENERALIZEDTIME_print, ASN1_TIME_diff, ASN1_TIME_cmp_time_t, ASN1_UTCTIME_cmp_time_t, ASN1_TIME_compare, ASN1_TIME_to_generalizedtime, ASN1_TIME_dup, ASN1_UTCTIME_dup, ASN1_GENERALIZEDTIME_dup โ€” literally all the ASN.1 Time functions, one big happy fam ๐Ÿ’…


โœจ SYNOPSIS โœจ

okay here's the receipts, copied exactly, don't @ me:

        ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t);
        ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t);
        ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
                                                       time_t t);

        ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t, int offset_day,
                                 long offset_sec);
        ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
                                       int offset_day, long offset_sec);
        ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
                                                       time_t t, int offset_day,
                                                       long offset_sec);

        int ASN1_TIME_set_string(ASN1_TIME *s, const char *str);
        int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str);
        int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str);
        int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s,
                                            const char *str);

        int ASN1_TIME_normalize(ASN1_TIME *s);

        int ASN1_TIME_check(const ASN1_TIME *t);
        int ASN1_UTCTIME_check(const ASN1_UTCTIME *t);
        int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *t);

        int ASN1_TIME_print(BIO *b, const ASN1_TIME *s);
        int ASN1_TIME_print_ex(BIO *bp, const ASN1_TIME *tm, unsigned long flags);
        int ASN1_UTCTIME_print(BIO *b, const ASN1_UTCTIME *s);
        int ASN1_GENERALIZEDTIME_print(BIO *b, const ASN1_GENERALIZEDTIME *s);

        int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm);
        int ASN1_TIME_diff(int *pday, int *psec, const ASN1_TIME *from,
                           const ASN1_TIME *to);

        int ASN1_TIME_cmp_time_t(const ASN1_TIME *s, time_t t);
        int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t);

        int ASN1_TIME_compare(const ASN1_TIME *a, const ASN1_TIME *b);

        ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(ASN1_TIME *t,
                                                           ASN1_GENERALIZEDTIME **out);

        ASN1_TIME *ASN1_TIME_dup(const ASN1_TIME *t);
        ASN1_UTCTIME *ASN1_UTCTIME_dup(const ASN1_UTCTIME *t);
        ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_dup(const ASN1_GENERALIZEDTIME *t);

๐Ÿ’œ DESCRIPTION ๐Ÿ’œ

okay so let's go function by function, bestie, because there's a lot of lore here ๐Ÿฆ‹

  • ASN1_TIME_set(), ASN1_UTCTIME_set() and ASN1_GENERALIZEDTIME_set() โ€” these three set the structure s to the time that the time_t value t represents. and if s is NULL? don't stress, a brand new time structure gets allocated and returned to you. main character energy, honestly โœจ
  • ASN1_TIME_adj(), ASN1_UTCTIME_adj() and ASN1_GENERALIZEDTIME_adj() โ€” these set the time structure s to the time you get offset_day and offset_sec after the time_t value t. wanna go backwards? offset_day or offset_sec can be negative to set a time before t, periodt. and offset_sec is lowkey unhinged โ€” it can exceed the number of seconds in a day, no problem. if s is NULL, a new structure gets allocated and returned ๐Ÿ’ซ
  • ASN1_TIME_set_string(), ASN1_UTCTIME_set_string() and ASN1_GENERALIZEDTIME_set_string() โ€” these set the time structure s to the time represented by the string str, which must be in the appropriate ASN.1 time format (think YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ, iykyk). if s is NULL, this function only does a format check on str. and yes, str gets copied into s ๐ŸŽ€
  • ASN1_TIME_set_string_X509() โ€” sets the ASN1_TIME structure s to the time represented by the string str, but str must be in the appropriate time format that RFC 5280 requires. translation: it only allows YYMMDDHHMMSSZ and YYYYMMDDHHMMSSZ, and leap second is rejected โ€” every other ASN.1 time format is not allowed, sorry babe. if s is NULL, it just does a format check on str ๐Ÿ‘‘
  • ASN1_TIME_normalize() โ€” converts an ASN1_GENERALIZEDTIME or ASN1_UTCTIME into a time value that can actually be used in a certificate. you should call it after the ASN1_TIME_set_string() functions and before the ASN1_TIME_print() functions so you get consistent (i.e. GMT) results. order matters bestie, it's giving choreography ๐Ÿ’ƒ
  • ASN1_TIME_check(), ASN1_UTCTIME_check() and ASN1_GENERALIZEDTIME_check() โ€” these just check the syntax of the time structure s. vibe check, but for timestamps ๐Ÿ”
  • ASN1_TIME_print(), ASN1_UTCTIME_print() and ASN1_GENERALIZEDTIME_print() โ€” these print the time structure s to BIO b in human readable format. the format is MMM DD HH:MM:SS[.s*] YYYY GMT, so like "Feb 3 00:55:52 2015 GMT", and it does not include a newline. if the time structure has an invalid format it prints out "Bad time value" and returns an error. also the output for generalized time may include a fractional part following the second ๐ŸŒธ
  • ASN1_TIME_print_ex() โ€” gives you flags to specify the output format of the datetime. it can be either ASN1_DTFLGS_RFC822 or ASN1_DTFLGS_ISO8601 ๐Ÿ’…
  • ASN1_TIME_to_tm() โ€” converts the time s to the standard tm structure. if s is NULL, then the current time gets converted. the output time is GMT. the tm_sec, tm_min, tm_hour, tm_mday, tm_wday, tm_yday, tm_mon and tm_year fields of the tm structure get set to proper values, whereas all the other fields get set to 0. if tm is NULL, this function only performs a format check on s. and heads up: if s is in Generalized format with fractional seconds, like YYYYMMDDHHMMSS.SSSZ, those fractional seconds will be lost while converting s to the tm structure ๐Ÿ˜ญ
  • ASN1_TIME_diff() โ€” sets *pday and *psec to the time difference between from and to. if to is a time later than from, then one or both (depending on the time difference) of *pday and *psec will be positive. if to is a time earlier than from, then one or both of *pday and *psec will be negative. if to and from are the same time, then *pday and *psec will both be zero. if both *pday and *psec are nonzero they will always have the same sign. the value of *psec will always be less than the number of seconds in a day. and if from or to is NULL, the current time is used โณ
  • ASN1_TIME_cmp_time_t() and ASN1_UTCTIME_cmp_time_t() โ€” these compare the two times represented by the time structure s and the time_t t. showdown, but make it timestamps ๐ŸฅŠ
  • ASN1_TIME_compare() โ€” compares the two times represented by the time structures a and b. same energy, different cast ๐Ÿ’–
  • ASN1_TIME_to_generalizedtime() โ€” converts an ASN1_TIME to an ASN1_GENERALIZEDTIME, regardless of year. if either out or *out are NULL, then a new object is allocated and it must be freed after use. don't forget, bestie, that's a leak waiting to happen ๐Ÿซถ
  • ASN1_TIME_dup(), ASN1_UTCTIME_dup() and ASN1_GENERALIZEDTIME_dup() โ€” these duplicate the time structure t and return the duplicated result correspondingly. copy-paste but make it โœจofficialโœจ

๐ŸŽ€ NOTES ๐ŸŽ€

the lore dump, part two ๐ŸŒˆ

  • The ASN1_TIME structure corresponds to the ASN.1 structure Time defined in RFC5280 et al. the time setting functions obey the rules outlined in RFC5280: if the date can be represented by UTCTime it is used, else GeneralizedTime is used. it picks for you, queen ๐Ÿ‘‘
  • The ASN1_TIME, ASN1_UTCTIME and ASN1_GENERALIZEDTIME structures are represented as an ASN1_STRING internally and can be freed up using ASN1_STRING_free() ๐Ÿงน
  • The ASN1_TIME structure can represent years from 0000 to 9999, but no attempt is made to correct ancient calendar changes (for example from Julian to Gregorian calendars). it's not doing history homework for you ๐Ÿ’€
  • ASN1_UTCTIME is limited to a year range of 1950 through 2049. that's the window, periodt ๐ŸชŸ
  • Some applications add offset times directly to a time_t value and pass the results to ASN1_TIME_set() (or equivalent). this can cause problems because the time_t value can overflow on some systems, resulting in unexpected results. new applications should use ASN1_TIME_adj() instead and pass the offset value in the offset_sec and offset_day parameters instead of directly manipulating a time_t value. don't do the math yourself bestie, let the function handle it ๐Ÿ™ˆ
  • ASN1_TIME_adj() may change the type from ASN1_GENERALIZEDTIME to ASN1_UTCTIME, or vice versa, based on the resulting year. but ASN1_GENERALIZEDTIME_adj() and ASN1_UTCTIME_adj() will not modify the type of the return structure. know the difference, it's giving plot twist ๐Ÿ”ฅ
  • It is recommended that functions starting with ASN1_TIME be used instead of those starting with ASN1_UTCTIME or ASN1_GENERALIZEDTIME. the functions starting with ASN1_UTCTIME and ASN1_GENERALIZEDTIME act only on that specific time format, whereas the functions starting with ASN1_TIME will operate on either format. flexible queen behaviour ๐Ÿ’…
  • Users familiar with RFC822 should note that when specifying the flag ASN1_DTFLGS_RFC822 the year will be formatted as documented above, i.e., using 4 digits, not 2 as specified in RFC822. not me calling out RFC822 like this ๐Ÿ˜ญ

๐Ÿ’€ BUGS ๐Ÿ’€

the fine print, bestie, read it before you ship ๐Ÿšจ

  • ASN1_TIME_print(), ASN1_UTCTIME_print() and ASN1_GENERALIZEDTIME_print() do not print out the timezone: it either prints out "GMT" or nothing. but all certificates complying with RFC5280 et al use GMT anyway, so likeโ€ฆ we move ๐Ÿšถโ€โ™€๏ธ
  • ASN1_TIME_print(), ASN1_TIME_print_ex(), ASN1_UTCTIME_print() and ASN1_GENERALIZEDTIME_print() do not distinguish if they fail because of an I/O error or an invalid time format. one error, two possible causes, no clarification ๐Ÿ’€
  • Use the ASN1_TIME_normalize() function to normalize the time value before printing to get GMT results. this is the fix, babes, use it ๐Ÿซถ

๐Ÿ‘‘ RETURN VALUES ๐Ÿ‘‘

what you actually get back, no guessing:

  • ASN1_TIME_set(), ASN1_UTCTIME_set(), ASN1_GENERALIZEDTIME_set(), ASN1_TIME_adj(), ASN1_UTCTIME_adj() and ASN1_GENERALIZEDTIME_set() return a pointer to a time structure, or NULL if an error occurred.
  • ASN1_TIME_set_string(), ASN1_UTCTIME_set_string(), ASN1_GENERALIZEDTIME_set_string() and ASN1_TIME_set_string_X509() return 1 if the time value is successfully set and 0 otherwise.
  • ASN1_TIME_normalize() returns 1 on success, and 0 on error.
  • ASN1_TIME_check(), ASN1_UTCTIME_check and ASN1_GENERALIZEDTIME_check() return 1 if the structure is syntactically correct and 0 otherwise.
  • ASN1_TIME_print(), ASN1_UTCTIME_print() and ASN1_GENERALIZEDTIME_print() return 1 if the time is successfully printed out and 0 if an I/O error occurred an error occurred (I/O error or invalid time format).
  • ASN1_TIME_to_tm() returns 1 if the time is successfully parsed and 0 if an error occurred (invalid time format).
  • ASN1_TIME_diff() returns 1 for success and 0 for failure. it can fail if the passed-in time structure has invalid syntax, for example.
  • ASN1_TIME_cmp_time_t() and ASN1_UTCTIME_cmp_time_t() return -1 if s is before t, 0 if s equals t, or 1 if s is after t. -2 is returned on error.
  • ASN1_TIME_compare() returns -1 if a is before b, 0 if a equals b, or 1 if a is after b. -2 is returned on error.
  • ASN1_TIME_to_generalizedtime() returns a pointer to the appropriate time structure on success, or NULL if an error occurred.
  • ASN1_TIME_dup(), ASN1_UTCTIME_dup() and ASN1_GENERALIZEDTIME_dup() return a pointer to a time structure, or NULL if an error occurred.

and bestie, we are not done โ€” there's more page where that came from, so don't go anywhere ๐ŸŒˆ

๐ŸŒธ EXAMPLES ๐ŸŒธ

Okay bestie, here's where it gets fun โ€” actual code you can vibe with. โœจ

๐Ÿ• Set a time structure to one hour after the current time and print it out

Like, literally "what time is it gonna be in an hour" and then shout it at the terminal. Iconic behaviour, no notes. ๐Ÿ’…

#include <time.h>
#include <openssl/asn1.h>

ASN1_TIME *tm;
time_t t;
BIO *b;

t = time(NULL);
tm = ASN1_TIME_adj(NULL, t, 0, 60 * 60);
b = BIO_new_fp(stdout, BIO_NOCLOSE);
ASN1_TIME_print(b, tm);
ASN1_STRING_free(tm);
BIO_free(b);

See how the 60 * 60 is doing the heavy lifting? That's one hour in seconds, bestie. Math ate. ๐Ÿงฎโœจ And ASN1_TIME_adj with NULL as the first arg means "make me a fresh one, don't touch anything existing" โ€” main character energy fr. And then we clean up with ASN1_STRING_free and BIO_free because we are responsible queens, periodt. ๐ŸŽ€

โณ Determine if one time is later or sooner than the current time

The classic "is this before or after now" moment. We're comparing to to here, and NULL means now, iykyk.

int day, sec;

if (!ASN1_TIME_diff(&day, &sec, NULL, to))
    /* Invalid time format */

if (day > 0 || sec > 0)
    printf("Later\n");
else if (day < 0 || sec < 0)
    printf("Sooner\n");
else
    printf("Same\n");

Lowkey the day/sec combo is such a vibe โ€” positive means later, negative means sooner, both zero means it's literally the same moment. And if ASN1_TIME_diff returns falsy? ๐Ÿšจ That's the invalid time format case, so don't you dare treat that as "Same". Guard your code, bestie. ๐Ÿ’–


๐Ÿ‘‘ HISTORY ๐Ÿ‘‘

So like, the glow-up timeline of these functions โ€” all of them dropped in OpenSSL 1.1.1, which was a moment. ๐ŸŒˆ

  • The ASN1_TIME_to_tm() function was added in OpenSSL 1.1.1.
  • The ASN1_TIME_set_string_X509() function was added in OpenSSL 1.1.1.
  • The ASN1_TIME_normalize() function was added in OpenSSL 1.1.1.
  • The ASN1_TIME_cmp_time_t() function was added in OpenSSL 1.1.1.
  • The ASN1_TIME_compare() function was added in OpenSSL 1.1.1.

Five functions, one release. That's not a debut, that's a whole era. ๐Ÿฆ‹โœจ


Copyright 2015-2025 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.

Real talk though โ€” "you may not use this file except in compliance with the License" is not a drill. Read the license, bestie. It's giving legal, not optional. ๐Ÿ’€

And that's the whole page, babes! ๐Ÿซถ This one's from OpenSSL 3.5.9, dated 2026-09-29 โ€” so yeah, fresh, current, and absolutely slaying in the ASN1_TIME_SET(3ossl) section. Go forth and handle your timestamps responsibly. Time zones can't hurt you anymore. ๐Ÿ’–โœจ๐ŸŽ€

๐Ÿ“ฆ where this page comes from