Daylight Savings Change

Is Vb.Net not aware my clock got set forward?

Page 1 of 1

6 Replies - 6464 Views - Last Post: 09 March 2009 - 04:58 PM Rate Topic: -----

#1 Wrecks   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 71
  • Joined: 24-February 09

Daylight Savings Change

Posted 08 March 2009 - 08:48 PM

Dim StartTime As Date = New Date(2009, 3, 8, 1, 59, 0, DateTimeKind.Local)
Dim EndTime As Date = New Date(2009, 3, 8, 3, 1, 0, DateTimeKind.Local)
Dim TimeDifference As TimeSpan = EndTime - StartTime
Dim Minutes As UShort = DateDiff(DateInterval.Minute, StartTime, EndTime)


TimeDifference returns one hour and two minutes.
Minutes returns 62.

Is Vb.Net not aware that the clock got set forward? Or am I doing something the wrong way?

Is This A Good Question/Topic? 0
  • +

Replies To: Daylight Savings Change

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Daylight Savings Change

Posted 09 March 2009 - 04:37 AM

You're entering specific times in there, without reference to the current time, and taking a difference. DST has nothing to do with it. The difference between 0159 and 0301 is always going to be 62 minutes.
Was This Post Helpful? 0
  • +
  • -

#3 Wrecks   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 71
  • Joined: 24-February 09

Re: Daylight Savings Change

Posted 09 March 2009 - 07:09 AM

View PostJackOfAllTrades, on 9 Mar, 2009 - 03:37 AM, said:

You're entering specific times in there, without reference to the current time, and taking a difference. DST has nothing to do with it.

I would accept that statement if we were talking UTC times, but as you can see in my code, I specifically created instances in local time (DateTimeKind.Local) in both places. My computer knows whether each time instance is in daylight savings time or not because when I check the .IsDaylightSavingTime property of each it is correct. Yet, when my code calcuates the time difference, it always returns an hour and two minutes instead of the two minutes it should be.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Daylight Savings Change

Posted 09 March 2009 - 07:38 AM

Ah, I see...you're spanning the 2AM time when it switches over.
Was This Post Helpful? 0
  • +
  • -

#5 Wrecks   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 71
  • Joined: 24-February 09

Re: Daylight Savings Change

Posted 09 March 2009 - 10:53 AM

This is what I settled on. Somebody please tell me why the two If blocks are necessary...

Dim StartTime As Date = DateTime.SpecifyKind(#3/8/2009 1:59:00 AM#, DateTimeKind.Local)
Dim StartTimeIsInDaylightSavingsTime As Boolean = StartTime.IsDaylightSavingTime
Dim EndTime As Date = DateTime.SpecifyKind(#3/8/2009 3:01:00 AM#, DateTimeKind.Local)
Dim EndTimeIsInDaylightSavingsTime As Boolean = EndTime.IsDaylightSavingTime
Dim TimeDifference As TimeSpan = EndTime - StartTime
Dim MinutesBetweenTimes As UShort = DateDiff(DateInterval.Minute, StartTime, EndTime)

If (StartTimeIsInDaylightSavingsTime = False And EndTimeIsInDaylightSavingsTime = True) Then
TimeDifference = TimeDifference - New TimeSpan(1, 0, 0)
MinutesBetweenTimes = MinutesBetweenTimes - 60
End If

If (StartTimeIsInDaylightSavingsTime = True And EndTimeIsInDaylightSavingsTime = False) Then
TimeDifference = TimeDifference + New TimeSpan(1, 0, 0)
MinutesBetweenTimes = MinutesBetweenTimes + 60
End If

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Daylight Savings Change

Posted 09 March 2009 - 01:42 PM

Sounds like a question you'd have to ask Microsoft.
Was This Post Helpful? 0
  • +
  • -

#10 Wrecks   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 71
  • Joined: 24-February 09

Re: Daylight Savings Change

Posted 09 March 2009 - 04:58 PM

I took my first comment a little further and finally succeeded. Here is the result.

Dim StartTime As Date = DateTime.SpecifyKind(#3/8/2009 1:59:00 AM#, DateTimeKind.Local)
Dim EndTime As Date = DateTime.SpecifyKind(#3/8/2009 3:01:00 AM#, DateTimeKind.Local)
Dim TimeDifference As TimeSpan = EndTime.ToUniversalTime - StartTime.ToUniversalTime
Dim MinutesBetweenTimes As UShort = DateDiff(DateInterval.Minute, StartTime.ToUniversalTime, EndTime.ToUniversalTime)

Was This Post Helpful? 1

Page 1 of 1