364,085 Members | 5253 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Want to get rid of UTC offset in serialization of DateTime

BjörnHolmberg
P: n/a
BjörnHolmberg
Hi everyone!

In the following code we get a UTC offset in xml. Since I want my WAP users in
UTC+02:00 to see data on a server in some US time zone, a lot of confusion will
be created from this behavior. Either I use .NET logic on the server and get
server local time or I create xhtml pages directly from serialized objects.
Eitherway I'll have to write my own logic to take care of utc offset. Is it
possible to override XmlSerializer so what my local desktop user send to the
webservice is saved on the server without utc offset? That way, the time that is

local to my users will be saved on the server. I guess it's kind of hard to
tweak ASP.NET WebService infrastructure into this. On the other hand it feels
like bad manners to change my xsd:dateTime values to xsd:string because of what
i discover about .NET. /Regards Bjorn

public class SomeDates {
public DateTime Summer;
public DateTime Winter;
}

public static void TestSomeDates() {
SomeDates obj = new SomeDates();
obj.Winter = DateTime.Parse("2003-12-22 12:32:45");
obj.Summer = DateTime.Parse("2004-08-05 18:45:21");
XmlSerializer serializer = new XmlSerializer(typeof(SomeDates));
StreamWriter writer = new StreamWriter("obj.xml");
serializer.Serialize(writer, obj);
writer.Close();
}





Nov 12 '05 #1
Share this Question
Share on Google+
2 Replies


Dino Chiesa [Microsoft]
P: n/a
Dino Chiesa [Microsoft]
This example shows some of the possibilities.
http://www.winisp.net/cheeso/srcview...le=DateTime.cs

-D


"BjörnHolmberg" <bjRemovethiSorn.holmberg@suliteAndthiSlma.com> wrote in
message news:411345C6.4AFD1A22@suliteAndthiSlma.com...[color=blue]
> Hi everyone!
>
> In the following code we get a UTC offset in xml. Since I want my WAP[/color]
users in[color=blue]
> UTC+02:00 to see data on a server in some US time zone, a lot of confusion[/color]
will[color=blue]
> be created from this behavior. Either I use .NET logic on the server and[/color]
get[color=blue]
> server local time or I create xhtml pages directly from serialized[/color]
objects.[color=blue]
> Eitherway I'll have to write my own logic to take care of utc offset. Is[/color]
it[color=blue]
> possible to override XmlSerializer so what my local desktop user send to[/color]
the[color=blue]
> webservice is saved on the server without utc offset? That way, the time[/color]
that is[color=blue]
>
> local to my users will be saved on the server. I guess it's kind of hard[/color]
to[color=blue]
> tweak ASP.NET WebService infrastructure into this. On the other hand it[/color]
feels[color=blue]
> like bad manners to change my xsd:dateTime values to xsd:string because of[/color]
what[color=blue]
> i discover about .NET. /Regards Bjorn
>
> public class SomeDates {
> public DateTime Summer;
> public DateTime Winter;
> }
>
> public static void TestSomeDates() {
> SomeDates obj = new SomeDates();
> obj.Winter = DateTime.Parse("2003-12-22 12:32:45");
> obj.Summer = DateTime.Parse("2004-08-05 18:45:21");
> XmlSerializer serializer = new XmlSerializer(typeof(SomeDates));
> StreamWriter writer = new StreamWriter("obj.xml");
> serializer.Serialize(writer, obj);
> writer.Close();
> }
>
>
>
>
>[/color]


Nov 12 '05 #2

BjörnHolmberg
P: n/a
BjörnHolmberg
Based on the ideas Dino sent me, I managed to find out a working solution to my
problem. I also realised that it's possible to keep xsd:dateTime in my schema and to
use it in a Web Service. Only drawback is that the wsdl can't be autogenerated.
Here's roughly what I came up with:

public class Approval {
public string Manager;
public int ProjectId;
public string Location;
public string ApprovedAt;

[XmlIgnore]
public DateTime LocalApprovedAt {
get {
return DateTime.Parse(ApprovedAt);
}
set {
ApprovedAt = value.ToString("yyyy-MM-ddTHH:mm:ss");
}
}
}

public static void TestApproval() {
Approval obj = new Approval();
obj.Manager = "Smith";
obj.ProjectId = 1234;
obj.Location = "Pleasantville";
obj.LocalApprovedAt = DateTime.Parse("2002-02-28 13:48:00");
XmlSerializer serializer = new XmlSerializer(typeof(Approval));
StreamWriter writer = new StreamWriter("obj.xml");
serializer.Serialize(writer, obj);
writer.Close();
}

/Bjorn

"Dino Chiesa [Microsoft]" wrote:
[color=blue]
> This example shows some of the possibilities.
> http://www.winisp.net/cheeso/srcview...le=DateTime.cs
>
> -D
>
> "BjörnHolmberg" <bjRemovethiSorn.holmberg@suliteAndthiSlma.com> wrote in
> message news:411345C6.4AFD1A22@suliteAndthiSlma.com...[color=green]
> > Hi everyone!
> >
> > In the following code we get a UTC offset in xml. Since I want my WAP[/color]
> users in[color=green]
> > UTC+02:00 to see data on a server in some US time zone, a lot of confusion[/color]
> will[color=green]
> > be created from this behavior. Either I use .NET logic on the server and[/color]
> get[color=green]
> > server local time or I create xhtml pages directly from serialized[/color]
> objects.[color=green]
> > Eitherway I'll have to write my own logic to take care of utc offset. Is[/color]
> it[color=green]
> > possible to override XmlSerializer so what my local desktop user send to[/color]
> the[color=green]
> > webservice is saved on the server without utc offset? That way, the time[/color]
> that is[color=green]
> >
> > local to my users will be saved on the server. I guess it's kind of hard[/color]
> to[color=green]
> > tweak ASP.NET WebService infrastructure into this. On the other hand it[/color]
> feels[color=green]
> > like bad manners to change my xsd:dateTime values to xsd:string because of[/color]
> what[color=green]
> > i discover about .NET. /Regards Bjorn
> >
> > public class SomeDates {
> > public DateTime Summer;
> > public DateTime Winter;
> > }
> >
> > public static void TestSomeDates() {
> > SomeDates obj = new SomeDates();
> > obj.Winter = DateTime.Parse("2003-12-22 12:32:45");
> > obj.Summer = DateTime.Parse("2004-08-05 18:45:21");
> > XmlSerializer serializer = new XmlSerializer(typeof(SomeDates));
> > StreamWriter writer = new StreamWriter("obj.xml");
> > serializer.Serialize(writer, obj);
> > writer.Close();
> > }
> >
> >
> >
> >
> >[/color][/color]

Nov 12 '05 #3

Post your reply

Help answer this question



Didn't find the answer to your .NET Framework question?

You can also browse similar questions: .NET Framework