Author Topic: Name of the day of the week  (Read 4407 times)

0 Members and 1 Guest are viewing this topic.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Name of the day of the week
« on: December 28, 2019, 06:35:48 am »
Name of the day of the week solve this function:

Code: QB64: [Select]
  1. 'function parameters: YEAR, MONTH, DAY
  2.  
  3. Today$ = GETDAYNAME(2019, 12, 28)
  4. PRINT "Today is "; Today$
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. FUNCTION ISLEAPYEAR (year)
  12.     IF year MOD 4 = 0 AND year MOD 100 THEN ISLEAPYEAR = 1
  13.     IF year MOD 100 = 0 AND year MOD 400 = 0 THEN ISLEAPYEAR = 1
  14.  
  15. FUNCTION GETDAYNAME$ (year, month, day)
  16.     IF year < 1900 THEN GETDAYNAME$ = "Invalid Year ( <1900 ).": EXIT FUNCTION
  17.     'spocitat pocet mesicu od ledna 1900:
  18.     Days = day
  19.     FOR yyr = 1900 TO year
  20.         IF yyr = year THEN monthend = month ELSE monthend = 12
  21.         FOR mont = 1 TO monthend
  22.             IF ISLEAPYEAR(yyr) = 0 THEN
  23.                 SELECT CASE mont
  24.                     CASE 1: m = 31
  25.                     CASE 2: m = 28
  26.                     CASE 3: m = 31
  27.                     CASE 4: m = 30
  28.                     CASE 5: m = 31
  29.                     CASE 6: m = 30
  30.                     CASE 7: m = 31
  31.                     CASE 8: m = 31
  32.                     CASE 9: m = 30
  33.                     CASE 10: m = 31
  34.                     CASE 11: m = 30
  35.                     CASE 12: m = 31
  36.                 END SELECT
  37.             ELSE
  38.                 SELECT CASE mont
  39.                     CASE 1: m = 31
  40.                     CASE 2: m = 29
  41.                     CASE 3: m = 31
  42.                     CASE 4: m = 30
  43.                     CASE 5: m = 31
  44.                     CASE 6: m = 30
  45.                     CASE 7: m = 31
  46.                     CASE 8: m = 31
  47.                     CASE 9: m = 30
  48.                     CASE 10: m = 31
  49.                     CASE 11: m = 30
  50.                     CASE 12: m = 31
  51.                 END SELECT
  52.             END IF
  53.             Om = m
  54.             Days = Days + m
  55.         NEXT
  56.     NEXT
  57.     Days = Days - m - 1
  58.     '  PRINT "Dnu:"; Days
  59.     a = (Days MOD 7) '0 = pondeli
  60.     RESTORE nms
  61.     FOR r = 0 TO a
  62.         READ GETDAYNAME$
  63.     NEXT
  64.     nms:
  65.     DATA Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday
  66.  

You may need to find out which week of the year it is, or determine by which week is according to date. For this I wrote this feature:

Code: QB64: [Select]
  1. 'function parameters: YEAR, MONTH, DAY
  2.  
  3. PRINT "Today is"; GETWEEK(2019, 12, 28); "week in this year."
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. FUNCTION GETWEEK (year, month, day)
  12.     Days = day
  13.     FOR mont = 1 TO month
  14.         IF ISLEAPYEAR(year) = 0 THEN
  15.             SELECT CASE mont
  16.                 CASE 1: m = 31
  17.                 CASE 2: m = 28
  18.                 CASE 3: m = 31
  19.                 CASE 4: m = 30
  20.                 CASE 5: m = 31
  21.                 CASE 6: m = 30
  22.                 CASE 7: m = 31
  23.                 CASE 8: m = 31
  24.                 CASE 9: m = 30
  25.                 CASE 10: m = 31
  26.                 CASE 11: m = 30
  27.                 CASE 12: m = 31
  28.             END SELECT
  29.         ELSE
  30.             SELECT CASE mont
  31.                 CASE 1: m = 31
  32.                 CASE 2: m = 29
  33.                 CASE 3: m = 31
  34.                 CASE 4: m = 30
  35.                 CASE 5: m = 31
  36.                 CASE 6: m = 30
  37.                 CASE 7: m = 31
  38.                 CASE 8: m = 31
  39.                 CASE 9: m = 30
  40.                 CASE 10: m = 31
  41.                 CASE 11: m = 30
  42.                 CASE 12: m = 31
  43.             END SELECT
  44.         END IF
  45.         Om = m
  46.         Days = Days + m
  47.     NEXT
  48.     Days = Days - m
  49.     GETWEEK = _CEIL(Days / 7)
  50.  
  51. FUNCTION ISLEAPYEAR (year)
  52.     IF year MOD 4 = 0 AND year MOD 100 THEN ISLEAPYEAR = 1
  53.     IF year MOD 100 = 0 AND year MOD 400 = 0 THEN ISLEAPYEAR = 1
  54.  
