Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 06:20 25 Apr 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 : Daylight Saving Time (DST) Functions - For Southern Hemisphere

Author Message
Andrew_G
Guru

Joined: 18/10/2016
Location: Australia
Posts: 840
Posted: 11:05pm 03 Oct 2022
Copy link to clipboard 
Print this post

Hi,
I couldn't find a DST Function for the Southern Hemisphere so I doctored the ones on the Fruit of The Shed here as best I could.
I'm happy to adopt better ones or take comments etc.

The following has some simple code to test the functions (it uses TassyJim's DO . . LOOP to exit nested FOR NEXT loops).

Cheers,

Andrew


'DST-AUS.BAS
'
'Functions to check for daylight saving time and return '0' if not active, '1' if active - for the Southern hemisphere.
'The STRING variable "Out" is the DATE and TIME of the resulting clock time.
 
'Fruit of the shed source:
'http://fruitoftheshed.com/MMBasic.Function-to-Test-a-DateTime-to-see-if-Daylight-Savings-should-be-applied.ashx?NoRedirect=1&NS=MMBasic
 
'Dependencies:
'ZPad() - returns a STRING based on an INTEGER number padded with leading zeros
 
'Syntax:
'=IsDST(DateTime)
'
'Examples:
'If IsDST("01-01-1970 00:00:00") Then ...
'hour=hour+IsDST(Now())
 
 Option Explicit
 
 const True  = 1
 const False = 0
 
'Needed to use the functions:
 Dim Integer dd, hh, mm, ss
 dim String In, Out
 
'This section just tests that it is working:
' Check the ending of DST:
 Print "Check the ending of DST:"
 
 do
   For dd = 3 to 3 'We know that in 2022 the 1st Sunday is April 3rd
     for hh = 1 to 3
       for mm = 0 to 59
         for ss = 0 to 0
           In = str$(dd,2,0,"0") + "-04-2022 " + str$(hh,2,0,"0")+":"+Str$(mm,2,0,"0")+":"+str$(ss,2,0,"0")
           print In, IsDST(In), Out
           if IsDST(In) = 0 and mm > 2 then Exit Do
         Next ss
       Next mm
     next hh
   Next dd
 loop until 1=1
 
'Skip:  'Not now used
 print
 pause 5000
 
' Check the Starting of DST
 Print "Check the Starting of DST:"
 do
   For dd = 2 to 2   'We know that in 2022 the 1st Sunday is October 2nd
     for hh = 1 to 3
       for mm = 0 to 59
         for ss = 0 to 0
           In = str$(dd,2,0,"0") + "-10-2022 " + str$(hh,2,0,"0")+":"+Str$(mm,2,0,"0")+":"+str$(ss,2,0,"0")
           print In, IsDST(In), Out
           if IsDST(In) = 1 and mm > 2 then Exit Do
         Next ss
       Next mm
     next hh
   Next dd
 loop until 1=1
 
End
 
'This section is the useful code:
'
'In Australia Daylight Saving Time begins at 2am on the first Sunday in October, when clocks are put forward one hour. It ends at 2am (which is 3am Daylight Saving Time) on the first Sunday in April, when clocks are put back one hour.'
'
Function IsDST(d as string) As Integer
 Select Case Val(Mid$(d,4,2))' extract the month number
   Case 1 To 3, 11 to 12 ' summer months
     IsDST=1
   Case 5 to 9           ' winter months
     IsDST=0
   Case 10
     If Val(Left$(d,2))>7 Then' is summer
       IsDST=1
     Else
       IsDST= not(Epoch(d)<Epoch(FindFirstSunday(d))) 'compare the two Epoch times
     EndIf
   EndIf
 Case 4
   If Val(Left$(d,2))>7 Then' is winter
     IsDST=0
   Else
     IsDST= (Epoch(d)<Epoch(FindFirstSunday(d)))
   EndIf
End Select

Out = DATETIME$(Epoch(d) + IsDST * 3600)

End Function

Function FindFirstSunday(d as string) As String
Local string a
a="07"+Mid$(d,3,9)+"02:00:00"' starting from midnight on the 7th - find the earliest possible Sunday (with embedded time when DST changes)
FindFirstSunday=ZPad(00+((7-Dow(a)) Mod 7),2)+Mid$(a,3)' add the difference in the day number and generate the true change date & time
End Function

'0=Sun
Function Dow(dt as string) As Integer
Local Integer m,d,y
d=Val(Left$(dt,2))
m=Val(Mid$(dt,4,2))
y=Val(Mid$(dt,7,4))
If m<3 Then
 m=m+12
 y=y-1
End If
dow=(d+((26*m+1)/10)-(2*(y\100))+y+(y\4)+(y\400)) Mod 7
End Function

'Syntax:
'=ZPad$(number,totalNumDigits)
'
'Example:
'Print ZPad$(7,3)' returns "007"
'
'Code:
Function ZPad(x As Integer,y As Integer) as string
ZPad=Str$(x,y,0,"0")
End Function

 
Print this page


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

© JAQ Software 2024