SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    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
    Code Csharp:
    String.Format("{0:yyyy'-'MM'-'dd'T'HH':'mm':'sszzz}",Model.created)
    and getting this:
    Code HTML4Strict:
    <time pubdate datetime="2011-11-15T08:32:55-06:00">
    when it should be
    Code HTML4Strict:
    <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?

  2. #2
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,576
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wwb_99 View Post
    ... 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.

  4. #4
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I ran the date through this
    Code Csharp:
    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:

    Code Csharp:
    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:
    Code Csharp:
    String.Format("{0:yyyy'-'MM'-'dd'T'HH':'mm':'ssZ}", blogPost.created.Value.ToUniversalTime())

  5. #5
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Here's the solution I'm happy with at the moment:
    Code Csharp:
    <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.

  6. #6
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,576
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wwb_99 View Post
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •