home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.unix.programmer:3951 comp.unix.questions:9476
- Newsgroups: comp.unix.programmer,comp.unix.questions
- 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
- From: colinl@cssc-syd.tansu.com.au (Colin Lynch)
- Subject: Re: Need (today's date + [1-7] days)
- Message-ID: <1992Jul28.072025.13886@cssc-syd.tansu.com.au>
- Keywords: date
- Organization: Customised Software Solutions Centres, AOTC
- References: <1992Jul16.145836.15586@avalon.nwc.navy.mil> <1992Jul19.063945.2322@metapro.DIALix.oz.au>
- Date: Tue, 28 Jul 1992 07:20:25 GMT
- Lines: 69
-
- bernie@metapro.DIALix.oz.au (Bernd Felsche) writes:
-
- >In <1992Jul16.145836.15586@avalon.nwc.navy.mil>
- > dejesus@archimedes.nwc.navy.mil (Francisco X DeJesus) writes:
-
- >>If I can get today's date in a shell script or C program, how can I find
- >>out the dates of the next week? For example, if I know today is
- >>"Wed Dec 30 1992", how can I have the program now that the next seven days
- >>are going to be "Thu Dec 31 1992", "Fri Jan 1 1993", etc (taking the
- >>case in which both month and year could change).
-
- >I usually get away with fiddling with the timezone.
- >Unfortunately, some operating systems ignore hour offsets
- >greater than 12 hours from UTC.
-
- ...The most reliable way to do this is to first convert the current
- date/time into a time_t type ("time_t" is defined in the system header file
- <time.h>, and is basically a long containing the number of seconds since the
- start of 01 Jan 1970 on most systems):
-
- #include <time.h>
-
- time_t current_time;
-
- current_time = time((time_t *)0);
-
- Having done this, just set a macro for the length (in seconds) of one
- day:
-
- #define ONE_DAY ((60) * (60) * (24))
-
- From here, you can get the value for any day you want. For example, the
- value for two days' into the future would be:
-
- two_days_ahead = current_time + (ONE_DAY * 2);
-
- You can then convert the time_t for the desired day back into some
- useful structure. If you want to manipulate it, maybe do this:
-
- struct tm *timestruct;
-
- timestruct = localtime(&two_days_ahead);
-
- The function localtime() and the struct tm are both defined in <time.h>.
- Struct tm includes separate values for year, month, etc, in case you want
- specific bits.
-
- If you just want to print it out, try this:
-
- struct tm *timestruct;
- char timestamp[30];
-
- timestruct = localtime(&two_days_ahead);
-
- strftime(timestamp, 30, "%a %b %d %H:%M:%S %Z %Y", timestruct);
-
- This will give you the string timestamp, which will have this format:
-
- Tue Jul 28 07:19:53 GMT 1992
-
- Hope this is helpful. Cheers, Colin.
-
-
-
- --
- <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- Colin Lynch - Telecom Australia | E-mail: colinl@cssc-syd.tansu.oz.au
- Advanced Network Products | post: PO Box A226
- Customised Software | Sydney South NSW 2000
-