home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / programm / 3831 < prev    next >
Encoding:
Text File  |  1992-07-21  |  1.7 KB  |  52 lines

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