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