🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.programmer
Article #74

Re: Need (today's date + [1-7] days)

#74
From: Michael J Marshall
Date: Tue, 21 Jul 1992 08:47
39 lines
1730 bytes
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
Technical Details
Message-ID: <1236@dorothy.xon.co.uk>
Path: arc1-ca.pugleaf.net!sparky!uunet!pipex!slxsys!xon!michael
References: <1992Jul20.185321.12811@ncmicro.lonestar.org>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!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