home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16577 < prev    next >
Encoding:
Internet Message Format  |  1992-11-15  |  2.0 KB

  1. Path: sparky!uunet!munnari.oz.au!bruce.cs.monash.edu.au!monu6!escargot!goanna!ok
  2. From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: using variables of type date in UNIX
  5. Message-ID: <15974@goanna.cs.rmit.oz.au>
  6. Date: 16 Nov 92 05:57:30 GMT
  7. References: <92314.235928U17868@uicvm.uic.edu>
  8. Organization: Comp Sci, RMIT, Melbourne, Australia
  9. Lines: 37
  10.  
  11. In article <92314.235928U17868@uicvm.uic.edu>, U17868@uicvm.uic.edu writes:
  12. > I want to know how I can use the variable of type date in C.
  13.  
  14. There is no built-in "date" type in C.
  15. There is, however, a type time_t defined in ANSI C (in Classic C, use
  16. 'long') and some handy functions for manipulating these values.  The
  17. reason that I say that there is no "date" type is that time_t has a
  18. finer grain (seconds) and typically doesn't have much range.
  19.  
  20. To find out what you have in your UNIX system, explore
  21.     % man ctime
  22.  
  23. > For example, how could I define a variable like
  24. > "todays_date" to be of type date and then read the input in the format like
  25. >                        11/09/92
  26.  
  27. That is an exceptionally bad format to use.  To me, that date uniquely
  28. and unambiguously means the 11th day of the 9th month of the 92nd year (of
  29. the current century).  If you can possibly arrange for dates to be entered
  30. in a format like
  31.     1992.09.11
  32. do so.  If not, prompt for day, month, and year separately.
  33.  
  34. There is a standard function called strftime() which takes time_t values
  35. and converts them to strings according to a format.  There is not, however,
  36. any _standard_ function for converting in the opposite direction.  The
  37. standard DOES include a function
  38.     time_t mktime(struct tm *tptr)
  39. which normalises the time record and converts the result to a single number.
  40. You can do
  41.     struct tm xxx;  time_t t;
  42.     xxx.tm_year = 92, xxx.tm_month = 09-1, xxx.tm_day = 11;
  43.     xxx.tm_hour = 0, xxx.tm_min = 0, xxx.tm_sec = 0;
  44.     t = mktime(&xxx);
  45.  
  46. Alas, mktime() is _still_ not available in many UNIX C libraries.  Beware
  47. of C compilers that support ANSI _syntax_ but not all the ANSI _library_.
  48.