« Last Edit: December 28, 2019, 06:37:17 am by Petr »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Name of the day of the week
« Reply #1 on: December 28, 2019, 06:40:01 am »
You can also use my little GetDay$ Function:

Code: QB64: [Select]
  1. FUNCTION GetDay$ (mm, dd, yyyy) 'use 4 digit year
  2.     'From Zeller's congruence: https://en.wikipedia.org/wiki/Zeller%27s_congruence
  3.     IF mm < 3 THEN mm = mm + 12: yyyy = yyyy - 1
  4.     century = yyyy MOD 100
  5.     zerocentury = yyyy \ 100
  6.     result = (dd + INT(13 * (mm + 1) / 5) + century + INT(century / 4) + INT(zerocentury / 4) + 5 * zerocentury) MOD 7
  7.     SELECT CASE result
  8.         CASE 0: GetDay$ = "Saturday"
  9.         CASE 1: GetDay$ = "Sunday"
  10.         CASE 2: GetDay$ = "Monday"
  11.         CASE 3: GetDay$ = "Tuesday"
  12.         CASE 4: GetDay$ = "Wednesday"
  13.         CASE 5: GetDay$ = "Thursday"
  14.         CASE 6: GetDay$ = "Friday"
  15.     END SELECT

An example, to compare, here:

Code: QB64: [Select]
  1. 'function parameters: YEAR, MONTH, DAY
  2.  
  3. Today$ = GETDAYNAME(2019, 12, 28)
  4. PRINT "Today is "; Today$
  5. PRINT "Today is also "; GetDay(12, 28, 2019)
  6.  
  7.  
  8.  
  9.  
  10.  
  11. FUNCTION ISLEAPYEAR (year)
  12.     IF year MOD 4 = 0 AND year MOD 100 THEN ISLEAPYEAR = 1
  13.     IF year MOD 100 = 0 AND year MOD 400 = 0 THEN ISLEAPYEAR = 1
  14.  
  15. FUNCTION GETDAYNAME$ (year, month, day) 'otestovano brutalne, fuguje skutecne spravne!
  16.     IF year < 1900 THEN GETDAYNAME$ = "Invalid Year ( <1900 ).": EXIT FUNCTION
  17.     'spocitat pocet mesicu od ledna 1900:
  18.     Days = day
  19.     FOR yyr = 1900 TO year
  20.         IF yyr = year THEN monthend = month ELSE monthend = 12
  21.         FOR mont = 1 TO monthend
  22.             IF ISLEAPYEAR(yyr) = 0 THEN
  23.                 SELECT CASE mont
  24.                     CASE 1: m = 31
  25.                     CASE 2: m = 28
  26.                     CASE 3: m = 31
  27.                     CASE 4: m = 30
  28.                     CASE 5: m = 31
  29.                     CASE 6: m = 30
  30.                     CASE 7: m = 31
  31.                     CASE 8: m = 31
  32.                     CASE 9: m = 30
  33.                     CASE 10: m = 31
  34.                     CASE 11: m = 30
  35.                     CASE 12: m = 31
  36.                 END SELECT
  37.             ELSE
  38.                 SELECT CASE mont
  39.                     CASE 1: m = 31
  40.                     CASE 2: m = 29
  41.                     CASE 3: m = 31
  42.                     CASE 4: m = 30
  43.                     CASE 5: m = 31
  44.                     CASE 6: m = 30
  45.                     CASE 7: m = 31
  46.                     CASE 8: m = 31
  47.                     CASE 9: m = 30
  48.                     CASE 10: m = 31
  49.                     CASE 11: m = 30
  50.                     CASE 12: m = 31
  51.                 END SELECT
  52.             END IF
  53.             Om = m
  54.             Days = Days + m
  55.         NEXT
  56.     NEXT
  57.     Days = Days - m - 1
  58.     '  PRINT "Dnu:"; Days
  59.     a = (Days MOD 7) '0 = pondeli
  60.     RESTORE nms
  61.     FOR r = 0 TO a
  62.         READ GETDAYNAME$
  63.     NEXT
  64.     nms:
  65.     DATA Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday
  66.  
  67.  
  68. FUNCTION GetDay$ (mm, dd, yyyy) 'use 4 digit year
  69.     'From Zeller's congruence: https://en.wikipedia.org/wiki/Zeller%27s_congruence
  70.     IF mm < 3 THEN mm = mm + 12: yyyy = yyyy - 1
  71.     century = yyyy MOD 100
  72.     zerocentury = yyyy \ 100
  73.     result = (dd + INT(13 * (mm + 1) / 5) + century + INT(century / 4) + INT(zerocentury / 4) + 5 * zerocentury) MOD 7
  74.     SELECT CASE result
  75.         CASE 0: GetDay$ = "Saturday"
  76.         CASE 1: GetDay$ = "Sunday"
  77.         CASE 2: GetDay$ = "Monday"
  78.         CASE 3: GetDay$ = "Tuesday"
  79.         CASE 4: GetDay$ = "Wednesday"
  80.         CASE 5: GetDay$ = "Thursday"
  81.         CASE 6: GetDay$ = "Friday"
  82.     END SELECT
  83.  
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Name of the day of the week
« Reply #2 on: December 28, 2019, 06:58:34 am »
Thank you Steve. I haven't known a Zeller yet. I know read more in my longer solution, than in the Zeller formula, that's... other level...

