Adjusting time zone

I’m in time zone -5:00 and my server is in time zone -6:00 so when I put an HTML5 <time> element in a post I’m using the following

String.Format("{0:yyyy'-'MM'-'dd'T'HH':'mm':'sszzz}",Model.created)

and getting this:

<time pubdate datetime="2011-11-15T08:32:55-06:00">

when it should be

<time pubdate datetime="2011-11-15T09:32:55-05:00">

I know I’m picking at nits but I’m wondering if it’s possible to change the app’s time zone without too much trouble? Can I set it in Web.config? Localization or something like that?

AFAIK it picks up the time zone from the server. Nothing says your server in chicago couldn’t think it was in eastern time.

The right way to do this now would be to use the DateTimeOffset structure and specify the proper offset in the constructor. Remember to adjust for daylight savings.

That’s the reason I was hoping to be able to control the servers reported time zone; so that I wouldn’t have to make adjustments for DST. This can’t be a unique situation.

I ran the date through this

private DateTime EasternTime(DateTime? shiftDate)
{
	return TimeZoneInfo.ConvertTime((DateTime)shiftDate, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));
}

but it didn’t help the offset (it was still -6 and -5 instead of -5 and -4) so I added this to the View:

blogPost.created.Value.IsDaylightSavingTime() ? "-4:00" : "-5:00"

instead of {0:zzz}

It’s not ideal though. I’d much rather set the app’s Time Zone and let it be handled properly.

I imagine I should just be converting it to UTC and putting a Z in there instead of an offset. Like this:

String.Format("{0:yyyy'-'MM'-'dd'T'HH':'mm':'ssZ}", blogPost.created.Value.ToUniversalTime())

Here’s the solution I’m happy with at the moment:

<time pubdate datetime="<%=String.Format("{0:yyyy'-'MM'-'dd'T'HH':'mm':'ssZ}", Model.created.Value.ToUniversalTime()) %>"><%=String.Format("{0:d} at {0:t}", TimeZoneInfo.ConvertTime(Model.created.Value, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"))) %></time>

That way it keeps the pubdate at UTC and displays EST. As long as it still works for EDST.

Yeah, I completely failed to mention that storing UTC is really the way to go, storing localized times is often OK. Except when it is isn’t and then it can be really, really ugly.

What I would probably do is build some smarts into my display model objects to spit out appropriately formatted dates and such, much better to centralize that logic.

That’s the plan for v2. :slight_smile: