Datetime strings to time_t if mktime() is unavailable

Datetime strings to time_t if mktime() is unavailable

Post by Space Invade » Sat, 23 Nov 1996 04:00:00

I recently had to come up with a workaround to mktime() on an old
SCO Xenix box on which mktime() was unavailable. Plenty of
algorithms to convert from a string representation of a date/time
to a time_t are floating around on the 'net, but most will only
get you close, usually screwing up on transitions to and from
Daylight Saving Time. Fortunately, the following let me get
around all that, and I found it "fast enough". If I get some
time, I might convert this to a real mktime() substitute for
those pre-ANSI C systems which don't have mktime(). There's
plenty of them still around, you know, often dedicated machines
running ancient vertical-market software that won't run on Linux!

/*      strtotime.c: convert string datetime to time_t using binary search
        by Joe Foster 1996

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*      arg format is ccyymmdd[hhmmss]

main(int argc, char** argv)
        int i;
        for (i = 1; i < argc; i++) {
                int year = 1900, month = 1, day = 1, hour = 0, min = 0, sec = 0;
                char target[16];

                time_t lbound = 0, ubound = 0x7fffffff, mid;

                sscanf(argv[i], "%4d%2d%2d%2d%2d%2d",
                        &year, &month, &day, &hour, &min, &sec);
                sprintf(target, "%04d%02d%02d%02d%02d%02d",
                        year, month, day, hour, min, sec);
                fprintf(stderr, "arg %d = %04d-%02d-%02d %02d:%02d:%02d = %s\n",
                        i, year, month, day, hour, min, sec, target);

                while (lbound <= ubound) {
                        struct tm* tp;
                        char attempt[16];
                        int r;

                        /* when a straight add might overflow... */
                        mid = ((lbound >> 1) + (ubound >> 1)) | (lbound & ubound & 1);

                        tp = localtime(&mid);
                        sprintf(attempt, "%04d%02d%02d%02d%02d%02d",
                                tp->tm_year + 1900, tp->tm_mon + 1, tp->tm_mday,
                                tp->tm_hour, tp->tm_min, tp->tm_sec);

                        fprintf(stderr, "mid = %s = %ld\n", attempt, (long)mid);

                        if (!(r = strcmp(target, attempt))) break; /* found it! */
                        else if (r < 0) ubound = mid - 1; /* guess was too high */
                        else if (r > 0) lbound = mid + 1; /* guess was too low */
                        else abort(); /* This had better be dead code! */
                printf("%s = %ld\n", target, (long)mid);
        return 0;



WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!

1. Looking for string->{tm,time_t} converter for SVR4

I recently purchased Consensys (vanilla) SVR4 and noticed when
compiling some applications I wrote on the sun that SVR4 lacked
a function similar to strptime() on bsd...

After looking around, I was unable to find *any* functions that
convert a String (ex: "Fri Apr 24 13:41:20") to structure tm
or return time_t

I find it hard to believe that these functions do not exist (even
after extensive 'man -k'/apropos's) -- Someone please tell me
I'm wrong...

Otherwise ... I noticed there was source for strftime posted to the
net a while back -- is there source for strptime floating around

Prefer email ; will summarize if there is interest...


2. 1024x768x256 on an Xstation 120 with a 6019 display ?

3. string -> time_t routine, anyone?

4. ethernet/modem probs on dell laptop (RH9)

5. string to time_t conversion

6. 3Com 3C9XX

7. Passing a String with an IFS without truncating

8. partitions/ibm.c

9. remove first 3 chars of a string && set IFS to newline

10. Q: getting datetime from a file

11. how to ftp a file and preserve the old datetime?

12. How to get the datetime of a file in a variable

13. datetime variable with mysql statements in shell scripts