WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Calculate Dates Times for Daylight Savings

 Keywords: Win32_LocalTime Win32_TimeZone WMI Calculate Dates Time Daylight Savings DST WMI TimeYmdHms Sunday Monday Tuesday Wednesday Thursday Friday Saturday January February March April May June July August September October November December

;Winbatch 2011A - calculate dates/times for Daylight Saving Settings
;Function DST(computer) -
;
;
;Stan Littlefield, February 19 2012
;////////////////////////////////////////////////////////////////////////////////////////////////

;Function supplied by Detlev
#DefineFunction udfDayOfWeekJulian (strYmdHms)
Return (5 + TimeJulianDay (strYmdHms)) mod 7
#EndFunction

#DefineFunction cvtDate(d)
Return ( ItemExtract(2,d,":"):"/":ItemExtract(3,d,":"):"/":ItemExtract(1,d,":") )
#EndFunction

#DefineFunction DST(computer)
;this works, could use some optimization
cDst=""  ;base var, output will be a concatenated text string
cYear=StrSub(TimeYmdHms(),1,4)  ;base everything on current year
If computer=="" Then computer=Environment("COMPUTERNAME")

;lookups
cOrder="First,Second,Third,Fourth,Last"
cDay="Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday"
cMonth="January,February,March,April,May,June,July,August,September,October,November,December"


cDst=cDst:"Computer: ":computer:@CRLF
oWMI = GetObject("winmgmts:\\" : computer : "\root\cimv2")

;just check WMI to get current local time
colItems = oWMI.ExecQuery("Select * from Win32_LocalTime",,48)
ForEach Item In colItems
   Month = StrFixLeft( Item.Month, 0, 2 )
   Day = StrFixLeft(Item.Day, 0, 2 )
   Year = Item.Year
   Hour = StrFixLeft(Item.Hour, 0, 2 )
   Minute = StrFixLeft( Item.Minute, 0, 2 )
   Second = StrFixLeft(Item.Second, 0, 2 )
   LocalTime = Month:"/":Day:"/":Year:" ":Hour:":":Minute:":":Second
   cDst=cDst:"Current Local Time: ":LocalTime:@CRLF:@CRLF
Next

;now use WMI to get properties for both DST and Standard Time
colItems=0
colItems = oWMI.ExecQuery("Select * from Win32_TimeZone",,48)
ForEach Item In colItems
   DOW= Item.DaylightDayOfWeek
   nDay= Item.DaylightDay
   nSDay= Item.StandardDay
   DMonth= Item.DaylightMonth
   STNDTime = Item.StandardName
   SDOW= Item.StandardDayOfWeek
   SDMonth= Item.StandardMonth

   DSTHour = Item.DaylightHour
   STNDHour = Item.StandardHour

   ;translate property values into lookup values
   DSTDay = ItemExtract(nDay,cOrder,",")
   DSTDOW = ItemExtract(DOW+1,cDay,",")
   DSTMonth = ItemExtract(DMonth,cMonth,",")
   SDSTDay = ItemExtract(nSDay,cOrder,",")
   SDSTDOW = ItemExtract(SDOW+1,cDay,",")
   SDSTMonth = ItemExtract(SDMonth,cMonth,",")

;translate to actual date(s)
   baseDSTDay=1
   baseDST = cYear:":":DMonth:":":baseDSTDay:":":DSTHour:":00:00"
   For i=1 To nDay+1
      If DOW<>udfDayOfWeekJulian(baseDST)
         While DOW<>udfDayOfWeekJulian(baseDST)
            baseDSTDay=baseDSTDay+1
            baseDST = cYear:":":DMonth:":":baseDSTDay:":":DSTHour:":00:00"
         EndWhile
      EndIf
      If i==nDay Then Break
      baseDSTDay=baseDSTDay+1
      baseDST = cYear:":":DMonth:":":baseDSTDay:":":DSTHour:":00:00"
   Next
   RealDST=cvtDate(baseDST)

   baseDSTDay=1
   baseDST = cYear:":":SDMonth:":":baseDSTDay:":":STNDHour:":00:00"
   For i=1 To nSDay+1
      If DOW<>udfDayOfWeekJulian(baseDST)
         While DOW<>udfDayOfWeekJulian(baseDST)
            baseDSTDay=baseDSTDay+1
            baseDST = cYear:":":SDMonth:":":baseDSTDay:":":STNDHour:":00:00"
         EndWhile
      EndIf
      If i==nSDay Then Break
      baseDSTDay=baseDSTDay+1
      baseDST = cYear:":":SDMonth:":":baseDSTDay:":":STNDHour:":00:00"
   Next
   RealSDST=cvtDate(baseDST)

   cDst=cDst:"Daylight Saving Change Due:":@CRLF
   DSTChange = DSTDay:" ":DSTDOW:" ":DSTMonth:" ":cYear:" (":RealDST:")":@CRLF
   cDst=cDst:DSTChange:@CRLF

   cDst=cDst:"Back To Standard Time:":@CRLF
   SDSTChange = SDSTDay:" ":SDSTDOW:" ":SDSTMonth:" ":cYear:" (":RealSDST:")":@CRLF
   cDst=cDst:SDSTChange:@CRLF


colItems=0
oWMI=0
Return(cDst)

#EndFunction

;call the function
Message("",DST(""))

Article ID:   W18304
Filename:   Calculate Dates Times for Daylight Savings.txt
File Created: 2012:02:21:11:15:46
Last Updated: 2012:02:21:11:15:46