Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #56029 > unrolled thread

Find day of week from a given date

Started byMark Wills <markwills1970@gmail.com>
First post2017-03-21 11:51 -0700
Last post2017-03-29 08:23 +0000
Articles 6 on this page of 46 — 11 participants

Back to article view | Back to comp.lang.forth


Contents

  Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-21 11:51 -0700
    Re: Find day of week from a given date "Wolfgang Allinger" <all2001@spambog.com> - 2017-03-21 18:17 -0300
      Re: Find day of week from a given date Hans Bezemer <the.beez.speaks@gmail.com> - 2017-03-22 17:47 +0100
        Re: Find day of week from a given date "Wolfgang Allinger" <all2001@spambog.com> - 2017-03-22 20:52 -0300
        Re: Find day of week from a given date Ron Aaron <rambamist@gmail.com> - 2017-03-23 06:57 +0200
    Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-22 19:16 -0700
      Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-22 19:25 -0700
      Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-22 21:28 -0700
        Re: Find day of week from a given date Alex <alex@rivadpm.com> - 2017-03-23 14:12 +0000
          Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-23 16:25 -0700
          Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-23 22:02 -0700
            Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-23 23:07 -0700
            Re: Find day of week from a given date Alex <alex@rivadpm.com> - 2017-03-24 08:46 +0000
      Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-23 05:35 -0700
        Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-23 15:23 -0700
          Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-24 09:38 -0700
            Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-24 10:24 -0700
              Re: Find day of week from a given date Cecil Bayona <cbayona@cbayona.com> - 2017-03-24 13:32 -0500
                Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-24 12:19 -0700
                  Re: Find day of week from a given date Cecil Bayona <cbayona@cbayona.com> - 2017-03-24 16:01 -0500
              Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-24 12:00 -0700
                Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-24 12:19 -0700
                  Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-24 23:35 -0700
                    Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-25 01:59 -0700
                      Re: Find day of week from a given date Julian Fondren <julian.fondren@gmail.com> - 2017-03-25 02:50 -0700
                      Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-25 04:37 -0700
                        Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-25 12:32 -0700
                          Re: Find day of week from a given date Cecil Bayona <cbayona@cbayona.com> - 2017-03-25 14:56 -0500
                            Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-26 02:27 -0700
                              Re: Find day of week from a given date Cecil Bayona <cbayona@cbayona.com> - 2017-03-26 14:07 -0500
                Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-24 13:17 -0700
                  Re: Find day of week from a given date Howerd <howerdo@yahoo.co.uk> - 2017-03-24 14:43 -0700
                    Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-24 15:36 -0700
                    Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-24 23:47 -0700
                  Re: Find day of week from a given date Julian Fondren <julian.fondren@gmail.com> - 2017-03-25 03:00 -0700
                  Re: Find day of week from a given date "Wolfgang Allinger" <all2001@spambog.com> - 2017-03-25 09:06 -0300
              Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-24 12:28 -0700
                Re: Find day of week from a given date Julian Fondren <julian.fondren@gmail.com> - 2017-03-24 12:49 -0700
                  Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-24 13:15 -0700
              Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-26 14:49 -0700
                Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-26 17:08 -0700
    Re: Find day of week from a given date humptydumpty <ouatubi@gmail.com> - 2017-03-25 04:23 -0700
      Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-26 15:00 -0700
        Re: Find day of week from a given date humptydumpty <ouatubi@gmail.com> - 2017-03-27 00:50 -0700
          Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-27 09:14 -0700
    Re: Find day of week from a given date "Robert L." <No_spamming@noWhere_7073.org> - 2017-03-29 08:23 +0000

Page 3 of 3 — ← Prev page 1 2 [3]


#56360

Fromhughaguilar96@gmail.com
Date2017-03-26 17:08 -0700
Message-ID<e6210ac5-ce0a-4b48-94d5-bd37fc883668@googlegroups.com>
In reply to#56355
On Sunday, March 26, 2017 at 2:49:26 PM UTC-7, hughag...@gmail.com wrote:
> I still like my own algorithm better than Zeller's Algorithm. Mine checks the validity of the date given to it and aborts with a helpful error message if it is invalid. DOW just returns an invalid result.

