Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Zeller's congruence to get day of week
#11
(09-16-2024, 12:16 PM)SMcNeill Wrote:
Code: (Select All)
CheckDate = Date$
CheckTime = Time$
If CheckDate <> Date$ Then ' Midnight rolled over
    CheckDate = Date$
    CheckTime = Time$
End If

I don't think the above is going to catch any Midnight roll over for you.  Follow along with me and think of this little scenario:

CheckDate = Date$  '23:59.9998 time (HH:MM:SSSS)
CheckTime = Time$  '23:59.9998 time
If CheckDate <> Date$ Then ' 23:59.9999 time  ==> OH NOS!  TIME DOESN'T MATCH!  GET NEW TIME!
    CheckDate = Date$ '          23:59.9999 time
    CheckTime = Time$ '          00:00.0000 time  <-- Midnight just rolled over between those times!
End If[/code]

As you can see, you're not preventing the Date from falling behind.
You're doing an apples vs. oranges comparison. I'm not comparing the time, time value does not matter, because either the date changed, or it didn't. And what I wrote works either way

The concept that the date and time changes after the comparison test, does not matter. There are only two possible states: Date changed between the retrieval of the date and the comparison, or date did not change. In almost every case, the "did not change" will be the default state.  Which means the date did not change from the time the first request is made and the comparison. Since the time is requested after the first date request, the date and time are in sync. So it does not matter that the time or date changed after the test, because the date and time are in sync at that instant. 

If the change of dates during the comparison did happen, midnight has occurred, it cannot happen again for 24 hours, so the date and time of the second request will be in sync.

There is another method to do this, I had forgotten I wrote on 3/16:
Code: (Select All)

Sub GetDateAndTIME
    Dim Date1$, Date2$, DateA$, TimeA$, Hour$

    ' The following makes the time and date match exactly.
    ' While it is extremely unlikely, the date could change between the time
    ' and the date is collected. So we get the Date first, then the time,
    ' then the date again.
    Date1$ = Date$
    TimeA$ = Time$
    Date2$ = Date$
    Hour$ = Left$(TimeA$, 2): Hour = Val(Hour$)
    Minute$ = Mid$(TimeA$, 4, 2): Minute = Val(Minute$)
    Second$ = Right$(TimeA$, 2): Second = Val(Second$)

    ' Now, all we need is to look at is the hour. If the hour is 0, there is
    ' a small chance the date changed between the first date, and we got the time,
    ' which would mean the first date could be wrong, so we use the date
    ' collected after the time. In all other cases, even at 1 second before midnight, 
    ' if the hour is not 0, the date _cannot_ have changed before the time was collected, so
    ' we use the first date.
    If Hour = 0 Then
        DateA$ = Date2$
    Else
        DateA$ = Date1$
    End If


    '    We keep certain variables as shared
    '    Year, Month, Day, Hour, Minute, Second  are the number values
    '    Year$, Month$, Day$, WeekDay$, AmPm$    are the strings for display
    '    Minute$  because time display of 9 minutes after is "09"
    '    Second$  for the same reason

    Year$ = Right$(DateA$, 4)
    Month$ = Left$(DateA$, 2)
    Day$ = Mid$(DateA$, 4, 2)
    Year = Val(Year$)
    Month = Val(Month$):
    Day = Val(Day$)
    ' Testbed
    'Year$ = "2029": Month$ = "12": Day$ = "31"
    'Year = 2029: Month = 12: Day = 31

    WeekDay$ = GetDay$
    If Hour > 12 Then
        Hour = Hour - 12
        AmPm$ = "P.M."
    Else
        AmPm$ = "A.M."
    End If
    Month$ = MonthList(Month)
    ' On this string, if a number should show with leading 0, string value is used
    ' Where the number should show without it, Str$ is used
    DateString = WeekDay$ + ", " + Month$ + Str$(Day) + ", " + Year$ + " at" + Str$(Hour) + ":" + Minute$ + ":" + Second$ + " " + AmPm$
End Sub

In this case, it's exactly the same. We look at the hour. If it is not 0, it is physically impossible for the time to have changed after the date was collected. If the hour is 0, we don't know if the time changed after the first date collected, we take the second date because, since midnight has already transpired, again, it's physically impossible for midnight to occur again for 23 hours.

I mean, is it strictly necessary to do this, no, but failing to protect against failures is what causes a lot of damage. I prefer to avoid the pain.
While 1
   Fix Bugs
   report all bugs fixed
   receive bug report
end while
Reply
#12
I have edited the first post of this thread to warn people reading the material later that the formula I'm using for Zeller's congruence has some error in it. Beyond that, I made no other changes. As I point out in the warning, I take responsibility for my actions, whether the formula was wrong or I copied it wrong, is irrelevant. It also means you don't hide your mistakes, you learn from them.
While 1
   Fix Bugs
   report all bugs fixed
   receive bug report
end while
Reply
#13
And if you want to know what I'm doing about this saga, check out this posting,  More Problems with (implementations of) Zeller's congruence.
While 1
   Fix Bugs
   report all bugs fixed
   receive bug report
end while
Reply




Users browsing this thread: 2 Guest(s)