Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 15:35 25 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : DST Gotcha

Author Message
DuinoMiteMegaAn
Senior Member

Joined: 17/11/2011
Location: Australia
Posts: 231
Posted: 03:05pm 11 Mar 2012
Copy link to clipboard 
Print this post

Today is DST (Daylight savings time in the US) and with my time/date algorithms I was expecting a hour increase.

You would think that a simple compare would work like

IF Date$ = "11-3-2012" then increase_time_1_hr <------- does not work

A print of Date$ reveals 11-3-2012 <----- The same as the string above ? Both are the same?

The trouble is the LEN of Date$ is 10 chars and the LEN of "11-3-2012" is 9 chars?

I had to convert the Date$ back to numbers and back to strings again for
the compare!

BTW ... Another way is to make sure the compare string/array is LEN of 10.
"11-3-2012" changed to "11-03-2012"
Edited by DuinoMiteMegaAn 2012-03-13
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2914
Posted: 08:59pm 11 Mar 2012
Copy link to clipboard 
Print this post

  DuinoMiteMegaAn said   Today is DST (Daylight savings time in the US) and with my time/date algorithms I was expecting a hour increase.

You would think that a simple compare would work like

IF Date$ = "11-3-2012" then increase_time_1_hr <------- does not work

A print of Date$ reveals 11-3-2012 <----- The same as the string above ? Both are the same?

The trouble is the LEN of Date$ is 10 chars and the LEN of "11-3-2012" is 9 chars?

I had to convert the Date$ back to numbers and back to strings again for
the compare!

BTW ... Another way is to make sure the compare string/array is LEN of 10.
"11-3-2012" changed to "11-03-2012"


Hi DuinoMiteMegaAn,

How did you get a Date$ without the 0 in the middle? Mine always prints in the format of 11-03-2012 ...

So a compare with 11-3-2012 would never work.

Am I confused here, I seem to be missing something.

Mick
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
Gizmo

Admin Group

Joined: 05/06/2004
Location: Australia
Posts: 5078
Posted: 11:36pm 11 Mar 2012
Copy link to clipboard 
Print this post

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
 
DuinoMiteMegaAn
Senior Member

Joined: 17/11/2011
Location: Australia
Posts: 231
Posted: 01:38am 12 Mar 2012
Copy link to clipboard 
Print this post

How did you get a Date$ without the 0 in the middle? Mine always prints in the format of 11-03-2012 ...


@Mike So sorry - I did not display the 03 in the date. Just a typo.

BTW ... I have a RTC that is attached and updates the PIC32 RTC on reset on bootup.
Normally in dates you never should have a leading zero for the month?
 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 04:36am 12 Mar 2012
Copy link to clipboard 
Print this post

  DuinoMiteMegaAn said   Normally in dates you never should have a leading zero for the month?

Many forms these days that want the date want it as 2 digits for day and month as in dd/mm/yyyy. When I use the date in file and folder names, which I do quite often with my photos, I always use yyyy mm dd as it sorts accurately in date order. Any other format is useless for self sorting folders and files.
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024