Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #383208 > unrolled thread
| Started by | porkchop@invalid.foo (Mike Sanders) |
|---|---|
| First post | 2024-03-01 03:48 +0000 |
| Last post | 2024-03-05 13:30 +0000 |
| Articles | 20 on this page of 91 — 18 participants |
Back to article view | Back to comp.lang.c
getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-01 03:48 +0000
Re: getFirstDayOfMonth() gazelle@shell.xmission.com (Kenny McCormack) - 2024-03-01 15:39 +0000
Re: getFirstDayOfMonth() Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-03-01 17:12 +0100
Re: getFirstDayOfMonth() Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-03-01 08:28 -0800
Re: getFirstDayOfMonth() Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-03-01 17:33 +0100
Re: getFirstDayOfMonth() Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-03-01 17:37 +0100
Re: getFirstDayOfMonth() Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-03-01 10:34 -0800
Re: getFirstDayOfMonth() Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-03-01 22:03 +0100
Re: getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-01 17:20 +0000
Re: getFirstDayOfMonth() scott@slp53.sl.home (Scott Lurndal) - 2024-03-01 16:47 +0000
Re: getFirstDayOfMonth() Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-03-01 17:56 +0100
Re: getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-01 17:18 +0000
Re: getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-01 20:47 +0000
Re: getFirstDayOfMonth() scott@slp53.sl.home (Scott Lurndal) - 2024-03-01 21:38 +0000
Re: getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-01 21:50 +0000
Re: getFirstDayOfMonth() Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-03-03 09:52 -0800
Re: getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-02 00:00 +0000
Re: getFirstDayOfMonth() scott@slp53.sl.home (Scott Lurndal) - 2024-03-02 14:46 +0000
Re: getFirstDayOfMonth() Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-03-02 16:41 +0100
Re: getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-02 19:59 +0000
Re: getFirstDayOfMonth() Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-03-02 22:14 +0100
Re: getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-04 18:08 +0000
Re: getFirstDayOfMonth() Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-03-05 03:03 +0100
Re: getFirstDayOfMonth() Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-03-02 14:22 -0800
Re: getFirstDayOfMonth() Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-03-03 15:43 +0100
Re: getFirstDayOfMonth() David Brown <david.brown@hesbynett.no> - 2024-03-03 18:45 +0100
Re: getFirstDayOfMonth() Kaz Kylheku <433-929-6894@kylheku.com> - 2024-03-03 18:42 +0000
Re: getFirstDayOfMonth() David Brown <david.brown@hesbynett.no> - 2024-03-04 09:49 +0100
Re: getFirstDayOfMonth() Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2024-03-04 11:31 +0000
Re: getFirstDayOfMonth() Richard Harnden <richard.nospam@gmail.invalid> - 2024-03-04 14:23 +0000
Re: getFirstDayOfMonth() Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-03-04 17:27 +0100
Re: getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-04 17:34 +0000
Re: getFirstDayOfMonth() scott@slp53.sl.home (Scott Lurndal) - 2024-03-04 17:40 +0000
Re: getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-04 18:11 +0000
Re: getFirstDayOfMonth() scott@slp53.sl.home (Scott Lurndal) - 2024-03-04 18:26 +0000
Re: getFirstDayOfMonth() Kaz Kylheku <433-929-6894@kylheku.com> - 2024-03-04 18:30 +0000
Re: getFirstDayOfMonth() scott@slp53.sl.home (Scott Lurndal) - 2024-03-04 19:01 +0000
Re: getFirstDayOfMonth() Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-03-05 03:09 +0100
Re: getFirstDayOfMonth() scott@slp53.sl.home (Scott Lurndal) - 2024-03-05 15:06 +0000
Re: getFirstDayOfMonth() Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-03-05 02:56 +0100
Re: getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-05 02:49 +0000
Re: getFirstDayOfMonth() Mark Bourne <nntp.mbourne@spamgourmet.com> - 2024-03-06 20:40 +0000
Re: getFirstDayOfMonth() scott@slp53.sl.home (Scott Lurndal) - 2024-03-06 20:54 +0000
Re: getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-04 17:26 +0000
Re: getFirstDayOfMonth() scott@slp53.sl.home (Scott Lurndal) - 2024-03-04 17:34 +0000
Re: getFirstDayOfMonth() Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-03-04 10:07 -0800
Re: getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-04 23:47 +0000
Re: getFirstDayOfMonth() Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-03-04 16:10 -0800
Re: getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-05 00:12 +0000
[OT] Re: getFirstDayOfMonth() Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-03-05 03:18 +0100
Re: [OT] Re: getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-05 02:55 +0000
Re: getFirstDayOfMonth() scott@slp53.sl.home (Scott Lurndal) - 2024-03-05 15:03 +0000
Re: getFirstDayOfMonth() Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-03-05 13:25 -0800
Re: getFirstDayOfMonth() Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2024-03-04 21:21 +0000
Re: getFirstDayOfMonth() Michael S <already5chosen@yahoo.com> - 2024-03-05 00:37 +0200
Re: getFirstDayOfMonth() Michael S <already5chosen@yahoo.com> - 2024-03-05 11:41 +0200
Re: getFirstDayOfMonth() Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-03-14 15:57 -0700
Re: getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-04 23:35 +0000
Re: getFirstDayOfMonth() Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-03-05 03:20 +0100
Re: getFirstDayOfMonth() James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-03-03 04:06 -0500
Re: getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-04 18:21 +0000
Re: getFirstDayOfMonth() James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-03-04 14:42 -0500
Re: getFirstDayOfMonth() Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-03-04 14:50 -0800
Re: getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-05 00:40 +0000
Re: getFirstDayOfMonth() Kaz Kylheku <433-929-6894@kylheku.com> - 2024-03-05 01:27 +0000
Re: getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-05 02:40 +0000
Re: getFirstDayOfMonth() scott@slp53.sl.home (Scott Lurndal) - 2024-03-05 15:05 +0000
Re: getFirstDayOfMonth() Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-03-04 17:39 -0800
Re: getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-05 02:43 +0000
Re: getFirstDayOfMonth() Michael S <already5chosen@yahoo.com> - 2024-03-05 01:08 +0200
Re: getFirstDayOfMonth() porkchop@invalid.foo (Mike Sanders) - 2024-03-05 00:18 +0000
Re: getFirstDayOfMonth() Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-03-04 16:33 -0800
Re: getFirstDayOfMonth() scott@slp53.sl.home (Scott Lurndal) - 2024-03-03 15:24 +0000
Re: getFirstDayOfMonth() Michael S <already5chosen@yahoo.com> - 2024-03-03 20:20 +0200
Re: getFirstDayOfMonth() scott@slp53.sl.home (Scott Lurndal) - 2024-03-03 18:56 +0000
Re: getFirstDayOfMonth() Mark Bourne <nntp.mbourne@spamgourmet.com> - 2024-03-03 13:17 +0000
Re: getFirstDayOfMonth() jak <nospam@please.ty> - 2024-03-02 02:49 +0100
Re: getFirstDayOfMonth() Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-03-03 09:47 -0800
Re: getFirstDayOfMonth() jak <nospam@please.ty> - 2024-03-04 02:26 +0100
Re: getFirstDayOfMonth() Peter 'Shaggy' Haywood <phaywood@alphalink.com.au> - 2024-03-05 14:03 +1100
Re: getFirstDayOfMonth() bart <bc@freeuk.com> - 2024-03-05 13:19 +0000
Re: getFirstDayOfMonth() jak <nospam@please.ty> - 2024-03-13 05:40 +0100
Re: getFirstDayOfMonth() jak <nospam@please.ty> - 2024-03-13 06:01 +0100
Re: getFirstDayOfMonth() Spiros Bousbouras <spibou@gmail.com> - 2024-03-13 13:38 +0000
Re: getFirstDayOfMonth() Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-03-13 08:47 -0700
Re: getFirstDayOfMonth() Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-03-13 09:11 -0700
Re: getFirstDayOfMonth() Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-03-13 07:36 -0700
Re: getFirstDayOfMonth() Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-03-14 19:56 -0700
Re: getFirstDayOfMonth() Kaz Kylheku <433-929-6894@kylheku.com> - 2024-03-02 03:44 +0000
Re: getFirstDayOfMonth() Peter 'Shaggy' Haywood <phaywood@alphalink.com.au> - 2024-03-05 14:07 +1100
Re: getFirstDayOfMonth() gazelle@shell.xmission.com (Kenny McCormack) - 2024-03-05 13:30 +0000
Page 1 of 5 [1] 2 3 4 5 Next page →
| From | porkchop@invalid.foo (Mike Sanders) |
|---|---|
| Date | 2024-03-01 03:48 +0000 |
| Subject | getFirstDayOfMonth() |
| Message-ID | <urrj5l$124o9$1@dont-email.me> |
Just sharing what I've learned, hope some of you can adapt it for your own use. Calculates the name of the weekday (Sun, Mon, etc) for the 1st day of a given month & year... https://busybox.neocities.org/notes/get-first-day-of-month.txt -- :wq Mike Sanders
[toc] | [next] | [standalone]
| From | gazelle@shell.xmission.com (Kenny McCormack) |
|---|---|
| Date | 2024-03-01 15:39 +0000 |
| Message-ID | <urssrf$1vntr$1@news.xmission.com> |
| In reply to | #383208 |
In article <urrj5l$124o9$1@dont-email.me>,
Mike Sanders <porkchop@invalid.foo> wrote:
>Just sharing what I've learned, hope some of you can adapt
>it for your own use.
>
>Calculates the name of the weekday (Sun, Mon, etc) for the
>1st day of a given month & year...
>
>https://busybox.neocities.org/notes/get-first-day-of-month.txt
Here's the guts of my version of the Zeller algorithm:
int day(d,m,y)
int d, m, y;
{
if (m < 3) {
m += 12;
y--;
}
return (d + (13*m-27)/5 + y + y/4 - y/100 + y/400) % 7;
}
I assume the two versions end up being equivalent.
BTW, how long do you think it will be until this thread gets hijacked into
a long, acrimonious debate about what the definition of the first day of
the month is and how various cultures define it differently, and how
insensitive we are (especially, if "we" are USA Americans) to assume that
our way is the only way? A matter of hours, I suspect.
--
You know politics has really been turned upside down when you have someone in the
government with a last name of Cheney (Liz, Senator from Wyoming) who is the voice of
reason.
[toc] | [prev] | [next] | [standalone]
| From | Janis Papanagnou <janis_papanagnou+ng@hotmail.com> |
|---|---|
| Date | 2024-03-01 17:12 +0100 |
| Message-ID | <ursupl$1al8k$1@dont-email.me> |
| In reply to | #383214 |
On 01.03.2024 16:39, Kenny McCormack wrote:
> In article <urrj5l$124o9$1@dont-email.me>,
> Mike Sanders <porkchop@invalid.foo> wrote:
>> Just sharing what I've learned, hope some of you can adapt
>> it for your own use.
>>
>> Calculates the name of the weekday (Sun, Mon, etc) for the
>> 1st day of a given month & year...
>>
>> https://busybox.neocities.org/notes/get-first-day-of-month.txt
>
> Here's the guts of my version of the Zeller algorithm:
>
> int day(d,m,y)
> int d, m, y;
> {
> if (m < 3) {
> m += 12;
> y--;
> }
> return (d + (13*m-27)/5 + y + y/4 - y/100 + y/400) % 7;
> }
>
> I assume the two versions end up being equivalent.
Are they?
I'm certainly irritated about the OP's final
return (f % 7) - 1;
returning values from -1 to 5. And then using these indexes
to address a C array. (Looks wrong to me, but I'm anyway
not interested in second hand copies of generally available
algorithms; there's little value.)
Janis
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Date | 2024-03-01 08:28 -0800 |
| Message-ID | <877cilsypt.fsf@nosuchdomain.example.com> |
| In reply to | #383215 |
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
[...]
>> In article <urrj5l$124o9$1@dont-email.me>,
>> Mike Sanders <porkchop@invalid.foo> wrote:
>>> Just sharing what I've learned, hope some of you can adapt
>>> it for your own use.
>>>
>>> Calculates the name of the weekday (Sun, Mon, etc) for the
>>> 1st day of a given month & year...
>>>
>>> https://busybox.neocities.org/notes/get-first-day-of-month.txt
[...]
> I'm certainly irritated about the OP's final
>
> return (f % 7) - 1;
>
> returning values from -1 to 5. And then using these indexes
> to address a C array. (Looks wrong to me, but I'm anyway
> not interested in second hand copies of generally available
> algorithms; there's little value.)
Yes, that's a bug in the original code. The program only tests for
August 2024, but the algorithm fails for June 2024, which starts on
Saturday.
One fix is to change
return (f % 7) - 1;
to:
return (f + 6) % 7;
which works correctly at least for all months of 2024.
Negative index values can be valid. For example, foo[-1] can be valid,
but only if foo is a pointer to an array element other than the first
one, not if foo is an array object.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */
[toc] | [prev] | [next] | [standalone]
| From | Janis Papanagnou <janis_papanagnou+ng@hotmail.com> |
|---|---|
| Date | 2024-03-01 17:33 +0100 |
| Message-ID | <urt009$1aso9$1@dont-email.me> |
| In reply to | #383216 |
On 01.03.2024 17:28, Keith Thompson wrote: > Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes: > [...] >>> In article <urrj5l$124o9$1@dont-email.me>, >>> Mike Sanders <porkchop@invalid.foo> wrote: >>>> Just sharing what I've learned, hope some of you can adapt >>>> it for your own use. >>>> >>>> Calculates the name of the weekday (Sun, Mon, etc) for the >>>> 1st day of a given month & year... >>>> >>>> https://busybox.neocities.org/notes/get-first-day-of-month.txt > [...] >> I'm certainly irritated about the OP's final >> >> return (f % 7) - 1; >> >> returning values from -1 to 5. And then using these indexes >> to address a C array. (Looks wrong to me, but I'm anyway >> not interested in second hand copies of generally available >> algorithms; there's little value.) > > Yes, that's a bug in the original code. The program only tests for > August 2024, but the algorithm fails for June 2024, which starts on > Saturday. > > One fix is to change > return (f % 7) - 1; > to: > return (f + 6) % 7; > which works correctly at least for all months of 2024. Yes. Though I'd have done the correction on the previous line. int f = d + ... - 1; return f % 7; To me that would be clearer. Janis
[toc] | [prev] | [next] | [standalone]
| From | Janis Papanagnou <janis_papanagnou+ng@hotmail.com> |
|---|---|
| Date | 2024-03-01 17:37 +0100 |
| Message-ID | <urt07v$1avj4$1@dont-email.me> |
| In reply to | #383217 |
On 01.03.2024 17:33, Janis Papanagnou wrote: > > Yes. Though I'd have done the correction on the previous line. > > int f = d + ... - 1; > return f % 7; > > To me that would be clearer. Or completely removing the 'd' and the '-1' since 'd' is actually a (non-const declared) constant and equals to 1. (Gee! Why do I mind, anyway.) > > Janis >
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Date | 2024-03-01 10:34 -0800 |
| Message-ID | <8734t9sswj.fsf@nosuchdomain.example.com> |
| In reply to | #383218 |
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
> On 01.03.2024 17:33, Janis Papanagnou wrote:
>> Yes. Though I'd have done the correction on the previous line.
>>
>> int f = d + ... - 1;
>> return f % 7;
>>
>> To me that would be clearer.
>
> Or completely removing the 'd' and the '-1' since 'd' is actually
> a (non-const declared) constant and equals to 1.
>
> (Gee! Why do I mind, anyway.)
I think the point of defining 'd' is to allow for computing the day of
the week for days of the month other than the first. The code as
presented suggests a lot of opportunities for generalization (such as
handling months other than August 2024). The local variable 'd' could
be replaced by a parameter 'int mday'.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */
[toc] | [prev] | [next] | [standalone]
| From | Janis Papanagnou <janis_papanagnou+ng@hotmail.com> |
|---|---|
| Date | 2024-03-01 22:03 +0100 |
| Message-ID | <urtfrt$1eg6j$1@dont-email.me> |
| In reply to | #383226 |
On 01.03.2024 19:34, Keith Thompson wrote: > Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes: >> On 01.03.2024 17:33, Janis Papanagnou wrote: >>> Yes. Though I'd have done the correction on the previous line. >>> >>> int f = d + ... - 1; >>> return f % 7; >>> >>> To me that would be clearer. >> >> Or completely removing the 'd' and the '-1' since 'd' is actually >> a (non-const declared) constant and equals to 1. >> >> (Gee! Why do I mind, anyway.) > > I think the point of defining 'd' is to allow for computing the day of > the week for days of the month other than the first. The code as > presented suggests a lot of opportunities for generalization (such as > handling months other than August 2024). The local variable 'd' could > be replaced by a parameter 'int mday'. Surely I am aware of that. But the OP's code isn't addressing any generalization (cf. "int getFirstDayOfMonth(int year, int month)"). If you'd strive for a generalization you'd define a function like the one Kenny posted (cf. "int day(d,m,y)"). If you want a special function (for whatever reason) as the OP did you can as well just optimize it by reducing redundant parts. (Anyway, it's not worth the dispute.) Janis
[toc] | [prev] | [next] | [standalone]
| From | porkchop@invalid.foo (Mike Sanders) |
|---|---|
| Date | 2024-03-01 17:20 +0000 |
| Message-ID | <urt2po$1beu1$2@dont-email.me> |
| In reply to | #383216 |
Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: > Yes, that's a bug in the original code. The program only tests for > August 2024, but the algorithm fails for June 2024, which starts on > Saturday. > > One fix is to change > return (f % 7) - 1; > to: > return (f + 6) % 7; > which works correctly at least for all months of 2024. > > Negative index values can be valid. For example, foo[-1] can be valid, > but only if foo is a pointer to an array element other than the first > one, not if foo is an array object. Thanks Keith, will use this moving forward. Glad to have some extra eyeballs on it. -- :wq Mike Sanders
[toc] | [prev] | [next] | [standalone]
| From | scott@slp53.sl.home (Scott Lurndal) |
|---|---|
| Date | 2024-03-01 16:47 +0000 |
| Message-ID | <KEnEN.70968$LONb.54416@fx08.iad> |
| In reply to | #383214 |
gazelle@shell.xmission.com (Kenny McCormack) writes:
>In article <urrj5l$124o9$1@dont-email.me>,
>Mike Sanders <porkchop@invalid.foo> wrote:
>>Just sharing what I've learned, hope some of you can adapt
>>it for your own use.
>>
>>Calculates the name of the weekday (Sun, Mon, etc) for the
>>1st day of a given month & year...
>>
>>https://busybox.neocities.org/notes/get-first-day-of-month.txt
>
>Here's the guts of my version of the Zeller algorithm:
>
>int day(d,m,y)
>int d, m, y;
>{
> if (m < 3) {
> m += 12;
> y--;
> }
> return (d + (13*m-27)/5 + y + y/4 - y/100 + y/400) % 7;
>}
>
>I assume the two versions end up being equivalent.
>
>BTW, how long do you think it will be until this thread gets hijacked into
>a long, acrimonious debate about what the definition of the first day of
>the month is and how various cultures define it differently, and how
>insensitive we are (especially, if "we" are USA Americans) to assume that
>our way is the only way? A matter of hours, I suspect.
Seconds, it would appear, as you have highjacked your own thread.
[toc] | [prev] | [next] | [standalone]
| From | Janis Papanagnou <janis_papanagnou+ng@hotmail.com> |
|---|---|
| Date | 2024-03-01 17:56 +0100 |
| Message-ID | <urt1b8$1b7rl$1@dont-email.me> |
| In reply to | #383214 |
On 01.03.2024 16:39, Kenny McCormack wrote:
>
> Here's the guts of my version of the Zeller algorithm:
>
> int day(d,m,y)
> int d, m, y;
> {
> if (m < 3) {
> m += 12;
> y--;
> }
> return (d + (13*m-27)/5 + y + y/4 - y/100 + y/400) % 7;
> }
I'm wondering what would be the better approach, using some date
functions or calculating it by a formula. (Serious question.)
I'm asking since I had previously used time-functions in another
context (determining Fri 13th dates) for such a purpose. And the
rewrite to the "first day" task is simple, and the code pattern
very flexible. Here's the Gawk counterpart just for illustration:
BEGIN { y = ARGV[1] ; m = ARGV[2]
print strftime ("%a", mktime (y " " m " 1 0 0 0"))
}
...and I'm sure you can also use this approach in C.
So what's more advantageous? - Is the domain of one or the other
approach restricted? Is one or the other more accurate? Or may
the runtime be an issue?
Janis
[toc] | [prev] | [next] | [standalone]
| From | porkchop@invalid.foo (Mike Sanders) |
|---|---|
| Date | 2024-03-01 17:18 +0000 |
| Message-ID | <urt2ku$1beu1$1@dont-email.me> |
| In reply to | #383214 |
Kenny McCormack <gazelle@shell.xmission.com> wrote:
> Here's the guts of my version of the Zeller algorithm:
>
> int day(d,m,y)
> int d, m, y;
> {
> if (m < 3) {
> m += 12;
> y--;
> }
> return (d + (13*m-27)/5 + y + y/4 - y/100 + y/400) % 7;
> }
>
> I assume the two versions end up being equivalent.
Hey-hey Kenny!
Many thanks for sharing this, now I have something to compare it too
more thoroughly.
> BTW, how long do you think it will be until this thread gets hijacked into
> a long, acrimonious debate about what the definition of the first day of
> the month is and how various cultures define it differently, and how
> insensitive we are (especially, if "we" are USA Americans) to assume that
> our way is the only way? A matter of hours, I suspect.
Well...
--
:wq
Mike Sanders
[toc] | [prev] | [next] | [standalone]
| From | porkchop@invalid.foo (Mike Sanders) |
|---|---|
| Date | 2024-03-01 20:47 +0000 |
| Message-ID | <urteto$1e55r$1@dont-email.me> |
| In reply to | #383214 |
Kenny McCormack <gazelle@shell.xmission.com> wrote:
> Here's the guts of my version of the Zeller algorithm:
>
> int day(d,m,y)
> int d, m, y;
> {
> if (m < 3) {
> m += 12;
> y--;
> }
> return (d + (13*m-27)/5 + y + y/4 - y/100 + y/400) % 7;
> }
I've put this right to work too Kenny...
Prints calander for the given year with every 2 months adjacent
https://busybox.neocities.org/notes/adjacent-calendars.txt
Example...
Jan Feb
Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat
1 1 2 3 4 5
2 3 4 5 6 7 8 6 7 8 9 10 11 12
9 10 11 12 13 14 15 13 14 15 16 17 18 19
16 17 18 19 20 21 22 20 21 22 23 24 25 26
23 24 25 26 27 28 29 27 28
30 31
--
:wq
Mike Sanders
[toc] | [prev] | [next] | [standalone]
| From | scott@slp53.sl.home (Scott Lurndal) |
|---|---|
| Date | 2024-03-01 21:38 +0000 |
| Message-ID | <7VrEN.517245$xHn7.96511@fx14.iad> |
| In reply to | #383228 |
porkchop@invalid.foo (Mike Sanders) writes:
>Kenny McCormack <gazelle@shell.xmission.com> wrote:
>
>> Here's the guts of my version of the Zeller algorithm:
>>
>> int day(d,m,y)
>> int d, m, y;
>> {
>> if (m < 3) {
>> m += 12;
>> y--;
>> }
>> return (d + (13*m-27)/5 + y + y/4 - y/100 + y/400) % 7;
>> }
>
>I've put this right to work too Kenny...
>
>Prints calander for the given year with every 2 months adjacent
>
>https://busybox.neocities.org/notes/adjacent-calendars.txt
>
>Example...
>
> Jan Feb
> Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat
> 1 1 2 3 4 5
> 2 3 4 5 6 7 8 6 7 8 9 10 11 12
> 9 10 11 12 13 14 15 13 14 15 16 17 18 19
> 16 17 18 19 20 21 22 20 21 22 23 24 25 26
> 23 24 25 26 27 28 29 27 28
> 30 31
That must not have been for 2024, or you have a bug.
A calendar utility comes with most linux systems. It also supports
ISO 8601 which starts the week on Monday instead of Sunday.
https://en.wikipedia.org/wiki/ISO_8601
https://github.com/util-linux/util-linux/blob/master/misc-utils/cal.c
$ cal 2024
2024
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 1 2 3 1 2
7 8 9 10 11 12 13 4 5 6 7 8 9 10 3 4 5 6 7 8 9
14 15 16 17 18 19 20 11 12 13 14 15 16 17 10 11 12 13 14 15 16
21 22 23 24 25 26 27 18 19 20 21 22 23 24 17 18 19 20 21 22 23
28 29 30 31 25 26 27 28 29 24 25 26 27 28 29 30
31
[toc] | [prev] | [next] | [standalone]
| From | porkchop@invalid.foo (Mike Sanders) |
|---|---|
| Date | 2024-03-01 21:50 +0000 |
| Message-ID | <urtij7$1etmt$1@dont-email.me> |
| In reply to | #383232 |
Scott Lurndal <scott@slp53.sl.home> wrote: > That must not have been for 2024, or you have a bug. Well Scott, in the code of the link posted its displaying 2022: int year = 2022; > A calendar utility comes with most linux systems. It also supports > ISO 8601 which starts the week on Monday instead of Sunday. Yes sir, I know that. But I want to build my own instead so I can learn more about it. Even if that means reiventing the wheel, at least I'll learn it as I wish. Hoping not to sound contrite, cant we all just help each other learn? Darn man. -- :wq Mike Sanders
[toc] | [prev] | [next] | [standalone]
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Date | 2024-03-03 09:52 -0800 |
| Message-ID | <86cysbtd75.fsf@linuxsc.com> |
| In reply to | #383233 |
porkchop@invalid.foo (Mike Sanders) writes: > Scott Lurndal <scott@slp53.sl.home> wrote: > >> That must not have been for 2024, or you have a bug. > > Well Scott, in the code of the link posted its displaying 2022: > > int year = 2022; > >> A calendar utility comes with most linux systems. It also supports >> ISO 8601 which starts the week on Monday instead of Sunday. > > Yes sir, I know that. But I want to build my own instead so I can > learn more about it. Even if that means reiventing the wheel, at > least I'll learn it as I wish. Hoping not to sound contrite, > cant we all just help each other learn? Darn man. If you want to reinvent wheels, I suggest looking for some rather more interesting wheels.
[toc] | [prev] | [next] | [standalone]
| From | porkchop@invalid.foo (Mike Sanders) |
|---|---|
| Date | 2024-03-02 00:00 +0000 |
| Message-ID | <urtq7q$1ggh0$1@dont-email.me> |
| In reply to | #383232 |
Scott Lurndal <scott@slp53.sl.home> wrote:
> https://en.wikipedia.org/wiki/ISO_8601
Have partial ISO8601 compliance now (when using option: -D=2):
Usage: DATES [-OPTIONS] FILE1 [FILE2 ...]
-A=NUM Number of days in advance to look ahead (366 max)
-P=NUM Number of days in the past to look back (366 max)
-D=NUM Format dates as: 1 mm/dd/yyyy, 2 yyyy/mm/dd
-S=NUM Separate date fields using: 1 Slash, 2 Dash, 3 Dot
-X=NUM Export results as: 1 TXT, 2 CSV, 3 SQL, 4 HTML
-T=TAG Filter results for 'TAG' (16 characters max)
-H Include holidays (see manual for list)
-W Include weekday abbreviations
-F Include attribute flags (see manual for details)
-C Display calendar for current year
-M Display embedded user manual
Error levels returned:
3 Option syntax errors
2 File open-read errors
1 Dates match today or matching tags
0 No dates match today or no tags match
But hey its Friday I should lighten up & have some fun:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
const char *getChineseZodiac(int year) {
const char* animals[] = {
"Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake",
"Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig"
};
return animals[year % 12];
}
int main() {
time_t now = time(NULL);
struct tm *localTime = localtime(&now);
int year = localTime->tm_year;
printf("%d Year of the %s\n", year + 1900, getChineseZodiac(year));
return 0;
}
--
:wq
Mike Sanders
[toc] | [prev] | [next] | [standalone]
| From | scott@slp53.sl.home (Scott Lurndal) |
|---|---|
| Date | 2024-03-02 14:46 +0000 |
| Message-ID | <pZGEN.500996$7sbb.11881@fx16.iad> |
| In reply to | #383235 |
porkchop@invalid.foo (Mike Sanders) writes: >Scott Lurndal <scott@slp53.sl.home> wrote: > >> https://en.wikipedia.org/wiki/ISO_8601 > >Have partial ISO8601 compliance now (when using option: -D=2): Nice. > >Usage: DATES [-OPTIONS] FILE1 [FILE2 ...] > >-A=NUM Number of days in advance to look ahead (366 max) >-P=NUM Number of days in the past to look back (366 max) >-D=NUM Format dates as: 1 mm/dd/yyyy, 2 yyyy/mm/dd >-S=NUM Separate date fields using: 1 Slash, 2 Dash, 3 Dot >-X=NUM Export results as: 1 TXT, 2 CSV, 3 SQL, 4 HTML >-T=TAG Filter results for 'TAG' (16 characters max) >-H Include holidays (see manual for list) >-W Include weekday abbreviations >-F Include attribute flags (see manual for details) >-C Display calendar for current year >-M Display embedded user manual > If I may suggest: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html '-h' or '-?' have traditionally been used to display usage information for an utility. >Error levels returned: > >3 Option syntax errors >2 File open-read errors >1 Dates match today or matching tags >0 No dates match today or no tags match For commands defined by POSIX (and generally in unix), an exit status of zero indicates success.
[toc] | [prev] | [next] | [standalone]
| From | Janis Papanagnou <janis_papanagnou+ng@hotmail.com> |
|---|---|
| Date | 2024-03-02 16:41 +0100 |
| Message-ID | <urvhbr$1umk2$1@dont-email.me> |
| In reply to | #383242 |
On 02.03.2024 15:46, Scott Lurndal wrote: > porkchop@invalid.foo (Mike Sanders) writes: >> >> Usage: DATES [-OPTIONS] FILE1 [FILE2 ...] >>[...] > > If I may suggest: > > https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html > > '-h' or '-?' have traditionally been used to display > usage information for an utility. Or, since there's a mandatory file name argument, provide usage information (also) if no argument or option is provided. Janis > [...]
[toc] | [prev] | [next] | [standalone]
| From | porkchop@invalid.foo (Mike Sanders) |
|---|---|
| Date | 2024-03-02 19:59 +0000 |
| Message-ID | <us00f7$21v55$1@dont-email.me> |
| In reply to | #383242 |
Scott Lurndal <scott@slp53.sl.home> wrote: > If I may suggest: > > https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html > > '-h' or '-?' have traditionally been used to display > usage information for an utility. Yep, it will definitely be '-?' (still working to finalize things). Many thanks for the link too btw. >>Error levels returned: >> >>3 Option syntax errors >>2 File open-read errors >>1 Dates match today or matching tags >>0 No dates match today or no tags match > > For commands defined by POSIX (and generally in > unix), an exit status of zero indicates success. Oh yeah. Have been wrestling with the potential consequences of this for a good while. I think I'm going leave it as is because, (only speaking for myself here) the POSIX 0 good, 1 bad is terrible. This is perhaps the only area I've ever seen where other OSs, like Windows, have a superior methodology on the issue. 0/1 provides next to no nuance. But I'm no expert, just my experience. Thanks again for the links Scott, I'll read them this weekend. -- :wq Mike Sanders
[toc] | [prev] | [next] | [standalone]
Page 1 of 5 [1] 2 3 4 5 Next page →
Back to top | Article view | comp.lang.c
csiph-web