Problem With PutDate() and GetDate()

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
Don_Auld
Posts: 3
Joined: 27 June 2007, 14:10 PM
Location: Vancouver Canada

Problem With PutDate() and GetDate()

Post by Don_Auld »

My Environment:
ZX24a v2.6.1
Zbasic IDE v1.4.5
Compiler version: v2.6.4

I'm having a problem setting the date on the last day of certain months. The PutDate() command fails to set the date correctly on August the 31st of every year. It also fails on Feb 29th and Dec 31st in leap years.

PutDate(2008,2,29) Sets the date to 2008/3/1
PutDate(2008,8,31) Sets the date to 2008/9/1
PutDate(2008,12,31) Sets the date to 2009/1/1
PutDate(2009,8,31) Sets the date to 2009/9/1
PutDate(2010,8,31) Sets the date to 2010/9/1
PutDate(2011,8,31) Sets the date to 2011/9/1
PutDate(2012,2,29) Sets the date to 2012/3/1
PutDate(2012,8,31) Sets the date to 2012/9/1
PutDate(2012,12,31) Sets the date to 2013/1/1

Also...

The sub GetDate(Y,M,D,DayNumber) returns unexpceted results.

Note that August 31st is missing.
GetDate(Y,M,D,3893) returns 2009/8/29
GetDate(Y,M,D,3894) returns 2009/8/30
GetDate(Y,M,D,3895) returns 2009/9/1
GetDate(Y,M,D,3896) returns 2009/9/2

And...

Note that DayNumbers 3346 and 3347 BOTH return the same date.
GetDate(Y,M,D,3345) returns 2008/2/28
GetDate(Y,M,D,3346) returns 2008/3/1
GetDate(Y,M,D,3347) returns 2008/3/1
GetDate(Y,M,D,3348) returns 2008/3/2

I don't have a second ZX24a available so I can't determine if this is a chip problem or a ZBasic anomaly. Has anyone else noticed this problem?

Don
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Problem With PutDate() and GetDate()

Post by dkinzer »

Don_Auld wrote:I'm having a problem setting the date on the last day of certain months.
The problems you mentioned have been confirmed and exist in all ZX devices. We believe that we have resolved the problems and we should have an update available soon.

The September problem exists both for converting a date to a day number and vice versa. The other problems only occur when converting from a day number to a date.
Last edited by dkinzer on 03 November 2008, 10:29 AM, edited 1 time in total.
- Don Kinzer
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Problem With PutDate() and GetDate()

Post by dkinzer »

dkinzer wrote:We believe that we have resolved the problems and we should have an update available soon.
Although we have not yet run the entire testsuite, you can try the release candidate for the ZX-24a containing the changes, available at:
http://www.zbasic.net/download/zvm/2.6/zx24a_2-6-2.zvm

The test code below checks for proper conversion both directions for all supported dates; 1999-01-01 (day number 0) to 2178-06-06 (day number 65535).

Code: Select all

Dim daysInMonth as ByteVectorData({ 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 })

Sub Main()
  Dim year as UnsignedInteger, dayNum as UnsignedInteger
  Dim month as Byte, day as Byte, count as Byte

  dayNum = 0
  count = 0

  ' check all years in the supported day number range
  For year = 1999 to 2178
    ' update the progress indicator
    Debug.Print "*";
    count = count + 1
    If (count >= 75) Then
      Debug.Print
      count = 0
    End If

    ' check each month of the year
    For month = 1 to 12
      ' determine the last day of the current month
      Dim lastDay as Byte
      If &#40;month <> 2&#41; Then
        lastDay = daysInMonth&#40;month&#41;
      ElseIf &#40;&#40;year Mod 4&#41; <> 0&#41; Or &#40;&#40;&#40;year Mod 100&#41; = 0&#41; And &#40;&#40;year Mod 400&#41; <> 0&#41;&#41; Then
        lastDay = 28
      Else
        lastDay = 29
      End If

      ' check each day of the month
      For day = 1 to lastDay
        ' see if the year/month/day is converted to the correct day number
        Call PutDate&#40;year, month, day&#41;
        If &#40;Register.RTCDay <> dayNum&#41; Then
          Debug.Print
          Debug.Print year; "-";
          Debug.Print Right&#40;"0" & CStr&#40;month&#41;, 2&#41;; "-";
          Debug.Print Right&#40;"0" & CStr&#40;day&#41;, 2&#41;; " --> day #"; Register.RTCDay;
          Debug.Print " s/b "; dayNum
        End If
        
        ' see if the day number is converted back correctly
        Dim y as UnsignedInteger, m as Byte, d as Byte
        Call GetDate&#40;y, m, d&#41;
        If &#40;&#40;y <> year&#41; Or &#40;m <> month&#41; Or &#40;d <> day&#41;&#41; Then
          Debug.Print
          Debug.Print "day #"; dayNum; " --> ";
          Debug.Print y; "-";
          Debug.Print Right&#40;"0" & CStr&#40;m&#41;, 2&#41;; "-";
          Debug.Print Right&#40;"0" & CStr&#40;d&#41;, 2&#41;;
          Debug.Print " s/b ";
          Debug.Print year; "-";
          Debug.Print Right&#40;"0" & CStr&#40;month&#41;, 2&#41;; "-";
          Debug.Print Right&#40;"0" & CStr&#40;day&#41;, 2&#41;
        End If

        ' move to the next day number, check for wrap around
        dayNum = dayNum + 1
        If &#40;dayNum = 0&#41; Then
          Exit For
        End If
      Next day

      ' check for day number wrap around
      If &#40;dayNum = 0&#41; Then
        Exit For
      End If
    Next month
  Next year

  Debug.Print
  Debug.Print "Done"
End Sub
- Don Kinzer
Don_Auld
Posts: 3
Joined: 27 June 2007, 14:10 PM
Location: Vancouver Canada

Post by Don_Auld »

Thank you!

Don
Post Reply