I'm trying to fetch a remote systems time (based on another post) and eventually set the local systems time to the same as the remote system time. But I"m trying to do some display of the values and a difference of the data time values (local vs remote). But when I try to do a ParseExact
on the date format that the remote system is outputting I'm getting an error that it's not a valid date time. Now while I'm trying to do this in C#, I'm very open for another language that I can write using VS 2010.
Here is the code I'm using so far.
private void GetTime_Click(object sender, EventArgs e)
{
var st = DateTime.Now.ToString("ddd MMM dd hh:mm:ss yyy");
System.Net.Sockets.TcpClient t = new System.Net.Sockets.TcpClient("10.10.10.10", 13);
System.IO.StreamReader rd = new System.IO.StreamReader(t.GetStream());
var rt = rd.ReadLine();
DateTime startTime = ParseDateTime(st) ?? DateTime.Now;
DateTime endTime = ParseDateTime(rt) ?? DateTime.Now;
TimeSpan span = endTime.Subtract(startTime);
var ts = span.Seconds;
remoteTime.Text = rt;
systemTime.Text = st;
timeDiff.Text = ts.ToString();
rd.Close();
t.Close();
}
public static DateTime? ParseDateTime(string value)
{
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
DateTimeStyles styles = DateTimeStyles.None;
return DateTime.ParseExact(value, "ddd MMM dd hh:mm:ss yyy", culture, styles);
}