Blitz2D Newbies: Uses For CurrentDate$(), Mod and Mid
by Snarty

Ok, recently I needed a better way of date entry and retrieval for a Kooldog project I have been working on. At first the idea was daunting as Guildhall suggested that I should be able top users from entering incorrect dates, and that the date must exists.

So, how could I make Blitz know internally what the current day was and what day would it have been 2 weeks ago, or even worse in 214 days time? At first I though I would need to write an entire data bank for say the next 2 years, containing dates and leap year information, that idea soon passed and I decided instead to expand on the CurrentDate$() function in Blitz.

Decision:

So, do I code in a function specifically for my program or do I write some generic functions I can include in other programs I write. I felt it would be more beneficial to write generic code, not only to expand on my own library of includes but also to hopefully supply a few handy commands to fellow programmers.

Step 1: Set out your initial Data:

Work out how to change the string returned from CurrentDate$() in the form of "DD MMM YYYY" into a more suitable, and numerical value. So for example we will take today as being Friday 26th April 2002.

CurrentDate$() would return "26 Apr 2002", so lets call that CDate$ as follows:


Now, how to convert this into a unique number, so every date entered would be recognizable from another. At first I though maybe storing them as 26+04+2002, but this would have produced multiple repartitions in date numbers. Eventually I decided that the best way would be to work out how many days had elapsed from the 1st January 0000. This way the information returned would be far more valuable to a developer. So…. How to get "26 Apr 2002" to 731346 (that's actually how many days elapsed)

Step 2: Separate your data:

Ok we have the CDate$ containing today's date, and since Blitz always returns in the form DD MMM YYYY, we know where within this data our sub data exists. Characters 1 & 2 are the date, 4 to 6 the month and finally 8 to 11 the year. So it's time to extract that to 3 separate variables using the Mid$() command.


Start at the 1st character "2" and include a total of 2 Characters "26", using the % operand to convert this to an integer value.


Start at the 3rd character "A" and include a total of 3 Characters "Apr", using the $ operand to make sure this value stays as a string. This is not necessary but good practice while learning the basics.


Start at the 8th character "2" and include a total of 4 characters "2002", using the # operand to convert this value into a floating point number, as this value will be used in a division calculation.

So we now have the following variables at our disposal ready to start our calculations on, or do we?


We still have to some how work out that April is the 4th month of the year, other wise calculating the final value will become difficult.

Step 3: Additional Data Required:

So we need some way of checking to see what in numerical values Apr means. We know it means the 4th month of the year, but to the computer it is just three characters. So now would be a good time to add some look up tables (a database to check values across).

So in go the basic information we need to base date look up and calculations with.

Days in a month first:
Data 31,28,31,30,31,30,31,31,30,31,30,31

Now the Days of a week:
Data "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"

And finally months of the year:
Data "January","February","March","April","May","June","July","August", "September","October","November","December"

Put these at the end of the program out of the way. And read them in at the beginning, also set some arrays up to hold the data in.


Ok, so now our arrays hold the basic data from the Data statements we created.

Step 4: Convert the Month Into a Number:

This believe it or not is far simpler than you may think. We already have the month in CMonth$ and we have a list of month names in the array AMonth$(), so all we need to do is a For…Next loop to find our where our month lands from 1 to 12 by matching our CMonth$ with the first 3 characters of each element in the array AMonth$(), so this would be as follows.


Now we have our Month number. I used Upper$ here to make sure there is no problems with case sensitivity this needed be so, but again good practice. Upper$ will change and string into all capitial letters, and again we used Mid$ to check only the first 3 characters of each element in the array AMonth$().

Step 5: Calculate Days Passed:

So now we have our numbers to play with, and need now to count the days. Not as long winded as it sounds. With a little math we can simply work it out.

So 365 days in a year (we wont include leap days just yet), and we have got to 2002. Now because we are in 2002 it is not a complete year so you would have though that we would need to drop one year, but to include 0-1 we keep it as is. So lets create a variable called num% to hold our equation sub totals as we go.


This will equal 730730 so far.

Now those leap days. We know a leap year has 1 extra day and that they occur every 4 years. So we need to divide 2002 by 4, but not round it in the usual way, we need to take the floor() value to get an accurate result. Floor() will return the lowest integer, ie 4.75 would give 4 rather than 5 in its integer form. Then we add the result to the total so far as follows.


This will now equal 731730 days including 500 leap days.

Notice I dropped 1 year from the division, this is to ensure it does not add this years extra day in the "days to date this year" equation below, in the event of this year being a leap year and also passed February. So now we are almost there, we have worked out how many days have passed in full years then added on any additional leap days at the end.

We now need to calculate how many actual days have passed this year. This is where our data and also our conversion from "Apr" to 4 will come into play. First we have our Months() array which holds how many days each month has, and what month we are currently in, so we need to add all the days to the total for whole months passed, then finally add the days passed in the current partial month.


The total will now read 731346, which is how many actual days have passed. You will noticed I made sure that the current month was more than 1 (January), this is because if it is January there would be no complete months to add. Also you will notice a Function called "CheckLeap()" this is listed below and basically returns true for a leap year and false otherwise, plus in order to successfully add this years leap day it also checks that it is actual February in the current check.


Above you see I used the Mod function to good effect. This basically does the Year/4 but instead of returning the result returns the remainder as an integer. If it equalled 0 it would mean that the year being checked was a leap year.

Step 6: Now we can find the Current week day:

So we have our numerical value, great, so what do we do with it, remember at the beginning I wanted to find the actual weekday, such as the test day is a Friday. (hmm what a thing to be doing on a Friday night.. hehe). Now we need a reference point to work from as the computer will not have a clue what the current day is and will need to be told to count back or forward from a set benchmark date. I decided to pick a Monday at random (being 1) and went for 29th April 2002 as the benchmark, knowing this was a Monday, this then calculates to 731349 using the above calculation. So I make a constant with that value to be used in future equations.


Now with that set we create another function called Day() which will return our numeric day number, where 1 would equal Monday and 7 would equal Sunday. These numbers would then tie in with our ADay$() array created holding the weekday names. So the function would read like this.


Above you can see I once again use Mod, but this time to actually retrieve the remaining integer and place it in the variable RDay. The variable we pass NDate% will have been the result passed from the ConvertDate() function. Also I've included a little error checking to make sure there are no horrible crashes or strange results. Lets be honest you cannot assume everyone's system clock is accurate or even set as default do to an empty battery. I could on second thoughts have made the bench date from 0, but hey that's the fun of programming, you not totally stuck to form.

The ConvertDate$() Function in Its Entirity:


Conclusion:

The actual code archive I have uploaded is slightly different than above but is aimed more at separating commands.

Well I hope that made sense or at least helped out in someway.

  • You can download the source from the Showcase entry by clicking HERE.

Regards
Paul Snart (Snarty)


For a printable copy of this article, please click HERE.


This site is Copyright© 2000-2004, BlitzCoder. All rights reserved.