Skip to main content

Microsoft Silverlight

Answered Question DateTime are always Local using RIA and Linq to SQL - need it to be UTCRSS Feed

(0)

ericsson007
ericsson007

Member

Member

43 points

36 Posts

DateTime are always Local using RIA and Linq to SQL - need it to be UTC

Is there a (simple/preferred) way to specify that Linq To SQL classes used in my DomainService should consider a DateTime property as UTC (i.e. having the Kind property of the DateTime type to be Utc by default), or is there a 'clean' workaround?

I tried overriding DomainService.UsesUtcDateTimes to return true (as mentioned in http://silverlight.net/forums/t/87574.aspx), but it didn't help in my case.

I'm using Linq to SQL with RIA Services. The time zone on my app-server is not the same as the SQL 2005 Server (cannot change any), and none is UTC. When I persist a property of type DateTime to the dB I use the UTC value (so the value in the db column is UTC), but when I read the values back (using Linq To SQL) I get the .Kind property of the DateTime value to be 'Unspecified'.

The problem is that the UTC value is now 4 hours off (or whatever timezone offset I happen to have on the server). Which of course also means I don't get the correct time when it ends up on the client. I would expect this scenario to be common for most Silverlight applications using RIA and Linq to SQL.

I guess I could add a partial classes with a new property, but I rather have a way to simply specify that the DateTime Property CreatedDateTime is persisted in the dB as UTC in the Db, and that's it.

ericsson007
ericsson007

Member

Member

43 points

36 Posts

Answered Question

Re: DateTime are always Local using RIA and Linq to SQL - need it to be UTC

To close this post off, I ended up with a solution based on http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/2aa5f2f1-00ec-496e-ad60-fb2c0f35f521

In short I changed the generated code for my CreateDT property so that the Property is used instead of the Field, and set .Kind to Utc. Example:

[Column(DbType = "DateTime NOT NULL")] // used to be [Column(Storage="_CreatedDT", DbType="DateTime NOT NULL")]
public System.DateTime CreatedDT
{
get
{
  return this._CreatedDT;
}
set
{
if ((this._CreatedDT != value))
{
if (value.Kind != DateTimeKind.Utc)
{
value = DateTime.SpecifyKind(value, DateTimeKind
.Utc);
}
this.OnCreatedDTChanging(value);
this.SendPropertyChanging();
this._CreatedDT = value;
this.SendPropertyChanged("CreatedDT");
this.OnCreatedDTChanged();
}
}
}

I guess the 'correct' solution is to use SQL Server 2008 and the type DateTimeOffset instead of DateTime.....  

 

manolovalenzuela
manolova...

Member

Member

4 points

11 Posts

Re: DateTime are always Local using RIA and Linq to SQL - need it to be UTC

Hi, i'm from Chile, and our time is -4 !!.

There is a method in the Date class: ToUTCTime() and ToLocalTime()

with this methods you can swap from one to other format time.

You can create a Converter Class and use it on your xaml:

Imports System.Windows.Data

Public Class TimeConverter

Implements IValueConverter

Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert

Dim dt As DateTime = CType(value, DateTime)

Return dt.ToLocalTime.ToShortTimeString

End Function

Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack

Throw New NotImplementedException()

End Function

End Class

xaml:

<UserControl.Resources>

<converter:TimeConverter x:Key="TimeConverter" />

</UserControl.Resources>

...

<TextBlock x:Name="txtTime" Text="{Binding Mode=TwoWay, Path=Time, Converter={StaticResource TimeConverter}}" />

i hope this help to you!

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities