Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Calculating date from # of occurance of Sunday in a month?
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Fri Jul 19, 2019 1:30 pm    Post subject: Reply with quote

six1 wrote:
i'm not sure, if i'm understanding you the right way...

I for once understood him, he wants to provide
Quote:
Month = 7
Year = 19
DoW = 1 (Tuesday)
n'th DoW = 4

With this data my code puts out:
Quote:
Requested 4. Tuesday is the 23.07.19

In case of invalid n'th DoW
Quote:
DoW = 3 (Thursday)
nt_dow = 5

my code spits out:
Quote:
Requested 5. Thursday is invalid


Your code does it exactly the other way around.


Last edited by MWS on Fri Jul 19, 2019 1:33 pm; edited 1 time in total
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5921
Location: Holland

blank.gif
PostPosted: Mon Jul 22, 2019 8:48 am    Post subject: Reply with quote

Please stick to the topic. You have the option to post or not. A post should be intentionally helpful and about the code/problem.

--- added the next day---
ok, the above i wrote which was clear so i thought. but to my amazement, i found a lot of posts after it. they did not contribute to the problem. I even read some really bizarre things.
i really have no time to read each posting. if you have concerns, send an email to support.
I know that some members write in a way that others dislike. Either because there is not enough info, or no code that was tried, or the way words are used.
I read and re-read some topics and indeed some are on the edge. Others are over it and i delete them.
I know for some it is boring, but please stick to the topic. It is a lot of work to remove and edit/moderate posts.
There are plenty of other fora where you can write about anything.
The main purpose of this forum is that users can help each other.
Some members have earned some credit over the years because of valuable contributions. i need to weight that all in. It is work that i do not like because of odd accusations i get.
To be clear, there is only one MCS baby and that is BASCOM Very Happy
Normally i would remove the whole topic but since it contains also good info, i let it be.
Ok, back to programming, or risk a ban.

_________________
Mark


Last edited by albertsm on Tue Jul 23, 2019 11:07 am; edited 1 time in total
Back to top
View user's profile Visit poster's website
six1

Bascom Expert



Joined: 27 Feb 2009
Posts: 553

germany.gif
PostPosted: Tue Jul 23, 2019 6:14 am    Post subject: Reply with quote

in consideration of Mark's wish to be on topic:

@KenHorse
i'm out of time, but i will make a demo Function for you, which will give you back the nth repetition of a Weekday by given month and year. It can take a while...
and of course it costs nothing. What is a forum for?

_________________
For technical reasons, the signature is on the back of this message.
Back to top
View user's profile
six1

Bascom Expert



Joined: 27 Feb 2009
Posts: 553

germany.gif
PostPosted: Tue Jul 23, 2019 10:40 am    Post subject: Reply with quote

Hi KenHorse,
since I have little time at the moment, it should give you an idea of ​​how it works.
Feel free to adapt to your needs.

Code:

$regfile = "m8def.dat"                                   ' specify the used micro
$hwstack = 32                                               ' default use 32 for the hardware stack
$swstack = 10                                               ' default use 10 for the SW stack
$framesize = 40                                             ' default use 40 for the frame space

$crystal = 4000000                                         ' used crystal frequency
$baud = 19200                                               ' use baud rate


Enable Interrupts

Config Clock = Soft
Config Date = YMD , Separator =.' ANSI-Format

dim dateArr(6) As Byte
dim gd as byte, b as byte
dim wday as byte, repetition as byte, month as byte, year as byte, Strweekday as string * 10

declare Function repetition_of_day( byval wday as byte, byval repetition as byte, byval month as byte, byval year as byte) as byte

$sim

'main


for b = 1 to 12
' Get Day from 3rd repetition from JAN to DEC 2019
' in case result is "0", it is no valid Result!
  wday = 6        ' 0-6  0=Monday
  repetition = 3  ' nth repetition of Day
  month = b
  year = 19
  gd = repetition_of_day( wday, repetition, month, year)
  if gd > 0 then
    Strweekday = Lookupstr(wday , Weekdays)
    print "Search: repetition #";str(repetition);" of ";Strweekday;" in ";str(month);"-20";str(year);" is ";str(gd);"-";str(month);"-20";str(year)
  else
    print "Search: repetition #";str(repetition);" of ";Strweekday;" in ";str(month);"-20";str(year);" is not in month Range"
  end if
next