Well, I wrote some more code. This comes from: \ https://alcor.concordia.ca//~gpkatch/gdate-algorithm.html

This is actually a good demonstration of how Forth's mixed-precision arithmetic improves efficiency. The C implementation made all the variables LONG so all of the calculations are double-precision. This was not necessary in Forth (also, not possible because we don't have a D/ function). Only on intermediate value could overflow and so I used UM* for that multiplication, then UM/MOD for the following division.

I moved all of this from TOY.4TH to NOVICE.4TH because it does have some practical value.

----------------------------------------------------------------------------

: date>n { month day year -- n }                    
    month 9 +  12 mod           to month
    year  month 10 /  -         to year
    year 365 *
    year 4 /                    +
    year 100 /                  -
    year 400 /                  +
    month 306 *  5 +  10 /      +
    day 1 -                     + ;
    
: n>dow ( n -- str )            \ N comes from DATE>N
    3 +  7 mod  days ;

: n>date { n | y mm dd ddd mi -- month day year }    
    n 10000 um*  14780. d+  3652425 um/mod nip              to y
    365 y *  y 4 / +  y 100 / -  y 400 / +  n swap -        to ddd
    ddd 0< if
        y 1 -                                               to y
        365 y *  y 4 / +  y 100 / -  y 400 / +  n swap -    to ddd      then
    ddd 100 *  52 +  3060 /                                 to mi
    mi 2 +  12 mod  1 +                                     to mm
    mi 2 +  12 /  y +                                       to y
    mi 306 *  5 +  10 /  ddd swap -  1 +                    to dd
    mm dd y ;

[toc] | [prev] | [next] | [standalone]


#56273

Fromhumptydumpty <ouatubi@gmail.com>
Date2017-03-25 04:23 -0700
Message-ID<f896523a-18ae-42b5-b15b-10510c27808d@googlegroups.com>
In reply to#56029
On Tuesday, March 21, 2017 at 8:51:10 PM UTC+2, Mark Wills wrote:
> A post on the Forth Programming group on Facebook inspired me
> to write this code today. Thought I'd share in the interests
> of posting some code.
> 
> Given a date in British format (day month year) it will tell you which
> day of the week it was. 0=Sunday, 1=Monday, 2=Tuesday and so on.
> 
> Works from 1/1/1700 to, well, anything I think.
> 
> So:
> 
> 3 12 1970 dow .
> 4 ok:0
> 
> Yay. I was born on a Thursday. Thursday is now my favourite day of the week!
> 
> variable year
> variable month
> variable day
> 
> create years 0 c, 3 c, 3 c, 6 c, 1 c, 4 c, 6 c, 2 c, 5 c, 0 c, 3 c, 5 c,
> create leap-years 6 c, 2 c, 3 c, 6 c, 1 c, 4 c, 6 c, 2 c, 5 c, 0 c, 3 c, 5 c,
> create centuries 4 c, 2 c, 0 c, 6 c, 
> 
> : century ( year - n )
>     1700 /mod drop 100 / 4 mod centuries + c@ ;
> 
> : leapYear? ( year - flag )
>     dup >r 400 mod 0=  r@ 100 mod 0<> or  r> 4 mod 0= and ;
> 
> : dow ( day month year -- n )
>     dup century >r  100 mod year !  1- month !  day !
>     r>  year @ dup 4 /  day @
>     year @ leapYear? if leap-years else years then month @ + c@
>     + + + +  7 mod ;
> 
> 
> 
> 
> Tests:
> 
> : ? ( day month year -- )
>     dow case
>         0 of ." Sunday" endof
>         1 of ." Monday" endof
>         2 of ." Tuesday" endof
>         3 of ." Wednesday" endof
>         4 of ." Thursday" endof
>         5 of ." Friday" endof
>         6 of ." Saturday" endof
>     endcase space ;
> 
> 31 1 1883 ?
> 3 12 1970 ?
> 25 12 2017 ?
> 1 1 2018 ?
> 
> 
> 
> Leap Year Tests:
> : testLeapYears ( -- )
>     \ A year will be a leap year if it is divisible by 4 but not by 100.
>     \ If a year is divisible by 4 and by 100, it is not a leap year 
>     \ unless it is also divisible by 400.
>     \ The years 1600, 2000 and 2400 are leap years, while 1700, 1800, 1900,
>     \ 2100, 2200 and 2300 are common years.
>     \ https://en.wikipedia.org/wiki/Leap_year
>     page 
>     2401 1600 do i leapYear? if i . then loop ;
> 
> 
> :-)


Looks like a 'dow' festival here :)  Gladly I join:

\ h = ( q + 13(m+1)/5 + k + k/4 + J/4 + 5J) mod 7
\ see:  https://en.wikipedia.org/wiki/Zeller's_congruence

: dow  ( d m y -- h ;based on TRUE=-1)
  over 3 < tuck
  + 100 /mod
  dup 5 *   >R
  4 /       >R
  dup 4 / + >R
  12 * - 1+ 13 * 5 /
  R> R> R> + + + +
  7 mod
;
: day-name  ( d m y -- $  ;english)
  dow
  S" Sat Sun Mon Tue Wed Thu Fri " drop
  swap 4 * + 4
;

Have a nice day,
hum9

[toc] | [prev] | [next] | [standalone]


#56356

Fromhughaguilar96@gmail.com
Date2017-03-26 15:00 -0700
Message-ID<9d377317-fdf7-4ac6-a3d1-5d0e2a31c18e@googlegroups.com>
In reply to#56273
On Saturday, March 25, 2017 at 4:23:39 AM UTC-7, humptydumpty wrote:
> Looks like a 'dow' festival here :)  Gladly I join:
> 
> \ h = ( q + 13(m+1)/5 + k + k/4 + J/4 + 5J) mod 7
> \ see:  https://en.wikipedia.org/wiki/Zeller's_congruence
> 
> : dow  ( d m y -- h ;based on TRUE=-1)
>   over 3 < tuck
>   + 100 /mod
>   dup 5 *   >R
>   4 /       >R
>   dup 4 / + >R
>   12 * - 1+ 13 * 5 /
>   R> R> R> + + + +
>   7 mod
> ;
> : day-name  ( d m y -- $  ;english)
>   dow
>   S" Sat Sun Mon Tue Wed Thu Fri " drop
>   swap 4 * + 4
> ;
> 
> Have a nice day,
> hum9

Your implementation of Zeller's algorithm works.

I said in my last post that typing in the formula given on the Wikipedia page doesn't work. I read further in the page though, and found that they do discuss the problem with MOD being given negative numbers --- apparently you read that far and/or you already knew how to fix the problem.

I never previously gave any thought to the subject of MOD being given a negative number --- so, something to keep in mind for the future.

[toc] | [prev] | [next] | [standalone]


#56375

Fromhumptydumpty <ouatubi@gmail.com>
Date2017-03-27 00:50 -0700
Message-ID<d6e2b538-be78-4c41-b902-88f794540efd@googlegroups.com>
In reply to#56356
On Monday, March 27, 2017 at 1:00:05 AM UTC+3, hughag...@gmail.com wrote:
> On Saturday, March 25, 2017 at 4:23:39 AM UTC-7, humptydumpty wrote:
> > Looks like a 'dow' festival here :)  Gladly I join:
> > 
> > \ h = ( q + 13(m+1)/5 + k + k/4 + J/4 + 5J) mod 7
> > \ see:  https://en.wikipedia.org/wiki/Zeller's_congruence
> > 
> > : dow  ( d m y -- h ;based on TRUE=-1)
> >   over 3 < tuck
> >   + 100 /mod
> >   dup 5 *   >R
> >   4 /       >R
> >   dup 4 / + >R
> >   12 * - 1+ 13 * 5 /
> >   R> R> R> + + + +
> >   7 mod
> > ;
> > : day-name  ( d m y -- $  ;english)
> >   dow
> >   S" Sat Sun Mon Tue Wed Thu Fri " drop
> >   swap 4 * + 4
> > ;
> > 
> > Have a nice day,
> > hum9
> 
> Your implementation of Zeller's algorithm works.
> 
> I said in my last post that typing in the formula given on the Wikipedia page doesn't work. I read further in the page though, and found that they do discuss the problem with MOD being given negative numbers --- apparently you read that far and/or you already knew how to fix the problem.
> 
> I never previously gave any thought to the subject of MOD being given a negative number --- so, something to keep in mind for the future.

