Go | New | Find | Notify | Tools | Reply |
Baroque Bloke |
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 | ||
|
Void Where Prohibited |
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 | |||
|
Member |
How far back do you have to go? 1582 with its 10 missing days might be an issue... === I would like to apologize to anyone I have *not* offended. Please be patient. I will get to you shortly. | |||
|
W07VH5 |
What happened in 1582? Did earth travel through a time distortion? I’ve been watching too much Star Trek. | |||
|
Baroque Bloke |
^^^^^^^ 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 | |||
|
Optimistic Cynic |
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! Is that even possible? | |||
|
Baroque Bloke |
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 | |||
|
אַרְיֵה |
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. הרחפת שלי מלאה בצלופחים | |||
|
Member |
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 | |||
|
Baroque Bloke |
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 | |||
|
Member |
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 | |||
|
אַרְיֵה |
Let me know what your function returns with this input string: "1752/09/09" הרחפת שלי מלאה בצלופחים | |||
|
Baroque Bloke |
^^^^^^* 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 | |||
|
אַרְיֵה |
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 הרחפת שלי מלאה בצלופחים | |||
|
Baroque Bloke |
^^^^^^ 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. Serious about crackers | |||
|
For real? |
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! | |||
|
Member |
The IRS might still be using one. Lover of the US Constitution Wile E. Coyote School of DIY Disaster | |||
|
Optimistic Cynic |
Only if it affects a missed due date penalty and interest. | |||
|
Member |
Ha ! Lover of the US Constitution Wile E. Coyote School of DIY Disaster | |||
|
Master of one hand pistol shooting |
I'm retired and I don't know what DoW it is either. Nerds you be SIGnature NRA Benefactor CMP Pistol Distinguished | |||
|
Powered by Social Strata | Page 1 2 |
Please Wait. Your request is being processed... |