end
'end of main
' Search: repetition #3 of Tuesday in 12-2019 is 17-12-2019
' Search: repetition #3 of Thursday in 11-2019 is 21-11-2019




Function repetition_of_day( byval wday as byte, byval repetition as byte, byval month as byte, byval year as byte) as byte
' wday = 0 to 6, Monday starts with 0
' repetition = nth repetition of Weekday "wday"
local fwom as byte, ldom as byte, hb as byte, foow as byte, Lsyssec As Long


  dateArr(1) = 0     'sec
  dateArr(2) = 0     'min
  dateArr(3) = 1     'hour
  dateArr(4) = 1     'day
  dateArr(5) = month 'month
  dateArr(6) = year  'year
  ' get first Weekday of month
  Lsyssec = Syssec(dateArr(1))
  fwom = DayOfWeek(lsyssec)

  ' getting last day of month
  ' 1) get first day from following month
  if month = 12 then
     dateArr(5)=1
     incr dateArr(6)
  else
     incr dateArr(5)
  end if
  Lsyssec = Syssec(dateArr(1))
  ' 2) substract one Day, so we've last day in month
  Lsyssec=Lsyssec-86400
  dateArr(4)=Date(Lsyssec)
  ' 3) get last Day of Month
  ldom=dateArr(4)

  ' calculate Day of first occurence of wday (foow) in month
  if fwom <= wday then
    foow = wday - fwom
    incr foow
  else
    foow = wday + 7
    foow = foow - fwom
    incr foow
  end if


  ' now "foow" holds the first Weekday we're searching for as Day in Month in given month and year

  ' we've to check if the nth "repetition" of our Weekday is in Range of month!
  decr repetition
  hb = repetition * 7
  hb=hb+foow
  if hb <= ldom then
    ' it's ok! we're inside the month Range
    repetition_of_day = hb
  else
    ' we're outside the day-Range of Month
    repetition_of_day = 0
  end if
end function



Weekdays:

Data "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" , "Sunday"
 

_________________
For technical reasons, the signature is on the back of this message.


Last edited by six1 on Wed Jul 24, 2019 5:28 am; edited 5 times in total
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Tue Jul 23, 2019 2:38 pm    Post subject: Reply with quote

six1 wrote:
Feel free to adapt to your needs.

Well, the first required adaption would be to make your code producing correct results. LOL
Back to top
View user's profile
KenHorse

Bascom Member



Joined: 16 Jul 2004
Posts: 523

blank.gif
PostPosted: Tue Jul 23, 2019 4:52 pm    Post subject: Reply with quote

six1 wrote:
Hi KenHorse,
since I have little time at the moment, it should give you an idea of ​​how it works.
Feel free to adapt to your needs.

Code:

$regfile = "m8def.dat"                                   ' specify the used micro
$hwstack = 32                                               ' default use 32 for the hardware stack
$swstack = 10                                               ' default use 10 for the SW stack
$framesize = 40                                             ' default use 40 for the frame space

$crystal = 4000000                                         ' used crystal frequency
$baud = 19200                                               ' use baud rate


Enable Interrupts

Config Clock = Soft
Config Date = YMD , Separator =.' ANSI-Format

dim dateArr(6) As Byte
dim gd as byte
dim wday as byte, repetition as byte, month as byte, year as byte, Strweekday as string * 10

declare Function repetition_of_day( byval wday as byte, byval repetition as byte, byval month as byte, byval year as byte) as byte

$sim

'main


' Get Day from 3rd repetition from July 2019
' in case result is "0", it is no valid Result!
  wday = 1        ' 0-6  0=Monday
  repetition = 3  ' nth repetition of Day
  month = 7
  year = 19
  gd = repetition_of_day( wday, repetition, month, year)
  if gd > 0 then
    Strweekday = Lookupstr(wday , Weekdays)
    print "Search: repetition #"+str(repetition)+" of "+Strweekday+" in "+str(month)+"-20"+str(year)+" is "+str(gd)+"-"+str(month)+"-20"+str(year)
  else
    print "Search: repetition #"+str(repetition)+" of "+Strweekday+" in "+str(month)+"-20"+str(year)+" is not in month Range"
  end if


end
'end of main