Also important is (wikipedia quote):

"NOTE: In this algorithm January and February are counted as months 13 and 14 of the previous year. E.g. if it is 2 February 2010, the algorithm counts the date as the second day of the fourteenth month of 2009 (02/14/2009 in DD/MM/YYYY format)"

Have a nice day,
hum9

[toc] | [prev] | [next] | [standalone]


#56391

Fromhughaguilar96@gmail.com
Date2017-03-27 09:14 -0700
Message-ID<2abfabf4-7903-4359-8dfe-0d0845001010@googlegroups.com>
In reply to#56375
On Monday, March 27, 2017 at 12:50:35 AM UTC-7, humptydumpty wrote:
> Also important is (wikipedia quote):
> 
> "NOTE: In this algorithm January and February are counted as months 13 and 14 of the previous year. E.g. if it is 2 February 2010, the algorithm counts the date as the second day of the fourteenth month of 2009 (02/14/2009 in DD/MM/YYYY format)"

My code does that with an IF THEN construct. Your code has a tricky use of a flag as -1. All in all, I think my version was more readable --- but, considering that I don't understand how the algorithm works, the concept of "readability" makes no sense (because readability is supposed to make code more understandable).

I got a general idea of how it works, but I haven't thought very deeply on the subject --- I wouldn't be able to write something like that without a guide to go on --- when I get more time I will study Zeller's Algorithm, but this isn't at the top of my list of priorities (putting together a new release of the novice-package is a higher priority).
 
> Have a nice day,
> hum9

You too!

[toc] | [prev] | [next] | [standalone]


#56441

From"Robert L." <No_spamming@noWhere_7073.org>
Date2017-03-29 08:23 +0000
Message-ID<obfqt9$pkv$1@gioia.aioe.org>
In reply to#56029
On 3/21/2017, Mark Wills wrote:

> 3 12 1970 dow .
> 4 ok:0
> 
> Yay. I was born on a Thursday. Thursday is now my favourite day of the week!
> 
> variable year
> variable month
> variable day
> 
> create years 0 c, 3 c, 3 c, 6 c, 1 c, 4 c, 6 c, 2 c, 5 c, 0 c, 3 c, 5 c,
> create leap-years 6 c, 2 c, 3 c, 6 c, 1 c, 4 c, 6 c, 2 c, 5 c, 0 c, 3 c, 5 c,
> create centuries 4 c, 2 c, 0 c, 6 c, 
> 
> : century ( year - n )
>     1700 /mod drop 100 / 4 mod centuries + c@ ;
> 
> : leapYear? ( year - flag )
>     dup >r 400 mod 0=  r@ 100 mod 0<> or  r> 4 mod 0= and ;
> 
> : dow ( day month year -- n )
>     dup century >r  100 mod year !  1- month !  day !
>     r>  year @ dup 4 /  day @
>     year @ leapYear? if leap-years else years then month @ + c@
>     + + + +  7 mod ;

Ruby:

require 'date'
    ==>true
Date.new(1970,12,3).wday
    ==>4

-- 
Jewish drug dealers, child porn pushers, and slave traders are free
from prosecution in Israel.  Israel does not consider these to be
crimes ... so long as the victims are non-Jews.
www.theoccidentalobserver.net/2009/08/the-culture-of-deceit/

[toc] | [prev] | [standalone]


Page 3 of 3 — ← Prev page 1 2 [3]

Back to top | Article view | comp.lang.forth


csiph-web