tags:

views:

58

answers:

1

I have following function to get utc time. dtuniversal I pass to stroed proc of sql server data type time. it works fine upto values 6 PM eastern ( which is 23 utc) it fails on 7 PM eastern ( which is 24 utc) I get error "Invalid value for this metadata".

Private Function GetUTCTime(ByVal time As TimeSpan) As TimeSpan

  Dim dt As New DateTime(time.Ticks)
  dt = dt.ToUniversalTime()
  Dim dtUniversal As New TimeSpan(dt.Ticks)
  Return dtUniversal

 End Function
A: 

The following test code suggests that the variable time(aTimeSpan) should be looked at.

Private Sub Button1_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles Button1.Click


    Dim ts As New TimeSpan(DateTime.Now.Ticks) 'create a test timespan
    'Dim ts As New TimeSpan(0,0,0) 'create a test timespan
    Dim oneHr As New TimeSpan(1, 0, 0) 'one hour increments

    For x As Integer = 0 To 24 '25 calls to GetUTCTime
        Debug.WriteLine("     '" & GetUTCTime(ts).ToString)
        ts = ts.Add(oneHr) 'add one hour
    Next
End Sub

Private Function GetUTCTime(ByVal aTimeSpan As TimeSpan) As TimeSpan
    'i don't like to use variable names that are keywords
    Dim dt As New DateTime(aTimeSpan.Ticks)
    dt = dt.ToUniversalTime()
    Dim dtUniversal As New TimeSpan(dt.Ticks)
    Return dtUniversal

End Function

'debug output
'733980.22:23:08.8112022
'733980.23:23:08.8112022
'733981.00:23:08.8112022
'733981.01:23:08.8112022
'733981.02:23:08.8112022
'733981.03:23:08.8112022
'733981.04:23:08.8112022
'733981.05:23:08.8112022
'733981.06:23:08.8112022
'733981.07:23:08.8112022
'733981.08:23:08.8112022
'733981.09:23:08.8112022
'733981.10:23:08.8112022
'733981.11:23:08.8112022
'733981.12:23:08.8112022
'733981.13:23:08.8112022
'733981.14:23:08.8112022
'733981.15:23:08.8112022
'733981.16:23:08.8112022
'733981.17:23:08.8112022
'733981.18:23:08.8112022
'733981.19:23:08.8112022
'733981.20:23:08.8112022
'733981.21:23:08.8112022
'733981.22:23:08.8112022

If you are just changing from local to UTC and back:

'a test date in local time
Dim d As DateTime = DateTime.Now
'convert local to UTC
Dim u As DateTime = d.ToUniversalTime
'convert UTC to local
Dim nd As DateTime = u.ToLocalTime

Either you are making this harder than it is, or I am missing something

    Dim d As DateTime = DateTime.Parse("10:00:00") 'this is local central daylight
    Dim u As DateTime = d.ToUniversalTime 'this is universal
    d = u.ToLocalTime 'double check
dbasnett
When I changed function following way it's saving all values correctly to the database without error. But while again showing back local values conversion works fine if time selected 6PM which is 23 UTC but when user selects time 7pm it saves 00 hours UTC to database but converting this UTC to local doesn not work for hour starting 00 --To convert UTC to Local Dim dt As New DateTime(time.Ticks) dt = dt.ToUniversalTime()-- Local to UTC Dim dt As New DateTime(time.Ticks) dt = dt.ToUniversalTime() 'Dim dtUniversal As New TimeSpan(dt.Ticks) Return New TimeSpan(dt.Hour, 0, 0)
Following statement gives me local time 3 AM which shoud have given me 2 AM ( I am in US eastern time zone)TimeZone.CurrentTimeZone.ToLocalTime(DateTime.Parse( " 07:00:00" ))
All I did is ran this TimeZone.CurrentTimeZone.ToLocalTime(DateTime.Parse( " 07:00:00" )) which should have returned 2 AM (EST) instead it returned 3 AM. I did not make any hard.
If you are off by one hour it could have something to do with DST. When I do this Dim db As DateTime = TimeZone.CurrentTimeZone.ToLocalTime(DateTime.UtcNow) I get the correct local time.
dbasnett