home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / programm / 3951 < prev    next >
Encoding:
Internet Message Format  |  1992-07-28  |  2.9 KB

  1. Xref: sparky comp.unix.programmer:3951 comp.unix.questions:9476
  2. Newsgroups: comp.unix.programmer,comp.unix.questions
  3. Path: sparky!uunet!usc!sdd.hp.com!mips!mips!munnari.oz.au!metro!cs.uow.edu.au!cssc-woll.tansu.com.au!cssc-syd.tansu.com.au!colinl
  4. From: colinl@cssc-syd.tansu.com.au (Colin Lynch)
  5. Subject: Re: Need (today's date + [1-7] days)
  6. Message-ID: <1992Jul28.072025.13886@cssc-syd.tansu.com.au>
  7. Keywords: date
  8. Organization: Customised Software Solutions Centres, AOTC
  9. References: <1992Jul16.145836.15586@avalon.nwc.navy.mil> <1992Jul19.063945.2322@metapro.DIALix.oz.au>
  10. Date: Tue, 28 Jul 1992 07:20:25 GMT
  11. Lines: 69
  12.  
  13. bernie@metapro.DIALix.oz.au (Bernd Felsche) writes:
  14.  
  15. >In <1992Jul16.145836.15586@avalon.nwc.navy.mil>
  16. >   dejesus@archimedes.nwc.navy.mil (Francisco X DeJesus) writes:
  17.  
  18. >>If I can get today's date in a shell script or C program, how can I find
  19. >>out the dates of the next week? For example, if I know today is
  20. >>"Wed Dec 30 1992", how can I have the program now that the next seven days
  21. >>are going to be "Thu Dec 31 1992", "Fri Jan 1 1993", etc (taking the
  22. >>case in which both month and year could change).
  23.  
  24. >I usually get away with fiddling with the timezone.
  25. >Unfortunately, some operating systems ignore hour offsets
  26. >greater than 12 hours from UTC.
  27.  
  28.     ...The most reliable way to do this is to first convert the current
  29. date/time into a time_t type ("time_t" is defined in the system header file
  30. <time.h>, and is basically a long containing the number of seconds since the
  31. start of 01 Jan 1970 on most systems):
  32.  
  33.        #include <time.h>
  34.  
  35.        time_t   current_time;
  36.  
  37.        current_time = time((time_t *)0);
  38.  
  39.     Having done this, just set a macro for the length (in seconds) of one
  40. day:
  41.  
  42.        #define ONE_DAY    ((60) * (60) * (24))
  43.  
  44.     From here, you can get the value for any day you want. For example, the 
  45. value for two days' into the future would be:
  46.  
  47.       two_days_ahead = current_time + (ONE_DAY * 2);
  48.  
  49.     You can then convert the time_t for the desired day back into some 
  50. useful structure. If you want to manipulate it, maybe do this:
  51.  
  52.       struct tm *timestruct;
  53.  
  54.       timestruct = localtime(&two_days_ahead);
  55.  
  56.     The function localtime() and the struct tm are both defined in <time.h>.
  57. Struct tm includes separate values for year, month, etc, in case you want 
  58. specific bits.
  59.  
  60.     If you just want to print it out, try this:
  61.  
  62.     struct tm *timestruct;
  63.     char timestamp[30];
  64.  
  65.     timestruct = localtime(&two_days_ahead);
  66.  
  67.     strftime(timestamp, 30, "%a %b %d %H:%M:%S %Z %Y", timestruct);
  68.  
  69.     This will give you the string timestamp, which will have this format:
  70.  
  71. Tue Jul 28 07:19:53 GMT 1992
  72.  
  73.   Hope this is helpful. Cheers, Colin.
  74.  
  75.  
  76.  
  77. -- 
  78. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  79. Colin Lynch - Telecom Australia | E-mail:  colinl@cssc-syd.tansu.oz.au
  80. Advanced Network Products       |   post:  PO Box A226
  81. Customised Software             |          Sydney South NSW  2000
  82.