DST Gotcha


Author Message
Gizmo

Admin Group

Joined: 05/06/2004
Location: Australia
Posts: 5181
Posted: 11:36pm 11 Mar 2012      

I just tested a CGMMSTICK runing 3.1 with the following little conversation.


>print date$
01-01-2000
>date$="12-03-2012"
>print date$
12-03-2012
>date$="12-3-2012"
>print date$
12-03-2012

So its displaying the date in a 10 character format. It even accepted the date as a 9 character format to set the date, and displayed it correctly as a 10 character format.

As MMBasic has no date functions ( minute, hour, day, weekday, weekdayname, month, etc) we have to use string functions to extract the day, month, hour etc from date$ and time$. Easy enough, Year=val(right$(date$,4)), minute=val(mid$(time$,4,2)).

One problem ( and its a big problem ) with dates is the different formats used by different countries. As a software developer its a real mess, and you have to remember a few basic rules or your application can give unexpected results. I write business applications, so I need to get it right.

Classic example is accesing data in a database. This forum uses a Access database to store the posts. Say I wanted to read posts made between 1st January and 1st March, I could use a query like Select id from Posts where PostDate between #1/1/2012# and #1/3/2012#. However, access will only return posts between 1st January and 3rd January. Its using the USA date format of Month/Day/Year ! Fair enough, MSAccess was developed in the USA ( though sorry to our friends in the USA, that is a stupid system ).

The fix is to swap the month and day around before quering the database. But that means your application is region sensitive, and will give unexpected results if used in another country. What I use is a function to convert a date to a long date, so the query above looks like Select id from Posts where PostDate between #1 January 2012# and #1 March 2012#. Access understands this and it works no matter what region the software is run. To get the long date string I use something like LongDateStr=Day(Date) & " " & MonthName(Month(Date)) & " " & Year(Date), in VB Script.

Little tip, if I ask Access for the date of a record, it will return the result in a format based on the region settings of my computer, but if I want to ask it to return a record for the same date, I need to use the US format. It will also "take a guess", choosing between different formats until it finds one that works. eg, 13/6/2012 is a valid date here in Australia, 13th June. But not in the USA, there is no 13th month. So if I ask Access for a record on the 13/6/2012, instead of erroring, it tries other formats. It will then return the correct record, for the 13th June. If I then ask it for a record from the day before, 12/6/2012, it gives me a record for the 6th December! Arghhh!

The US date format is also a problem with expiry dates, but thats another story. Where possible I now try to use the international format of Year/Month/Day, ie, today is 2012/03/12. Cant be confused.

So getting back to MMBasic. We can break up the date into chunks using string manipulations, but if we change region, it going to give wrong dates. Note sure on the Maximite, but the DuinoMite has the ability to change date format to suit the region it lives in, so we need to be carefull here. A application written to use date$ may not work correctly.

GlennEdited by Gizmo 2012-03-13
The best time to plant a tree was twenty years ago, the second best time is right now.
JAQ