Function repetition_of_day( byval wday as byte, byval repetition as byte, byval month as byte, byval year as byte) as byte
' wday = 0 to 6, Monday starts with 0
' repetition = nth repetition of Weekday "wday"
local fwom as byte, ldom as byte, hb as byte, foow as byte, Lsyssec As Long


  dateArr(1) = 0     'sec
  dateArr(2) = 0     'min
  dateArr(3) = 1     'hour
  dateArr(4) = 1     'day
  dateArr(5) = month 'month
  dateArr(6) = year  'year
  ' get first Weekday of month
  Lsyssec = Syssec(dateArr(1))
  fwom = DayOfWeek(lsyssec)

  ' getting last day of month
  ' 1) get first day from following month
  if month = 12 then
     dateArr(5)=1
     incr dateArr(6)
  else
     incr dateArr(5)
  end if
  Lsyssec = Syssec(dateArr(1))
  ' 2) substract one Day, so we've last day in month
  Lsyssec=Lsyssec-86400
  dateArr(4)=Date(Lsyssec)
  ' 3) get last Day of Month
  ldom=dateArr(4)

  ' calculate Day of first occurence of wday (foow) in month
  if wday = fwom then
     foow = wday
  elseif wday > fwom then
     foow = wday - fwom
  else
     foow = fwom - wday
     foow = foow + 7
  end if
  ' now "foow" holds the first Weekday we're searching for as Day in Month in given month and year

  ' we've to check if the nth "repetition" of our Weekday is in Range of month!
  decr repetition
  hb = repetition * 7
  hb=hb+foow
  incr hb
  if hb <= ldom then
    ' it's ok! we're inside the month Range
    repetition_of_day = hb
  else
    ' we're outside the day-Range of Month
    repetition_of_day = 0
  end if
end function



Weekdays:

Data "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" , "Sunday"
 


Thank you very much! This seems to do what I needed!
Back to top
View user's profile
six1

Bascom Expert



Joined: 27 Feb 2009
Posts: 553

germany.gif
PostPosted: Tue Jul 23, 2019 4:59 pm    Post subject: Reply with quote

Hi Ken,
i haven't had much time today... please have a look at source once more. I corrected calculation of first occurence of day we search...


best, michael

_________________
For technical reasons, the signature is on the back of this message.
Back to top
View user's profile
KenHorse

Bascom Member



Joined: 16 Jul 2004
Posts: 523

blank.gif
PostPosted: Tue Jul 23, 2019 5:10 pm    Post subject: Reply with quote

six1 wrote:
Hi Ken,
i haven't had much time today... please have a look at source once more. I corrected calculation of first occurence of day we search...


best, michael


I must have tried it after the fix as I have not had an entered nth occurrence of a day yet fail to produce the correct result.

Again, thank you
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Tue Jul 23, 2019 5:27 pm    Post subject: Reply with quote

KenHorse wrote:
I must have tried it after the fix as I have not had an entered nth occurrence of a day yet fail to produce the correct result.

There was no fix, the previous code puts out nonsense, as well as the current code does.
My previous message from 2:38 pm was for a reason, it reflected the tests I made with this code.
At least it's truly KenHorse-style code, shooting the horse from the back. Very Happy

@Six1:
I'd suggest to post new code as new and don't edit old code in old messages, nobody can keep pace with such mess.
Back to top
View user's profile
six1

Bascom Expert



Joined: 27 Feb 2009
Posts: 553

germany.gif
PostPosted: Tue Jul 23, 2019 6:23 pm    Post subject: Reply with quote

@KenHorse, plz try again. Corrected above Code
_________________
For technical reasons, the signature is on the back of this message.
Back to top
View user's profile
KenHorse

Bascom Member



Joined: 16 Jul 2004
Posts: 523

blank.gif
PostPosted: Tue Jul 23, 2019 6:37 pm    Post subject: Reply with quote

MWS wrote:
KenHorse wrote:
I must have tried it after the fix as I have not had an entered nth occurrence of a day yet fail to produce the correct result.

There was no fix, the previous code puts out nonsense, as well as the current code does.
My previous message from 2:38 pm was for a reason, it reflected the tests I made with this code.
At least it's truly KenHorse-style code, shooting the horse from the back. Very Happy

@Six1:
I'd suggest to post new code as new and don't edit old code in old messages, nobody can keep pace with such mess.


Really?
Back to top
View user's profile
six1

Bascom Expert



Joined: 27 Feb 2009
Posts: 553

germany.gif
PostPosted: Tue Jul 23, 2019 6:54 pm    Post subject: Reply with quote

made a loop for month and result is:

Search: repetition #3 of Sunday in 1-2019 is 20-1-2019
Search: repetition #3 of Sunday in 2-2019 is 17-2-2019
Search: repetition #3 of Sunday in 3-2019 is 17-3-2019
Search: repetition #3 of Sunday in 4-2019 is 21-4-2019
Search: repetition #3 of Sunday in 5-2019 is 19-5-2019
Search: repetition #3 of Sunday in 6-2019 is 16-6-2019
Search: repetition #3 of Sunday in 7-2019 is 21-7-2019
Search: repetition #3 of Sunday in 8-2019 is 18-8-2019
Search: repetition #3 of Sunday in 9-2019 is 15-9-2019
Search: repetition #3 of Sunday in 10-2019 is 20-10-2019
Search: repetition #3 of Sunday in 11-2019 is 17-11-2019
Search: repetition #3 of Sunday in 12-2019 is 15-12-2019

_________________
For technical reasons, the signature is on the back of this message.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Tue Jul 23, 2019 7:32 pm    Post subject: Reply with quote

KenHorse wrote:
Really?

Really.
The code from your post Jul 23, 2019 4:52 pm gives with these values:
Code:
  wday = 6        ' 0-6  0=Monday
  repetition = 1  ' nth repetition of Day
  month = 12
  year = 19

this result:
Quote:
Search: repetition #1 of Sunday in 12-2019 is 7-12-2019

Which is not the case.

The code from six1 from Jul 23, 2019 10:40 am, which six1 did edit already two times, puts out:
Quote:
Search: repetition #1 of Sunday in 12-2019 is 2-12-2019

Which is wrong too.

This means your shown codes work randomly, I'd call it guesswork-code.
It may be that there exists not shown code which produces reliable results.
I know it's feasible, well, it took me a hour. Wink

Currently as I write this post, there is no correctly working code shown.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Tue Jul 23, 2019 7:38 pm    Post subject: Reply with quote

six1 wrote:
Search: repetition #3 of Sunday in 12-2019 is 15-12-2019

No one of your shown code is able to produce that result.

Either you're making things up, or finally you've found your grain of corn.
If you've found it, show it.

Edit:
Quote:
Posted: Tue Jul 23, 2019 10:40 am Post subject: Reply with quote
...
Last edited by six1 on Tue Jul 23, 2019 7:44 pm; edited 3 times in total

3rd edit and you finally got it, applause. That was hard, wasn't it?

And as you're in the way of editing, here's one more:
Code:
print "Search: repetition #"+str(repetition)+" of "+Strweekday+" in "+str(month)+"-20"+str(year)+" is not in month Range"

returns trash in the simulator's UART0 window.
If you try it the correct way, it works:
Code:
print "Search: repetition #"; repetition; " of "; Strweekday; " in ";month; "-20"; year;" is not in month Range"

Also I would put:
Code:
Strweekday = Lookupstr(wday , Weekdays)

somewhere else, as if the 'in range' branch is not followed, you display an empty value.


Last edited by MWS on Tue Jul 23, 2019 8:23 pm; edited 2 times in total
Back to top
View user's profile
KenHorse

Bascom Member



Joined: 16 Jul 2004
Posts: 523

blank.gif
PostPosted: Tue Jul 23, 2019 8:11 pm    Post subject: Reply with quote

six1 wrote:
made a loop for month and result is:

Search: repetition #3 of Sunday in 1-2019 is 20-1-2019
Search: repetition #3 of Sunday in 2-2019 is 17-2-2019
Search: repetition #3 of Sunday in 3-2019 is 17-3-2019
Search: repetition #3 of Sunday in 4-2019 is 21-4-2019
Search: repetition #3 of Sunday in 5-2019 is 19-5-2019
Search: repetition #3 of Sunday in 6-2019 is 16-6-2019
Search: repetition #3 of Sunday in 7-2019 is 21-7-2019
Search: repetition #3 of Sunday in 8-2019 is 18-8-2019
Search: repetition #3 of Sunday in 9-2019 is 15-9-2019
Search: repetition #3 of Sunday in 10-2019 is 20-10-2019
Search: repetition #3 of Sunday in 11-2019 is 17-11-2019
Search: repetition #3 of Sunday in 12-2019 is 15-12-2019


Those are the results I obtain as well (although I did change the printout to reflect the US standard of M-D-Y

thanks again for your effort and assistance
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum