SIGforum.com    Main Page  Hop To Forum Categories  The Lounge    A nifty algorithm for date => day of week
Page 1 2 
Go
New
Find
Notify
Tools
Reply
  
A nifty algorithm for date => day of week Login/Join 
Baroque Bloke
Picture of Pipe Smoker
posted
The programming language that I use (SNOBOL4) has a built-in function that returns the current date and time. But it has no facility to report the day of week (DoW hereafter). Some of my programs need to know the DoW. Until recently I had to type it in myself.

I began looking on the web for an algorithm to produce DoW from the current date. I soon found one that was simple to implement. It was said to be from The Old Farmer’s Almanac. Although it worked for dates near the current date, I discovered many other dates that produced a wrong DoW value. I had little faith in the algorithm.

Searching further, I found a Wikipedia article that described several such algorithms:
https://en.m.wikipedia.org/wik..._the_day_of_the_week

Of those listed I found Wang's algorithm (under the “Mathematical algorithms” tab) to be most appealing. Elegant, and not much more complex than the Farmer’s Almanac algorithm. Unfortunately it produced bad DoW values. I attributed the failure to ambiguities in the Wiki implementation description.

I found a link with a PDF of the Wang paper:
https://arxiv.org/abs/1404.2510

No ambiguities there, but I found the notation tough going. But between the Wiki description and the Wang description I managed to produce a date => DoW function for my programming language. It’s pretty well tested by now. Very handy for me, it is. I’m glad to have it. And I’m grateful to Mr. Wang for making his nifty algorithm freely available.

Considering the irregularity of the Gregorian calendar I’m amazed that the relatively simple Wang algorithm works for all dates.

BTW – Lewis Carol, author of “Alice's Adventures in Wonderland” (1865) and its sequel, “Through the Looking-Glass” (1871), also devised an algorithm for this purpose.



Serious about crackers
 
Posts: 9698 | Location: San Diego | Registered: July 26, 2014Reply With QuoteReport This Post
Void Where Prohibited
Picture of WaterburyBob
posted Hide Post
Another approach, if you have the ability to make database calls, would be to do a simple SQL query to return the day of the week for a specified date.



"If Gun Control worked, Chicago would look like Mayberry, not Thunderdome" - Cam Edwards
 
Posts: 16725 | Location: Under the Boot of Tyranny in Connectistan | Registered: February 02, 2005Reply With QuoteReport This Post
Member
posted Hide Post
How far back do you have to go? 1582 with its 10 missing days might be an issue... Smile


===
I would like to apologize to anyone I have *not* offended. Please be patient. I will get to you shortly.
 
Posts: 2143 | Location: The Sticks in Wisconsin. | Registered: September 30, 2012Reply With QuoteReport This Post
W07VH5
Picture of mark123
posted Hide Post
quote:
Originally posted by whanson_wi:
How far back do you have to go? 1582 with its 10 missing days might be an issue... Smile
What happened in 1582? Did earth travel through a time distortion?

I’ve been watching too much Star Trek.
 
Posts: 45676 | Location: Pennsyltucky | Registered: December 05, 2001Reply With QuoteReport This Post
Baroque Bloke
Picture of Pipe Smoker
posted Hide Post
^^^^^^^
The Wang algorithm works for days in the Gregorian calendar which was introduced in 1582 by Pope Gregory XIII.

It corrected calendar errors that had accumulated in the Julian calendar.

A modification enables the Wang algorithm to work for the Julian calendar.



Serious about crackers
 
Posts: 9698 | Location: San Diego | Registered: July 26, 2014Reply With QuoteReport This Post
Optimistic Cynic
Picture of architect
posted Hide Post
quote:
Originally posted by mark123:
What happened in 1582? Did earth travel through a time distortion?
1582 was when the Julian Calendar was succeeded by the Gregorian Calendar, the missing days were a "journal entry" meant to align the dates in each after 1600 years of accumulated discrepancies. For the curious, the biggest difference between the two calendars is that the every four years leap day was eliminated in years that are evenly divisible by 1000 (and not those evenly divisible by 400).

All because a year cannot be evenly divided by the length of a day. There are still countries, and religions that use the Julian calendar, and calendar reform has been a topic of great controversy over the Centuries (akin to the debate herein of the usefulness of DST).

If you think there is complexity here, just look up the debate about calculating the correct date of Easter!

quote:
I’ve been watching too much Star Trek.
Is that even possible?
 
Posts: 6935 | Location: NoVA | Registered: July 22, 2009Reply With QuoteReport This Post
Baroque Bloke
Picture of Pipe Smoker
posted Hide Post
quote:
Originally posted by architect:
<snip>
For the curious, the biggest difference between the two calendars is that the every four years leap day was eliminated in years that are evenly divisible by 1000 (and not those evenly divisible by 400).
<snip>

