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 
Member
Picture of wrightd
posted December 19, 2024 06:08 PMHide Post
Yea I thot it was Friday, now I gotta go to work tomorrow. Dammit.




Lover of the US Constitution
Wile E. Coyote School of DIY Disaster
 
Posts: 9306 | Location: Nowhere the constitution is not honored | Registered: February 01, 2008Reply With QuoteReport This Post
His diet consists of black
coffee, and sarcasm.
Picture of egregore
posted December 20, 2024 09:35 AMHide Post
If you work in a calendar factory, will they fire you for missing days?
 
Posts: 29766 | Location: Johnson City, TN | Registered: April 28, 2012Reply With QuoteReport This Post
Member
Picture of wrightd
posted December 21, 2024 10:04 PMHide Post
Not if thems calendars be made in china.




Lover of the US Constitution
Wile E. Coyote School of DIY Disaster
 
Posts: 9306 | Location: Nowhere the constitution is not honored | Registered: February 01, 2008Reply With QuoteReport This Post
His diet consists of black
coffee, and sarcasm.
Picture of egregore
posted December 27, 2024 06:32 PMHide Post
A long time ago on 60 Minutes, there was a savant guy who was his own algorithm. Give him any date at random and he could answer in less than 5 seconds and be right every time.
 
Posts: 29766 | Location: Johnson City, TN | Registered: April 28, 2012Reply With QuoteReport This Post
Baroque Bloke
Picture of Pipe Smoker
posted January 06, 2025 08:32 AMHide Post
I create at least one testbench program for every function in my function library. That includes the function that I wrote that implements the Wang day-of-week (DoW) algorithm.

That testbench verified that my DoW function produced the correct weekday name for 30+ diverse dates. Not a bad test, but I thought it ought to be better.

After some thought, I figured out a way to write a testbench to check the DoW result for EVERY date in a specified span of dates.

I wrote that testbench and set it to test the DoW result for every date from 1582/10/15, the first date in the Gregorian calendar, through 2199/12/31, far in the future.

I’m glad I did – that testbench quickly found a wrong result. But after fixing one bug in my DoW function the testbench ran perfectly.

It reported that there were 235,433 dates in that span. Run time to generate all of those dates, and to check the DoW result for each date, was slightly more than 5 seconds.

I now have complete confidence in the Wang algorithm and my implementation of it. Very satisfying. Functions are programs small enough to be thoroughly tested. They make larger programs that use them simpler and more reliable.



Serious about crackers.
 
Posts: 10051 | Location: San Diego | Registered: July 26, 2014Reply With QuoteReport This Post
Baroque Bloke
Picture of Pipe Smoker
posted March 11, 2025 08:30 AMHide Post
Here’s my function that implements Wang’s date to day of week algorithm. I should’ve posted this sooner. The language is SNOBOL4.

Lines with an ‘*’ in column 1 are just annotation. Lines with a ‘+’ in column 1 are continuations of the previous line. (A line wrap)

Given '1941/12/07’, it returns ’Sun’

I’ve prepended line numbers to each code line to facilitate discussion, if necessary.

 1 |* Library function 'dow' (day of week) takes arguement 'x',
 2 |* a date in the yyyy/mm/dd format.  It returns its weekday
 3 |* name abbreviated to three characters.  E.g., "Sun".
 4 |* It's Wamg's algorithm for the Gregorian calendar found in
 5 |* Wikipedia.
 6 |* https://en.m.wikipedia.org/wik..._the_day_of_the_week
 7 |
 8 |*   Rev 9
 9 |-INCLUDE 'floor.fun'
10 |-INCLUDE 'mod.fun'
11 |-INCLUDE 'leap_year.fun'
12 |    define('dow(x)y,c,y1,y0,m,d,a,b,d0') :(dowEND)
13 |dow a = array(12); b = array(7)
14 |    a[ 1] =  1; a[ 3] =  5; a[ 5] =  7
15 |    a[ 7] =  9; a[ 9] =  3; a[11] = 12
16 |    a[ 2] = 12; a[ 4] =  2; a[ 6] =  4
17 |    a[ 8] =  6; a[10] =  8; a[12] = 10
18 |    b[1] = 'Sun'; b[2] = 'Mon'
19 |    b[3] = 'Tue'; b[4] = 'Wed'; b[5] = 'Thr'
20 |    b[6] = 'Fri'; b[7] = 'Sat'
21 |    x ? break('/') . y '/' break('/') . m '/' rem . d
22 |    y ? len(2) . c len(1) . y1 len(1) . y0
23 |    d0 = a[m]
24 |    d = d - d0 + y0 - y1
25 |+       + floor(y0 / 4. - y1 / 2.)
26 |+       - 2 * mod(c, 4)
27 |    d = ~(~leap_year(y), ~le(m, 2)) d - 1
28 |    d = mod(d, 7) + 1
29 |    dow = b[d] :(RETURN)
30 |dowEND

This message has been edited. Last edited by: Pipe Smoker, March 11, 2025 09:30 PM



Serious about crackers.
 
Posts: 10051 | Location: San Diego | Registered: July 26, 2014Reply 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 2025