Qbasicnews.com
May 25, 2020, 11:02:18 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1] 2
  Print  
Author Topic: Validating a custom date against a calendar  (Read 5198 times)
worchyld
New Member

Posts: 6


« on: August 15, 2003, 06:42:44 AM »

In my game I want to use
 
type curGame
     day as integer
     week as integer
     month as integer
     year as integer
end type
 
You play each 'day' and the day is incremented at the end of each day (thus incrementing the week, month, etc).
 
Now how do I validate my day/week/month/year to ensure that the date I've displayed is a valid one?
 
For example it should detect where there are 30/31 days in a month or whether its a leap-year?
 
Is there a way of validating the date perhaps against a calendar?
 
Thanks.
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #1 on: August 15, 2003, 03:35:04 PM »

WORCHYLD:

You said: "you play each day and the day is incremented at the at the end of each day". Are these "real" days or imaginary days within the game? If they are real days, then why don't you use the DATE$ statement to get the current date?

If you want to keep track of your own imaginary dates, then you are going to need the following:
1) You will need some way of determining "the end of the day" in order to increment the day, week, month, year.
2) You will have to store your date onto a file.
3) You actually won't need to store the week number, you can compute this at any given time.
4) You don't need to validate the date if your program is the only one handling the date file.
5) You will need a routine, or a table lookup, for computing days per month. I can give you this.
6) You will need a function for determining leap year. I can give you this.
7) In order to handle weeks, you will need a routine to convert the date in number of days from year zero. Then you can compute the weeks. I can give both of these too.

Let me say this, handling dates properly is not a simple matter. It is not a lot of code. All the above that I can give you is probably less that 25 lines of code, but it's tricky.
*****
Logged
oracle
*/-\*
*****
Posts: 3652



WWW
« Reply #2 on: August 15, 2003, 10:51:58 PM »

Leap year:

Code:
DEF FNleap% (y%) = (y MOD 4 = 0 AND Y MOD 100 <> 0) OR y MOD 400 = 0
Logged

Moneo
Na_th_an
*****
Posts: 1971


« Reply #3 on: August 15, 2003, 11:55:44 PM »

Yeah, ORA,
That that the one-liner I've been using for years,
except, your posted version needs parentheses around the the last expression..... OR (y MOD 400 = 0)
otherwise it won't work.
*****
Logged
oracle
*/-\*
*****
Posts: 3652



WWW
« Reply #4 on: August 16, 2003, 12:09:05 AM »

Hmmm... more "basic wierdness"? You'd think you wouldn't need the brackets, but hey.
Logged

worchyld
New Member

Posts: 6


« Reply #5 on: August 20, 2003, 09:11:15 AM »

Thanks a lot for all your help.

I downloaded quite a few calendar scripts - I got one of them to work pretty well (with Leap Year functions, etc) - all I do is keep a storage of the day, month and year (I ditched the week idea) and call the appropriate function or test it against the calendar's maximum values.

It seems to be working alright now.

Again, thank you again for all your help
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #6 on: August 20, 2003, 02:39:28 PM »

Good, I'm glad you got it working. Cheesy
*****
Logged
toonski84
__/--\__
*****
Posts: 2567



« Reply #7 on: August 20, 2003, 06:19:58 PM »

I know your problem's soved, but there's one relatively simple way to test a date, increment by days, and calculate the day of the week, and that's using the julian day number.

If you convert to the julian day, and then convert it back to gregorian day, the date is only valid if it converts back to the *same* day, month and year.  Plus, you can use the julian day to increment days, and test the day of the week, since it's a serial date.
Logged

i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Moneo
Na_th_an
*****
Posts: 1971


« Reply #8 on: August 20, 2003, 09:24:38 PM »

Gee Toonski, I hope Worchyld understands your idea about julian dates, because I honestly don't.
*****
Logged
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #9 on: August 20, 2003, 09:40:05 PM »

Julian date is an absolute number. That says that July 30, 1890 is a number like 3248729 or something like that. To increment days, you add 1 (3248730) and then convert to gregorian again: July 31, 1890. There are funcions wich perform both conversions, so you don't have to care about leap years, different number of days in a month and stuff. Also, to calculate a week's day, just MOD 7 the julian date and every result (0, 1, 2,..., 7) always corresponds with the same day.
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
toonski84
__/--\__
*****
Posts: 2567



« Reply #10 on: August 20, 2003, 10:30:49 PM »

well, in perl i do something like this to validate dates:

Code:
$jd = julian ($day, $month, $year);
($nday, $nmonth, $nyear) = gregorian($jd)

'pseudocode for understanding sakes:

if (
     $nday = $day AND
     $nmonth = $month AND
     $nyear = $year
   ) {
     $isvalid = "VALID"
  }


to increase a day:

Code:
$jd = julian ($day, $month, $year);
$jd = $jd + 1;
($day, $month, $year) = gregorian($jd);


and to find the day of the week:

Code:
$jd = julian ($day, $month, $year);
$dayofweek = $jd MOD 7


of course, i substituted some of that with qbasic code but it should be easier to get.  The julian day number, which most formulas are accurrate up to about the next 100 years, is what's called a serial date, the counting of days from a particular date.  It's very useful for date math.
Logged

i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Moneo
Na_th_an
*****
Posts: 1971


« Reply #11 on: August 21, 2003, 12:52:29 AM »

I think someone has redefined the meaning of "julian date". Before the Y2K issue, julian dates were: YYNNN where YY was the year and NNN was the number of days in the year. Example: 03001 would be 01-Jan-03 and 03365 would be 31-Dec-03. This was a standard for decades, and as a matter of fact, IBM standard header labels for files contained this dates in this format.

Now what you guys are saying is that in some language (like the sample code above) they have redefined julian date to be the total number of days of a given date, probably a number of total days after some arbitrarily chosen date in the past, probably 15xx when Gregorian dates began. I've seen a language (PICKBasic) that starts these total days in 1967 for convenience of keeping the number small. For lack of a better term, I always referred to these total days as a "date factor".

However, I can't believe that this total days after some date has yet become an industry standard for the term "julian date". I'll look into this further.
*****
Logged
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #12 on: August 21, 2003, 07:31:58 AM »

Moneo, that julian date has nothing to do with the language, is an older concept:

http://tycho.usno.navy.mil/mjd.html

It has been in use by astronomes and mathematicians for centuries.
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
toonski84
__/--\__
*****
Posts: 2567



« Reply #13 on: August 21, 2003, 01:24:14 PM »

The julian day number isnt the julian calendar created by caesar.  Moneo's confused because you used "julian date", which *is* what he's talking about.   The julian day number was established in the 16th century, named after the guy that invented it, and that's the official title for it.  There is a modern Julian day number that ticks from sometime in the 1850s, but you still have to calculate the big one to get it so there's no point.  

Here's a good link on the subject:
Julian Day Numbers
Logged

i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Moneo
Na_th_an
*****
Posts: 1971


« Reply #14 on: August 21, 2003, 04:04:14 PM »

TOONSKI,
Thanks for backing me up. According to the article for which you provided the link,  my definition of julian date is the same as what NASA uses --- not as "old" as Nathan claims". :bounce:
*****
Logged
Pages: [1] 2
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!