Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: What about my last date functions? there are some error? |
| Date | 2016-11-08 20:08 +0000 |
| Organization | Fix this later |
| Message-ID | <nvtbaf$9uf$1@dont-email.me> (permalink) |
| References | (1 earlier) <87d1i69r5w.fsf@bsb.me.uk> <nvsd9k$nd4$1@dont-email.me> <871sym9dpv.fsf@bsb.me.uk> <nvstum$jt5$1@dont-email.me> <a4078985-def6-4593-9f9d-3989b2a8ff18@googlegroups.com> |
On 08/11/16 19:16, jadill33@gmail.com wrote:
<snip>
> I'm curious, if you don't mind, what's your use-case on calculating a
> Julian date when the Gregorian calendar is primarily what we use these
> days?
Oh, I don't care about it being Julian. In fact, that's just a name as
far as I'm concerned. What I find useful is simply the fact that the
date is represented as a single integer, such that to get tomorrow's
date from today's, all I have to do is ++d, without worrying about
whether this is the last day of the month or the last day of the year.
Consider how we might prepare a list of dates, 30 days apart, starting
from next Tuesday, and continuing until 17th January 2021. We might do
it like this (I'm assuming C90 here, for maximum portability):
time_t tmtstart = {0};
time_t tmtend = {0};
struct tm start_date = {0};
struct tm end_date = {0};
start_date.tm_year = 116; /* tm_year is 1900-based! */
start_date.tm_mon = 10; /* November, not October! tm_mon is 0-11 */
start_date.tm_mday = 15; /* 15th, not 14th! tm_mday is 1-31 */
end_date.tm_year = 121;
end_date.tm_mon = 0;
end_date.tm_mday = 17;
tmtstart = mktime(&start_date);
tmtend = mktime(&end_date);
while(difftime(tmtend, tmtstart) > 0.0)
{
/* I could use strftime here, but it seems unnecessary for such a
simple printf */
printf("%02d/%02d/%04d\n",
tmtstart.tm_mday,
tmtstart.tm_mon,
tmtstart.tm_year + 1900);
start_date.tm_mday += 30;
tmtstart = mktime(&start_date);
}
And that's fine. But it's a lot of code for such a simple task (and, to
my eyes at least, it's ugly code). Now consider this:
long start = tojul(2016, 11, 15);
long end = tojul(2021, 1, 17);
int d = 0;
int m = 0;
int y = 0;
while(start < end)
{
fromjul(&y, &m, &d, start);
printf("%02d/%02d/%04d\n", d, m, y);
start += 30;
}
(I note in passing that the Julian version uses two function calls per
loop iteration rather than three.)
Okay, some people might prefer the former code, but I'm not one of them.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
What about my last date functions? there are some error? Rosario19 <Ros@invalid.invalid> - 2016-11-08 11:28 +0100
Re: What about my last date functions? there are some error? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-08 10:55 +0000
Re: What about my last date functions? there are some error? "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2016-11-08 19:00 +0800
Re: What about my last date functions? there are some error? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-08 11:04 +0000
Re: What about my last date functions? there are some error? "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2016-11-08 19:22 +0800
Re: What about my last date functions? there are some error? Richard Heathfield <rjh@cpax.org.uk> - 2016-11-08 11:35 +0000
Re: What about my last date functions? there are some error? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-08 15:46 +0000
Re: What about my last date functions? there are some error? Richard Heathfield <rjh@cpax.org.uk> - 2016-11-08 16:20 +0000
Re: What about my last date functions? there are some error? Rosario19 <Ros@invalid.invalid> - 2016-11-08 17:44 +0100
Re: What about my last date functions? there are some error? Richard Heathfield <rjh@cpax.org.uk> - 2016-11-08 16:51 +0000
Re: What about my last date functions? there are some error? Jerry Stuckle <jstucklex@attglobal.net> - 2016-11-08 13:21 -0500
Re: What about my last date functions? there are some error? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-08 18:06 +0000
Re: What about my last date functions? there are some error? Richard Heathfield <rjh@cpax.org.uk> - 2016-11-08 18:56 +0000
Re: What about my last date functions? there are some error? Keith Thompson <kst-u@mib.org> - 2016-11-08 12:05 -0800
Re: What about my last date functions? there are some error? David Brown <david.brown@hesbynett.no> - 2016-11-08 21:10 +0100
Re: What about my last date functions? there are some error? Richard Heathfield <rjh@cpax.org.uk> - 2016-11-08 20:16 +0000
Re: What about my last date functions? there are some error? jadill33@gmail.com - 2016-11-08 11:16 -0800
Re: What about my last date functions? there are some error? Richard Heathfield <rjh@cpax.org.uk> - 2016-11-08 20:08 +0000
Re: What about my last date functions? there are some error? Keith Thompson <kst-u@mib.org> - 2016-11-08 12:11 -0800
Re: What about my last date functions? there are some error? Keith Thompson <kst-u@mib.org> - 2016-11-08 12:29 -0800
Re: What about my last date functions? there are some error? Rosario19 <Ros@invalid.invalid> - 2016-11-08 17:41 +0100
Re: What about my last date functions? there are some error? asetofsymbols@gmail.com - 2016-11-08 11:26 -0800
Re: What about my last date functions? there are some error? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-08 19:38 +0000
Re: What about my last date functions? there are some error? Keith Thompson <kst-u@mib.org> - 2016-11-08 08:19 -0800
csiph-web