Home  |  Contents 

Microcontroller and PC projects
  Forum Index : Microcontroller and PC projects         Section
Subject Topic: A day of the year function. Post ReplyPost New Topic
<< Prev Page of 2
Author
Message << Prev Topic | Next Topic >>
CaptainBoing
Guru
Guru
Avatar

Joined: 07 September 2016
Location: United Kingdom
Online Status: Offline
Posts: 662
Posted: 09 September 2018 at 6:17pm | IP Logged Quote CaptainBoing

really tiny Leapyear function.

returns a boolean so if you want, you can just "add" it to 365 for days in a particular year

http://www.fruitoftheshed.com/MMBasic.IsLeapYear-Function-to-determine-if-the-given-year-is-a-Leap-Year-on-the-Gregorian -western-calendar.ashx


Back to Top View CaptainBoing's Profile Search for other posts by CaptainBoing
 
Paul_L
Guru
Guru


Joined: 03 March 2016
Location: United States
Online Status: Offline
Posts: 426
Posted: 10 September 2018 at 3:51am | IP Logged Quote Paul_L

lew247 wrote:
I'm still having problems with the day of the week function
It keeps saying the day is Sat
Can anyone see any problems with this code?

Hi Lew ... It looks like your code repetitively extracts Year, Month, and Day from the MMVar DATE$. If you run this under DOS then DATE$ will get the current date from DOS (Windowze?). If you run it on a chip it might return the correct date if you have an RTC module hooked up and it's set properly, or if it has access to a GPS receiver. If you just start up a cold PIC chip I don't know what DATE$ will initially return. It does make sense that it will repetitively return the same day, ("Sat"), if DATE$ doesn't change. I have added comments and indented your code below.

Paul in NY

DOW$(0)="Sun":DOW$(1)="Mon":DOW$(2)="Tue":DOW$(3)="Wed":DOW$(4)="Thu":DOW$(5)="Fri":DOW$(6)="Sat"
Year=Val(Mid$(Date$,7,4)):Month=Val(Mid$(Date$,4,2)):Day=Val(Mid$(Date$,1,2))
dowx1$=DOW$(DayOfWeek(YEAR,MONTH,DAY)) 'DOESN'T DO ANYTHING BECAUSE dowx1$ IS NEVER USED BELOW
do 'THIS DO DOESN'T DO ANYTHING BECAUSE THERE IS NO LOOP BELOW 
  print DOW$(DayOfWeek(YEAR,MONTH,DAY))
  pause 2000 'YEAR, MONTH, or DAY don't change during this pause
             'DATE$ will change if you pass midnight during the pause
             'but YEAR, MONTH and DAY still won't change unless you specifically
             'set them again with line #2 above
  print DOW$(DayOfWeek(YEAR,MONTH,DAY)) '@print 2nd time to see if it changes the day after a pause
end

function DayOfWeek(year, month, day)
  print year 'to check if it's correct
  print month 'to check if it's correct
  print day 'to check if it's correct
  local a,m,y
  a = int((14-month)/12)
  m = month + 12*a - 2
  y = year - a
  DayOfWeek = (day + y + int(y/4)-int(y/100)+int(y/400)+int(31*m/12)) mod 7
end function
Back to Top View Paul_L's Profile Search for other posts by Paul_L
 
lew247
Guru
Guru
Avatar

Joined: 23 December 2015
Location: United Kingdom
Online Status: Offline
Posts: 972
Posted: 10 September 2018 at 6:59am | IP Logged Quote lew247

Hi Paul

I only included the actual DOW part of the actual code

Yes when booted up it uses the default date I have told it to use whic is 01-01-1999

After 2 seconds the Picromite has got the current correct local time and date and stored it in DAY$ and DATE$

I'm only trying to get the day of the week after the correct local time and date has been processes

The "only" reason I have the Pause in the code above is to confirm it prints the same day both times - it's not actually needed

Edited by lew247 on 10 September 2018 at 7:00am
Back to Top View lew247's Profile Search for other posts by lew247
 
GoodToGo!
Senior Member
Senior Member
Avatar

Joined: 23 April 2017
Location: Australia
Online Status: Offline
Posts: 163
Posted: 10 September 2018 at 4:25pm | IP Logged Quote GoodToGo!

Hi Lew,