Not quite.

“If the year is divisible by four, it's a leap year. But if the year can be divided by 100 as well as four, it's not a leap year. However, if the year is divisible by 400, it is a leap year.”
https://www.bbc.com/future/art...-to-fix-the-calendar



Serious about crackers
 
Posts: 9698 | Location: San Diego | Registered: July 26, 2014Reply With QuoteReport This Post
אַרְיֵה
Picture of V-Tail
posted Hide Post
September 2, 1752, was on a Wednesday. Thursday was September 14. September 3 through September 13 weren't there that year.

The Julian and Gregorian calendars are solar calendars, in which everything is keyed to our journey around the sun.

If you really want to make your brain hurt, try the Hebrew calendar. It's a lunisolar calendar -- lunar for months, and solar for years. Each month is 29 or 30 days because the moon makes a trip around the earth approximately every 29 1/2 days. Starting date of each month is keyed to the new moon. This scheme does not lend itself to a twelve month year, so the leap year cycle is 19 years, and seven years in that cycle (years 3, 6, 8, 11, 14, 17, and 19) are leap years. For leap years, we don't add no steenkin' day, we add a whole month! (We don't screw around, if you're going to have a leap year, let's really do it.)

Does your brain hurt yet? Wait, there's more. In some systems, an hour is defined as 1/12 of the elapsed time between sunrise and sunset, so in this system, not all hours are equal. Winter hours are shorter than summer hours during the day, inverse at night, noon still comes at the sun's peak. Fortunately, this system is not commonly used.

I'm an old-time C user, don't know SNOBOL, haven't tried to code any day / date routines in C.



הרחפת שלי מלאה בצלופחים
 
Posts: 31703 | Location: Central Florida, Orlando area | Registered: January 03, 2010Reply With QuoteReport This Post
Member
Picture of wrightd
posted Hide Post
Long time ago I found the mother lode of calendar algorithms, at the Naval Observatory website. It was the actual algorithms themselves, not any language library plugins. I wish I had saved them, because when I found them I used one of them to resolve an important calendar issue in some custom application software that was not otherwise available at the time for that particular language. I'd hazard to guess they are still there, or by today's standard prob lurking all over the internet, since I doubt they invented it, these algorithms are likely very old.

Personally I find calendar and time machinations to be one of the most difficult class of algorithms to code, across the board. Hard to write, hard to test, just a plain old pita.




Lover of the US Constitution
Wile E. Coyote School of DIY Disaster
 
Posts: 9091 | Location: Nowhere the constitution is not honored | Registered: February 01, 2008Reply With QuoteReport This Post
Baroque Bloke
Picture of Pipe Smoker
posted Hide Post
quote:
Originally posted by wrightd:
Long time ago I found the mother lode of calendar algorithms, at the Naval Observatory website. It was the actual algorithms themselves, not any language library plugins.
<snip>

My OP is in regard to an actual algorithm, not a language library plugin. I created a SNOBOL4 function that implements the clever algorithm devised by Mr. Wang.

My function takes a string argument specifying a date in this format: “yyyy/mm/dd”. It returns the corresponding day of week name as a three-character string. E.g.: “Sun”, “Mon”, etc. for any date in the Gregorian calendar.



Serious about crackers
 
Posts: 9698 | Location: San Diego | Registered: July 26, 2014Reply With QuoteReport This Post
Member
Picture of wrightd
posted Hide Post
Yes I know, lots of built-in calendar libraries are not all that rich, which is why I find calendar arithmetic a pita when you need something fast and they're not paying you to mess with those algorithms. The history of calendar machinations is nutty for programmers since the rules, patterns, and exceptions change so much in history over time. Such and so king was born on the day when the phoenix rose from the third caldron, and so he replaced a perfectly working calendar for part of the world just for the hell of it. Go figure.




Lover of the US Constitution
Wile E. Coyote School of DIY Disaster
 
Posts: 9091 | Location: Nowhere the constitution is not honored | Registered: February 01, 2008Reply With QuoteReport This Post
אַרְיֵה
Picture of V-Tail
posted Hide Post
quote:
Originally posted by Pipe Smoker:

My function takes a string argument specifying a date in this format: “yyyy/mm/dd”. It returns the corresponding day of week name as a three-character string. E.g.: “Sun”, “Mon”, etc. for any date in the Gregorian calendar.
Let me know what your function returns with this input string: "1752/09/09"



הרחפת שלי מלאה בצלופחים
 
