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)
{
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
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.
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.
In your LINQ-2-XML query change the lines that set StartTime and EndTime like this