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

  1. Xref: sparky comp.unix.programmer:3821 comp.unix.questions:9238
  2. Newsgroups: comp.unix.programmer,comp.unix.questions
  3. Path: sparky!uunet!uunet.ca!geac!zooid!ross
  4. From: Ross Ridge <ross@zooid.guild.org>
  5. Subject: Re: Need (today's date + [1-7] days)
  6. Organization: ZOOiD BBS
  7. Date: Tue, 21 Jul 1992 05:27:06 GMT
  8. Message-ID: <1992Jul21.052706.11824@zooid.guild.org>
  9. Keywords: date
  10. References: <1992Jul16.145836.15586@avalon.nwc.navy.mil>
  11. Lines: 41
  12.  
  13. dejesus@archimedes.nwc.navy.mil (Francisco X DeJesus) writes:
  14. >I realize that date/time-related questions are common here, so no flames
  15. >if this has been asked before...
  16. >
  17. >If I can get today's date in a shell script or C program, how can I find
  18. >out the dates of the next week? For example, if I know today is
  19. >"Wed Dec 30 1992", how can I have the program now that the next seven days
  20. >are going to be "Thu Dec 31 1992", "Fri Jan 1 1993", etc (taking the
  21. >case in which both month and year could change).
  22.  
  23. In C something like:
  24.  
  25.     int
  26.     main(argc, argv) 
  27.     int argc;
  28.     char **argv; {
  29.         long now, time(), atol();
  30.  
  31.         now = time((long *)0);
  32.         if (argc == 2) {
  33.             now += atol(argv[1]) * 60L * 60L * 24L;
  34.         }
  35.  
  36.         puts(ctime(&now));
  37.  
  38.         return  0;
  39.     }
  40.  
  41. If you call this programme "foo", then "foo 3" will print out what
  42. the time and date will be three days from now in the same format
  43. as the date command.  The key thing here is that Unix time is
  44. kept as the number of seconds since midnight Jan 1 1970 GMT, and
  45. so it's easy to do arithmetic like adding a day.
  46.  
  47.                             Ross Ridge
  48.  
  49. -- 
  50. Ross Ridge - The Great HTMU                         l/     //
  51.                                     [OO][oo]
  52. ross@zooid.guild.org                            /()\/()/
  53. uunet.ca!zooid!ross                             db     //
  54.