How to convert EPG XML Time to UTC - Silverlight - WP7
I am working on project to convert Standard time + DST ( "20110710115500 +1200" ) to actual time.
It is on an XML file and I can pull and display the data, but I am looking to convert it so its readable.
Eg.. "20110710115500 +1200" to 11:55:00 10/07/2011
I am using visual studio and silver light, and its for a windows phone application.
I have been reading about TimeZoneInfo.ConvertTimeToUtc Method (DateTime, TimeZoneInfo), but I can't seem to get that to work and I was hoping someone could point me in the right direction.
Thanks
My Code..... StartTime and EndTime and the dates I need to change.
EDIT: I have updated the code with your changes, but it is giving me an error when I attempt to run on emulator.
ERROR:
"When converting a string to DateTime, parse the string to take the date before putting each variable into the date time object"
Noted: Your right about the C# learning, I am currently working my way through a c# book. Thanks again for your help on this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Xml;
using System.Xml.Linq;
namespace tvGuide
{
public partial class TV2 : PhoneApplicationPage
{
public TV2()
{
InitializeComponent();
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
WebClient c = new WebClient();
c.DownloadStringCompleted += new DownloadStringCompletedEventHandler(c_DownloadStringCompleted);
c.DownloadStringAsync(new Uri("http://www.designized.com/tv/freeview.xml?"));
}
void c_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
return;
var r = XDocument.Parse(e.Result);
listBox2.ItemsSource = from tv in r.Root.Descendants("programme")
let channelE1 = tv.Attribute("channel")
let nameEl = tv.Element("title")
let urlEl = tv.Element("desc")
let startE1 = tv.Attribute("start")
let endE1 = tv.Attribute("stop")
//let iconEl = tv.Element("icon")
select new TV2guide
{
DisplayName = nameEl == null ? null : nameEl.Value,
ChannelName = channelE1 == null ? null : channelE1.Value,
ChannelURL = urlEl == null ? null : urlEl.Value,
StartTime = startE1 == null ? (DateTime?)null : DateTime.Parse(startE1.Value),
EndTime = endE1 == null ? (DateTime?)null : DateTime.Parse(endE1.Value),
//ImageSource = iconEl == null ? null : iconEl.Attribute("src").Value,
};
}
private void button3_Click_1(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
private void button4_Click_1(object sender, RoutedEventArgs e)
开发者_JS百科 {
NavigationService.GoBack();
}
}
public class TV2guide
{
public string DisplayName { get; set; }
public string ChannelURL { get; set; }
public string ImageSource { get; set; }
public DateTime? StartTime { get; set; }
public DateTime? EndTime { get; set; }
public string ChannelName { get; set; }
}
}
First you need to get the XML value into a DateTime variable
DateTime showTime = DateTime.Parse(xmlValue);
From there you will be able to manipulate it as needed to get it into the right timezone. There are ToLocalTime() and ToUniversalTime() methods.
To get it back into a string to display you can use the .ToString() method and pass a format in.
showTime.ToString("HH:mm:ss dd/MM/yyyy");
Formatting information is on MSDN http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
Edit: One more thing to consider, having written a Windows Phone EPG myself, is to check if the time your are being provided is actually correct for all timezones. For example we have national channels, and the news starts at 6pm local time, but the EPG source I was using had a single file for the national channel with times set to the east coast. So I had to just drop the timezone information from the XML and treat it as local time for those channels.
Edit 2: You really need to learn the basics of C# before getting too far into this by the sounds of it. In your class definition TV2guide, change the proeprties StartTime and EndTime to be of type DateTime like this.
public DateTime? StartTime { get; set; }
public DateTime? EndTime { get; set; }
In your LINQ-2-XML query change the lines that set StartTime and EndTime like this
StartTime = startE1 == null ? (DateTime?)null : DateTime.ParseExact(startE1.Value, "yyyyMMddHHmmss zzz", DateTimeFormatInfo.CurrentInfo, DateTimeStyles.AssumeLocal),
EndTime = endE1 == null ? (DateTime?)null : DateTime.ParseExact(endE1.Value, "yyyyMMddHHmmss zzz", DateTimeFormatInfo.CurrentInfo, DateTimeStyles.AssumeLocal),
Sorry to be bearer of bad news, but if your application needs accuracy during daylight saving changes, then it is not possible to convert any local time to UTC. Local time does not uniquely identify an actual time. Sounds implausible doesnt it?
Example: Say you have a local time of 2:30am on the morning that you transition to daylight saving. Darn! Which 2:30am is it? When the clock reached 3:00am (in our zone) the local time moves back to 2am, so 2:30am occurs twice.
So, if you are doing something like measuring the time between two event there is no substitue for UTC. Having hit this problem so many times I recon that all data acquisition "should" be in UTC and only converted to local for visualisation. It is kinda a fundamental problem.
But that is not really what you asked. If multiple 2:30am's once a year does not matter for your app here is some sample code for you, this code goes the opposite but you should be able to get some use from it (.Net C#):
var zoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeLocation);
var localTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, zoneInfo);
zoneInfo is a string retrieved from the source PC.
精彩评论