I tried your code in DOS MMBasic ver 5.04.05 Beta 6 and it worked fine after I made a couple of edits:-
I had to declare the variable DOW$(6) at the start,
I commented out the 'do'
I commented out the 'dowx1$=DOW$...etc etc line.

dim DOW$(6) as string

DOW$(0)="Sun":DOW$(1)="Mon":DOW$(2)="Tue":DOW$(3)="Wed":DOW$(4)="Thu":DOW$(5)="Fri":DOW$(6)="Sat"

Year=Val(Mid$(Date$,7,4)):Month=Val(Mid$(Date$,4,2)):Day=Val(Mid$(Date$,1,2))

'dowx1$=DOW$(DayOfWeek(YEAR,MONTH,DAY)) 'DOESN'T DO ANYTHING BECAUSE dowx1$ IS NEVER USED BELOW
'do 'THIS DO DOESN'T DO ANYTHING BECAUSE THERE IS NO LOOP BELOW 
  print DOW$(DayOfWeek(YEAR,MONTH,DAY))
  pause 2000 'YEAR, MONTH, or DAY don't change during this pause
             'DATE$ will change if you pass midnight during the pause
             'but YEAR, MONTH and DAY still won't change unless you specifically
             'set them again with line #2 above
  print DOW$(DayOfWeek(YEAR,MONTH,DAY)) '@print 2nd time to see if it changes the day after a pause
end

function DayOfWeek(year, month, day)
  print year 'to check if it's correct
  print month 'to check if it's correct
  print day 'to check if it's correct
  local a,m,y
  a = int((14-month)/12)
  m = month + 12*a - 2
  y = year - a
  DayOfWeek = (day + y + int(y/4)-int(y/100)+int(y/400)+int(31*m/12)) mod 7
end function


And the result was:-



Is the format of Date$ the same in Picromite as it is in the normal MMbasic?
ie. dd-mm-yyyy format?

Cheers,
GTG!


__________________
...... Don't worry mate, it'll be GoodToGo!
Back to Top View GoodToGo!'s Profile Search for other posts by GoodToGo!
 
lew247
Guru
Guru
Avatar

Joined: 23 December 2015
Location: United Kingdom
Online Status: Offline
Posts: 972
Posted: 10 September 2018 at 5:17pm | IP Logged Quote lew247

Thanks G2G
I thought I'd got everything correct
In my code I had
DIM DOW$ and not DIM DOW$(6)
Tiny little error but stopped it working

Thanks for spotting that

Yes the Date format is the same dd-mm-yyyy

Lewis
Back to Top View lew247's Profile Search for other posts by lew247
 
matherp
Guru
Guru


Joined: 11 December 2012
Location: United Kingdom
Online Status: Offline
Posts: 2404
Posted: 10 September 2018 at 11:58pm | IP Logged Quote matherp

Quote:
In my code I had
DIM DOW$ and not DIM DOW$(6)
Tiny little error but stopped it working


This is an error that should be getting caught by the MMBasic parser when you assign to DOW$(n). I've reported it to Geoff and I'm sure a fix will be included in a subsequent version of MMBasic so that it does generate an error.

DIM DOW$
DOW$(1)="test" ' this statement should generate an error
Back to Top View matherp's Profile Search for other posts by matherp
 
Volhout
Regular Member
Regular Member


Joined: 05 March 2018
Location: Netherlands
Online Status: Offline
Posts: 77
Posted: 11 September 2018 at 4:39pm | IP Logged Quote Volhout

MicroBlocks wrote:
Grogster wrote:

The formula for DayOfWeek came from someone here on the forums, and I think they got it from the net. I understand it is a reasonably well known formula for calculating the day of the week.

Yep, that would have been me. :)

Used that for so many years even back in the GWBasic days.

I know I got it from somewhere (was not the internet at that time!) :)

I made it a bit smaller, and more obscure I think, to fit in a tiny function that can be included when needed.


It was in K&R standard book on C.

Volhout

__________________
If nothing goes right ... turn left
Back to Top View Volhout's Profile Search for other posts by Volhout
 


If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Search the forums using Google.
<< Prev Page of 2
In the news...
 
Post ReplyPost New Topic
Printable version Printable version
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum

Powered by Web Wiz Forums version 7.8
Copyright ©2001-2004 Web Wiz Guide

This page was generated in 0.1553 seconds.
Privacy Policy     Process times : 0, 0, 0, 0.16