ctime() backwards 
Author Message
 ctime() backwards

Does anyone know of a C function or lib that can take in an ASCII date and give
the unix 32-bit unsigned long date?  This is what the function ctype() does,
but in reverse.  Thanks.


Mon, 17 Sep 2001 03:00:00 GMT  
 ctime() backwards

Quote:

> Does anyone know of a C function or lib that can take in an ASCII date and give
> the unix 32-bit unsigned long date?  This is what the function ctype() does,
> but in reverse.

The argument for ctype() need not be a pointer to an unsigned long. The Standard
specifies type time_t. So to have portable code you would need to declare the
function:

time_t rctime(const char *s);
Here is one way you coulde define your function.

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

time_t rctime(const char *date) {
   int i;
   time_t ttime;
   struct tm t = {0},*ptm;
   char *s,buf[40],month[5],time[10];
   const static char *marr[] = {"Jan","Feb","Mar","Apr",
                                 "May","Jun","Jul","Aug",
                                 "Sep","Oct","Nov","Dec"};
   strcpy(buf,date);
   if(4 != sscanf(buf,"%*s %4s %d %9s %d",month,&t.tm_mday,
                              time,&t.tm_year)) return (time_t)-1;
   for(i = 0; i < 12;i++) if(strcmp(marr[i],month) == 0) break;
   if(i == 12) return (time_t)-1;
   t.tm_mon = i;
   if((s = strtok(time,":")) == NULL) return (time_t)-1;
   t.tm_hour = atoi(s);
   if((s = strtok(NULL,":")) == NULL) return (time_t)-1;
   t.tm_min = atoi(s);
   if((s = strtok(NULL,":")) == NULL) return (time_t)-1;
   t.tm_sec = atoi(s);
   t.tm_year -= 1900;
   if((ttime = mktime(&t)) == (time_t)-1) return ttime;
   ptm = localtime(&ttime);
   return mktime(ptm);
   }

int main(void) {
   time_t now = time(NULL),date;
   if(now == (time_t)-1) return 1;
   date = rctime(ctime(&now));
   if(date != (time_t)-1)
      puts(ctime(&date));
   return 0;
   }

--
Al Bowers
Tampa, FL  USA

http://www.gate.net/~abowers/



Mon, 17 Sep 2001 03:00:00 GMT  
 ctime() backwards
Thanks guys.


Tue, 18 Sep 2001 03:00:00 GMT  
 ctime() backwards
Hi Al,

I'm trying to obtain the unsigned long value for dates up to and including Dec 31,
9999 23:59:59.  I do plan to be around then by the way ;).  I took your snippet of
code and it works great up to the mktime() call.  mktime() is unable to handle dates
this far out.  Now, since time_t is defined as long, I found every occurrence of its
definition in all the header files in the package I'm using (Visual C/C++) and
changed it to unsigned long.  But I guess it must not be that trivial a matter.  Do
you have any suggestion for handling such dates?

--
Robert A. Nurse
Web Development
RCN Corporation

ICQ Network Number 15566746
http://members.tripod.com/~rnurse



Tue, 25 Sep 2001 03:00:00 GMT  
 ctime() backwards

Quote:

>Hi Al,

>I'm trying to obtain the unsigned long value for dates up to and including
Dec 31,
>9999 23:59:59.

No way. Not portably, at any rate. Not using the standard C functions.

To handle dates this big, you're going to need to write your own handlers.

Consider a system which uses seconds as its unit of time granularity (this
is not an uncommon scenario).

unsigned long typically holds 32 bits. Let's call it 4000000000. Okay,
divide that by 80000 (seconds in a day, rounded down a bit to make the math
easy and favouring your intended use), to get 50000 days. Divide by about
300 to get years without tears (massive loss of accuracy, again favouring
your intended use), gives 170-or-so years absolute max. So I think 9999 AD
is a bit optimistic. Nothing to stop you writing your own code to handle
dates like that, but I suggest you give up on unsigned long straight away.

Quote:
>  I do plan to be around then by the way ;).

Don't we all!

Quote:
>Now, since time_t is defined as long, I found every occurrence of its
>definition in all the header files in the package I'm using (Visual C/C++)
and
>changed it to unsigned long.  But I guess it must not be that trivial a

matter.

Ouch! I strongly suggest you reinstall your implementation. An
implementation's header files should be regarded as read-only, constant,
sacrosanct. Did you somehow think that by changing the header files, you
would change the library binaries?

Quote:
>Do you have any suggestion for handling such dates?

Cut code. Explore the idea of Julian dates.

Richard H



Tue, 25 Sep 2001 03:00:00 GMT  
 ctime() backwards


Quote:
>Hi Al,

>I'm trying to obtain the unsigned long value for dates up to and including Dec 31,
>9999 23:59:59.  I do plan to be around then by the way ;).  I took your snippet of
>code and it works great up to the mktime() call.  mktime() is unable to handle dates
>this far out.  Now, since time_t is defined as long, I found every occurrence of its
>definition in all the header files in the package I'm using (Visual C/C++) and
>changed it to unsigned long.  But I guess it must not be that trivial a matter.  Do

 ^^^^^^^^^^^^^^^^^^^^^^^^^^^

Quote:
>you have any suggestion for handling such dates?

Eeek! Changing the definitions in a vendor-supplied header file is
*really* treading on thin ice! What happens when supplied library
functions are depending on definitions and limitations as stated in
the original headers and you have no way of knowing whether or not
this is the case? Anybody?

--

Cameron Foster
http://www.fosterreaves.com

http://www2.netcom.com/~cdfoster/



Wed, 26 Sep 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. ctime() backwards

2. Backwards Compatibility or Not

3. Reading logfiles backwards?

4. NUMBER SYSTEM CONVERSION PRINTS BACKWARDS - HELP?

5. Making a C API backwards compatible

6. Moving Backwards through Link List

7. paging backwards in a file...? help

8. For those who like doing things backwards

9. Reading a file backwards

10. Moving backwards in time using struct tm

11. how to write an entered no backwards ?

12. : Going backwards in linked structs?

 

 
Powered by phpBB® Forum Software