How to convert pubDate's GMT time to current time zone by using .NET 3.5/2.0

Hello,

I have a question about pubDate element of yahoo rss news feed: http://rss.news.yahoo.com/rss/topstories

<pubDate>Sat, 24 Apr 2010 03:32:38 GMT</pubDate>

I am not sure exactly how to proceed with pubDate element's content. The pubDate has GMT time format. I want to show that specific GMT time in PST (California)/EST(New York) or whatever other timezones are in usa. Say if a person is in Alaska or is in Florida, the pubDate's time will be calculated based on that person's timeZone.

Please help.

I am using C# .NET 3.5 LINQ to display the rss feed.
LVL 3
ashley2009Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_Katka_Commented:
Hi, try this: <see code>

regards,
Kate



String gmt = "Sat, 24 Apr 2010 03:32:38 GMT";
DateTime gmtDateTime;

if (DateTime.TryParse(gmt, CultureInfo.CurrentCulture, DateTimeStyles.AssumeUniversal, out gmtDateTime))
{
    gmtDateTime = DateTime.SpecifyKind(gmtDateTime, DateTimeKind.Utc);
    DateTime localDateTime = gmtDateTime.ToLocalTime();
}
else
{
    throw new ArgumentException("Invalid date was provided!");
}

Open in new window

0
lazyberezovskyCommented:
Well, when you parse date string in format "Sat, 24 Apr 2010 03:32:38 GMT" you always get local time, not GMT time. Suppose you have correct xml feed (so, you do not have to try parse date):

BTW: Use TimeZoneInfo.GetSystemTimeZones() to get time zones list (and time zone id).

string s = "Sat, 24 Apr 2010 03:32:38 GMT";
DateTime localDate = DateTime.Parse(s);
DateTime gmtDate = localDate.ToUniversalTime();
DateTime newYorkTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(localDate, "Eastern Standard Time");

Open in new window

0
_Katka_Commented:
Two corrections:

1) DateTimeStyles.AssumeUniversal should be DateTimeStyles.RoundtripKind
2) IFormatProvider is not required

So the whole code is: <see code>

regards,
Kate
String gmt = "Sat, 24 Apr 2010 03:32:38 GMT";
DateTime gmtDateTime;

if (DateTime.TryParse(gmt, null, DateTimeStyles.RoundtripKind, out gmtDateTime))
{
    gmtDateTime = DateTime.SpecifyKind(gmtDateTime, DateTimeKind.Utc);
    DateTime localDateTime = gmtDateTime.ToLocalTime();
}
else
{
    throw new ArgumentException("Invalid date was provided!");
}

Open in new window

0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

_Katka_Commented:
lazyberezovsky:

First of all hi :) don't you get a shifted time ? Consider this (I'll omit date as it's not a concern in this case):

original: 03:32:38
parsed: 03:32:38 <- so far ok

now in line: localDate.ToUniversalTime()
if you are shifted let's say +2 zone you'll shift really by -2 because you're converting from local to universal)

universal: 1:32:38 <- not true because 3:32:38 is universal

new york time: a correct conversion, but converted from incorrect universal time
(thus -2h incorrect, so only people who are living in GMT zone will get it right)

Maybe I'm wrong, just a guess.

regards,
Kate
0
lazyberezovskyCommented:
Hi Kate :)
Nope, I don't have shifted time. In example above:
localDate = 4/24/2010 06:32:38
gmtDate = localDate.ToUniversalTime() = 4/24/2010 03:32:38 // I'm back to pubDate
newYorkTime = 4/23/2010 23:32:38

See here different time parsing: http://dotnetperls.com/datetime-parse
Parsing of httpTime returns local time.
0
_Katka_Commented:
You're right, my bad, I didn't realize that parse will already convert it to a local time (kind of more than you would ask.. usually, but I guess it has it's logic).

regards,
Kate
0
ashley2009Author Commented:
Hello,

thank you all for your responses. I have observed that by just using DateTime.Parse(httpTime).ToString();

I can produce a correct result. For example below is a code sample, that has some yahoo pubDate element's content. After I excute,

 httpTime = "Sat, 24 Apr 2010 16:31:03 GMT";
        time = DateTime.Parse(httpTime).ToString();
        Console.WriteLine("\n" + httpTime + "Equals to: " + time);

       
        httpTime = "Sat, 24 Apr 2010 14:45:12 GMT";
        time = DateTime.Parse(httpTime).ToString();
        Console.WriteLine("\n" + httpTime + "Equals to: " + time);


     
        httpTime = "Sat, 24 Apr 2010 12:51:28 GMT";
        time = DateTime.Parse(httpTime).ToString();
        Console.WriteLine("\n"+httpTime + "Equals to: " + time);


     
        httpTime = "Sat, 24 Apr 2010 03:53:22 GMT";
        time = DateTime.Parse(httpTime).ToString();
        Console.WriteLine("\n" + httpTime + "Equals to: " + time);

I get result like:

Sat, 24 Apr 2010 16:31:03 GMTEquals to: 4/24/2010 9:31:03 AM
Sat, 24 Apr 2010 14:45:12 GMTEquals to: 4/24/2010 7:45:12 AM
Sat, 24 Apr 2010 12:51:28 GMTEquals to: 4/24/2010 5:51:28 AM
Sat, 24 Apr 2010 03:53:22 GMTEquals to: 4/23/2010 8:53:22 PM



which seems correct, as they matches with
http://rss.news.yahoo.com/rss/topstories

content.

Do you think that it will be really necessary to to use the timeZone class as it seems that httpTime is doing good.
0
lazyberezovskyCommented:
If you want just to display GMT time in local time, then simple parsing will be enough. If you have desktop application, then wherever you run it, you'll get appropriate local time.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.