home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.unix.programmer:3831 comp.unix.questions:9251
- Path: sparky!uunet!pipex!slxsys!xon!michael
- From: michael@xon.co.uk (Michael J Marshall)
- Newsgroups: comp.unix.programmer,comp.unix.questions
- Subject: Re: Need (today's date + [1-7] days)
- Message-ID: <1236@dorothy.xon.co.uk>
- Date: 21 Jul 92 08:47:21 GMT
- References: <1992Jul20.185321.12811@ncmicro.lonestar.org>
- Organization: X-ON Software - London, England
- Lines: 39
- X-Newsreader: Tin 1.1 PL4
-
- ltf@ncmicro.lonestar.org (Lance Franklin) writes:
- : In article <1992Jul16.145836.15586@avalon.nwc.navy.mil> dejesus@archimedes.nwc.navy.mil (Francisco X DeJesus) writes:
- : }I realize that date/time-related questions are common here, so no flames
- : ...
-
- Many UNIX/ANSI systems have an even easier way to calculate relative dates,
- using the mktime() function:
-
- time_t mktime( struct tm * );
-
- First you get a starting date (easiest with time() and localtime()):
-
- time_t now;
- struct tm t1;
-
- time(&now);
- t1 = *localtime( &now );
-
- To get the date say 9 days from now:
-
- t1->tm_mday += 9;
- mktime( &t1 );
-
- mktime() will accept values 'out of range' in the fields and adjust them -
- so it deals nicely if you add 9 days to 29 June (for example). It's clean,
- it's elegant, and you don't have to deal with leap years, days in the
- month, or any math at all.
-
- Be careful if you subtract, though. SCO UNIX's mktime() is buggy when
- dealing with negative values in the struct tm.
-
- I hope this answers your questions.
-
- Michael
- = Michael John Marshall - michael@xon.co.uk = B0ftg+k-s(+)h-r-
- = X-ON Software - 21 Great Tower St, London EC3R 5AQ ENGLAND
- = H 071 281 7056 - W 071 522 0088
- = Last night I played poker with a deck of Tarot cards -
- = I got a full house and four people died - Steven Wright
-