Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Adjusting for different Daylight Saving Time (DST) periods in USA and Europe

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • PBWin Adjusting for different Daylight Saving Time (DST) periods in USA and Europe

    ' The Daylight Saving Time (DST) period is defined differently in USA and Europe.
    '
    ' In USA DST begins on the 2nd Sunday in March and ends on the 1st Sunday in November.
    ' In EU DST begins on the last Sunday in March and ends on the last Sunday in October.
    '
    ' This program calculates the delay in hours of Eastern Time relative to Central
    ' European Time taking the different DST period definitions into account.
    '
    ' Try these dates to see the varying time delay: 14 March 2015 and 30 October 2015
    '
    ' The program applies the DateTimePicker control:
    ' https://msdn.microsoft.com/en-us/lib...=vs.60%29.aspx
    '
    ' Best regards, Erik Christensen
    '
    ' http://ecstep.com/

    Code:
    ' The Daylight Saving Time (DST) period is defined differently in USA and Europe.
    '
    ' In USA DST begins on the 2nd Sunday in March and ends on the 1st Sunday in November.
    ' In EU DST begins on the last Sunday in March and ends on the last Sunday in October.
    '
    ' This program calculates the delay in hours of Eastern Time relative to Central
    ' European Time taking the different DST period definitions into account.
    '
    ' Try these dates to see the varying time delay: 14 March 2015 and 30 October 2015
    '
    ' The program applies the DateTimePicker control:
    ' https://msdn.microsoft.com/en-us/library/aa231249%28v=vs.60%29.aspx
    '
    ' Best regards, Erik Christensen'
    
    ' http://ecstep.com/
    
    #COMPILER PBWIN 10
    #COMPILE EXE
    #DIM ALL
    
    #INCLUDE ONCE "Win32API.inc"
    #INCLUDE ONCE "InitCtrl.inc"
    
    %IDC_LABEL    = %WM_USER + 2110
    %IDC_LABEL2   = %WM_USER + 2111
    %IDC_CALDATE  = %WM_USER + 2150
    '
    FUNCTION DaylightSavingTimeCorrection_USA_EU(BYVAL DateStr AS STRING) AS LONG
    ' based on formulas presented here:
    ' http://delphiforfun.org/Programs/Math_Topics/DSTCalc.htm
        LOCAL i1, i2 AS LONG
        LOCAL day, month, year AS STRING
        LOCAL da, mo, yr AS LONG
        LOCAL USAstart, USAend, EUstart, EUend AS LONG
    '****************************************************************
    '****************************************************************
        ' The extraction of data from the date-string is
        ' based on this format of the string: m/d/y (the USA locale)
        ' This section may need to be modified according to the
        ' default date format of your location (LOCALE)
        i1 = INSTR(DateStr, "/")
        i2 = INSTR(i1+1, DateStr, "/")
    
        day = MID$(DateStr, i1+1, i2-i1-1) : da = VAL(day)
        month = LEFT$(DateStr, i1-1)       : mo = VAL(month)
        year = MID$(DateStr, i2+1)         : yr = VAL(year)
    '****************************************************************
    '****************************************************************
        EUstart = 31 - (4 + 5 * yr \ 4) MOD 7  ' last Sunday in March
        EUend   = 31 - (1 + 5 * yr \ 4) MOD 7  ' last Sunday in October
    
        USAstart = 14 - (1 + 5 * yr \ 4) MOD 7 ' 2nd Sunday in March
        USAend   = 7 -  (1 + 5 * yr \ 4) MOD 7 ' 1st Sunday in November
    
        IF mo = 3 AND da >= USAstart AND da < EUstart THEN FUNCTION = -60 : EXIT FUNCTION
        IF mo = 10 AND da >= EUend THEN FUNCTION = 60 : EXIT FUNCTION
        IF mo = 11 AND da < USAend THEN FUNCTION = 60 : EXIT FUNCTION
        FUNCTION = 0
    END FUNCTION
    '
    CALLBACK FUNCTION DlgProc () AS LONG
    
        LOCAL pNMDTC AS NMDATETIMECHANGE PTR
        LOCAL wszDate AS WSTRINGZ * 64
        LOCAL s AS STRING
        LOCAL HourDifference AS LONG
    
        SELECT CASE CB.MSG
    
            CASE %WM_COMMAND
    
                IF CB.CTL = %IDCANCEL THEN DIALOG END CB.HNDL
    
            CASE %WM_NOTIFY 'Use this to detect changes in the calendar
                ' Set up the NMDATETIMECHANGE pointer passed in CB.LPARAM
                pNMDTC = CB.LPARAM
    
                IF @pNMDTC.hdr.code = %DTN_DATETIMECHANGE THEN 'Get selected date/time
                    ' Here we use API messages to format a system local date/time text,
                    ' but we can also use CONTROL GET TEXT to get the text directly, or
                    ' format individual members of the @hdrX.st SYSTEMTIME structure directly.
    
                    GetDateFormatW %LOCALE_USER_DEFAULT, %DATE_SHORTDATE, _
                        @pNMDTC.st, BYVAL %NULL, wszDate, SIZEOF(wszDate)
    
                    s = wszDate : HourDifference = DaylightSavingTimeCorrection_USA_EU(s) / 60
                    CONTROL SET TEXT CB.HNDL, %IDC_LABEL2, _
                        s + $CRLF + $CRLF + " Eastern Time is "+FORMAT$((6 + HourDifference) , "##") + " hours behind Central European Time" + _
                        $CRLF + $CRLF + "Central European Time is "+FORMAT$((6 + HourDifference) , "##") + " hours ahead of Eastern Time"
                END IF
    
        END SELECT
    
    END FUNCTION
    '
    FUNCTION PBMAIN () AS LONG
    
        LOCAL hDlg AS DWORD
    
        '---------------------------------------------------------------------------
        ' Note: SysMonthCal32 requires COMCTL32.DLL v4.70 (MSIE 3.0) or latter.
        ' Therefore, we must check the version number of COMCTL32, so we'll use the
        ' return value from the helper function InitComCtl32() for this.
        '---------------------------------------------------------------------------
        IF InitComCtl32W(%ICC_DATE_CLASSES) < 4.70@ THEN
            MSGBOX "Sorry, this program needs a later version of COMCTL32.DLL." + $CRLF _
              + "Please install MSIE version 3 or higher, or get the file directly from: " + $CRLF _
              + $CRLF _
              + "http://www.microsoft.com/msdownload/ieplatform/ie/comctrlx86.asp", _
                %MB_ICONWARNING OR %MB_SYSTEMMODAL, "SysMonthCal32 Error"
            EXIT FUNCTION
        END IF
    
        DIALOG NEW PIXELS, 0, "Delay in Eastern Time compared to Central European Time adjusted for " _
                + "Daylight Saving Time", , , 600, 300, %WS_CAPTION OR %WS_SYSMENU OR %WS_VISIBLE OR _
          %DS_3DLOOK OR %DS_NOFAILCREATE OR %DS_SETFONT, %WS_EX_LEFT OR %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR OR _
          %WS_EX_CONTROLPARENT, TO hDlg
    
        CONTROL ADD LABEL,  hDlg, %IDC_LABEL, "Select a date", 268, 50, 120, 30
    
        CONTROL ADD LABEL,  hDlg, %IDC_LABEL2, "", 216, 150, 170, 130, %SS_CENTER
    
        CONTROL ADD BUTTON, hDlg, %IDCANCEL, "&Close", 545, 270, 50, 25
    
        CONTROL ADD "SysDateTimePick32", hDlg, %IDC_CALDATE, "", 216, 90, 170, 25, %WS_CHILD OR %WS_TABSTOP OR %WS_VISIBLE _
          OR %DTS_LONGDATEFORMAT, %WS_EX_LEFT OR %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR OR %WS_EX_CLIENTEDGE
    
        DIALOG SHOW MODAL hDlg CALL DlgProc
    
    END FUNCTION
    Last edited by Erik Christensen; 27 Jul 2015, 08:04 AM. Reason: Corrected small error
Working...
X