Better is my own algorithm. You know how i count cows in a meadow? Count the legs and then divide by four ...  :)

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Name of the day of the week
« Reply #3 on: December 28, 2019, 07:45:55 am »
Thank you Steve. I haven't known a Zeller yet. I know read more in my longer solution, than in the Zeller formula, that's... other level...

Better is my own algorithm. You know how i count cows in a meadow? Count the legs and then divide by four ...  :)

I just hope you never run across a field with 4 three-legged cows in it.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

FellippeHeitor

  • Guest
Re: Name of the day of the week
« Reply #4 on: December 28, 2019, 07:51:55 am »
Code: QB64: [Select]
  1. numberOfCows = _CEIL(numberOfLegs / 4)

😁
« Last Edit: December 28, 2019, 07:55:04 am by FellippeHeitor »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Name of the day of the week
« Reply #5 on: December 28, 2019, 10:36:25 am »
IMHO for leap years

Quote
DO WHILE Bull =< 0
numberOfCows = _CEIL(numberOfLegs / 4)
LOOP

^_^
Programming isn't difficult, only it's  consuming time and coffee

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Name of the day of the week
« Reply #6 on: December 28, 2019, 10:56:01 am »
:-)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Name of the day of the week
« Reply #7 on: December 28, 2019, 01:04:02 pm »
You can also use my little GetDay$ Function:

Code: QB64: [Select]
  1. FUNCTION GetDay$ (mm, dd, yyyy) 'use 4 digit year
  2.     'From Zeller's congruence: https://en.wikipedia.org/wiki/Zeller%27s_congruence
  3.     IF mm < 3 THEN mm = mm + 12: yyyy = yyyy - 1
  4.     century = yyyy MOD 100
  5.     zerocentury = yyyy \ 100
  6.     result = (dd + INT(13 * (mm + 1) / 5) + century + INT(century / 4) + INT(zerocentury / 4) + 5 * zerocentury) MOD 7
  7.     SELECT CASE result
  8.         CASE 0: GetDay$ = "Saturday"
  9.         CASE 1: GetDay$ = "Sunday"
  10.         CASE 2: GetDay$ = "Monday"
  11.         CASE 3: GetDay$ = "Tuesday"
  12.         CASE 4: GetDay$ = "Wednesday"
  13.         CASE 5: GetDay$ = "Thursday"
  14.         CASE 6: GetDay$ = "Friday"
  15.     END SELECT