Posts: 31703 | Location: Central Florida, Orlando area | Registered: January 03, 2010Reply With QuoteReport This Post
Baroque Bloke
Picture of Pipe Smoker
posted Hide Post
^^^^^^*
bash> sbl dow.sbl
dow(‘1752/09/09’)
>Sat<
bash>

This web site also says that 1752/09/09 is a Saturday in the Gregorian calendar:
https://www.calculator.net/day...9%2F1752&x=Calculate

Is there something special about 1752/09/09?

ETA: I found it:
“About September 9, 1752
Skipped Day: This day does not exist in Great Britain and its colonies including America and Ireland. Under the Calendar (New Style) Act 1750, Britain skipped 11 days to adopt the Gregorian calendar.”

So 1752/09/09 doesn’t exist in the British calendar, but it does exist in the Gregorian calendar.



Serious about crackers
 
Posts: 9698 | Location: San Diego | Registered: July 26, 2014Reply With QuoteReport This Post
אַרְיֵה
Picture of V-Tail
posted Hide Post
quote:
Originally posted by Pipe Smoker:
^^^^^^*
bash> sbl dow.sbl
dow(‘1752/09/09’)
>Sat<
bash>

This web site also says that 1752/09/09 is a Saturday in the Gregorian calendar:
https://www.calculator.net/day...9%2F1752&x=Calculate

Is there something special about 1752/09/09?

ETA: I found it:
“About September 9, 1752
Skipped Day: This day does not exist in Great Britain and its colonies including America and Ireland. Under the Calendar (New Style) Act 1750, Britain skipped 11 days to adopt the Gregorian calendar.”

So 1752/09/09 doesn’t exist in the British calendar, but it exists in the Gregorian calendar.
Try "cal 9 1752" -- most POSIX compliant systems will return this:

September 1752
Su Mo Tu We Th Fr Sa
1 2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30



הרחפת שלי מלאה בצלופחים
 
Posts: 31703 | Location: Central Florida, Orlando area | Registered: January 03, 2010Reply With QuoteReport This Post
Baroque Bloke
Picture of Pipe Smoker
posted Hide Post
^^^^^^
You’re correct – That’s exactly what:
bash> cal 9 1752
produced. Except ’1’ was under ‘Tu’, not ‘Su’. (SIGforum takes out leading whitespace unless you use the code, /code facility.)

The Wang algorithm may not be POSIX compliant, but it’s Gregorian compliant. Smile



Serious about crackers
 
Posts: 9698 | Location: San Diego | Registered: July 26, 2014Reply With QuoteReport This Post
For real?
Picture of Chowser
posted Hide Post
It's been 30+ years, but if anyone remembers DCL language, I wrote a little program that had a mathematical equation which derived the day of the week. All you did was enter the date and the program figured out what day it was. It even factored in leap years.

Then in C++ class, we had to write a program which you enter the month and year and what day the month started and the program was supposed to print a calender. I just modified my DCL program to make it work in C++. It stumped the instructor and he was about to fail me and said you were supposed to write a program with arrays in it. I said the problem said make a calendar. That's what I did. lol


I wish I could find the program. I highly doubt the college still has the vax machines installed for me to stop in and ask.



Not minority enough!
 
Posts: 8244 | Location: Cleveland, OH | Registered: August 09, 2007Reply With QuoteReport This Post
Member
Picture of wrightd
posted Hide Post
The IRS might still be using one.




Lover of the US Constitution
Wile E. Coyote School of DIY Disaster
 
Posts: 9091 | Location: Nowhere the constitution is not honored | Registered: February 01, 2008Reply With QuoteReport This Post
Optimistic Cynic
Picture of architect
posted Hide Post
quote:
Originally posted by wrightd:
The IRS might still be using one.
Only if it affects a missed due date penalty and interest.
 
Posts: 6935 | Location: NoVA | Registered: July 22, 2009Reply With QuoteReport This Post
Member
Picture of wrightd
posted Hide Post
Ha !




Lover of the US Constitution
Wile E. Coyote School of DIY Disaster
 
Posts: 9091 | Location: Nowhere the constitution is not honored | Registered: February 01, 2008Reply With QuoteReport This Post
Master of one hand
pistol shooting
Picture of Hamden106
posted Hide Post
I'm retired and I don't know what DoW it is either. Nerds you be Smile



SIGnature
NRA Benefactor CMP Pistol Distinguished
 
Posts: 6453 | Location: Oregon | Registered: September 01, 2001Reply With QuoteReport This Post
  Powered by Social Strata Page 1 2  
 

SIGforum.com    Main Page  Hop To Forum Categories  The Lounge    A nifty algorithm for date => day of week

© SIGforum 2024