An example, to compare, here:

Code: QB64: [Select]
  1. 'function parameters: YEAR, MONTH, DAY
  2.  
  3. Today$ = GETDAYNAME(2019, 12, 28)
  4. PRINT "Today is "; Today$
  5. PRINT "Today is also "; GetDay(12, 28, 2019)
  6.  
  7.  
  8.  
  9.  
  10.  
  11. FUNCTION ISLEAPYEAR (year)
  12.     IF year MOD 4 = 0 AND year MOD 100 THEN ISLEAPYEAR = 1
  13.     IF year MOD 100 = 0 AND year MOD 400 = 0 THEN ISLEAPYEAR = 1
  14.  
  15. FUNCTION GETDAYNAME$ (year, month, day) 'otestovano brutalne, fuguje skutecne spravne!
  16.     IF year < 1900 THEN GETDAYNAME$ = "Invalid Year ( <1900 ).": EXIT FUNCTION
  17.     'spocitat pocet mesicu od ledna 1900:
  18.     Days = day
  19.     FOR yyr = 1900 TO year
  20.         IF yyr = year THEN monthend = month ELSE monthend = 12
  21.         FOR mont = 1 TO monthend
  22.             IF ISLEAPYEAR(yyr) = 0 THEN
  23.                 SELECT CASE mont
  24.                     CASE 1: m = 31
  25.                     CASE 2: m = 28
  26.                     CASE 3: m = 31
  27.                     CASE 4: m = 30
  28.                     CASE 5: m = 31
  29.                     CASE 6: m = 30
  30.                     CASE 7: m = 31
  31.                     CASE 8: m = 31
  32.                     CASE 9: m = 30
  33.                     CASE 10: m = 31
  34.                     CASE 11: m = 30
  35.                     CASE 12: m = 31
  36.                 END SELECT
  37.             ELSE
  38.                 SELECT CASE mont
  39.                     CASE 1: m = 31
  40.                     CASE 2: m = 29
  41.                     CASE 3: m = 31
  42.                     CASE 4: m = 30
  43.                     CASE 5: m = 31
  44.                     CASE 6: m = 30
  45.                     CASE 7: m = 31
  46.                     CASE 8: m = 31
  47.                     CASE 9: m = 30
  48.                     CASE 10: m = 31
  49.                     CASE 11: m = 30
  50.                     CASE 12: m = 31
  51.                 END SELECT
  52.             END IF
  53.             Om = m
  54.             Days = Days + m
  55.         NEXT
  56.     NEXT
  57.     Days = Days - m - 1
  58.     '  PRINT "Dnu:"; Days
  59.     a = (Days MOD 7) '0 = pondeli
  60.     RESTORE nms
  61.     FOR r = 0 TO a
  62.         READ GETDAYNAME$
  63.     NEXT
  64.     nms:
  65.     DATA Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday
  66.  
  67.  
  68. FUNCTION GetDay$ (mm, dd, yyyy) 'use 4 digit year
  69.     'From Zeller's congruence: https://en.wikipedia.org/wiki/Zeller%27s_congruence
  70.     IF mm < 3 THEN mm = mm + 12: yyyy = yyyy - 1
  71.     century = yyyy MOD 100
  72.     zerocentury = yyyy \ 100
  73.     result = (dd + INT(13 * (mm + 1) / 5) + century + INT(century / 4) + INT(zerocentury / 4) + 5 * zerocentury) MOD 7
  74.     SELECT CASE result
  75.         CASE 0: GetDay$ = "Saturday"
  76.         CASE 1: GetDay$ = "Sunday"
  77.         CASE 2: GetDay$ = "Monday"
  78.         CASE 3: GetDay$ = "Tuesday"
  79.         CASE 4: GetDay$ = "Wednesday"
  80.         CASE 5: GetDay$ = "Thursday"
  81.         CASE 6: GetDay$ = "Friday"
  82.     END SELECT
  83.  

Once again I remind folks to get copies of mm and yyyy because the getDay$ function changes them in first line.