0

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);
}
Community
  • 1
  • 1
Tim Meers
  • 928
  • 1
  • 14
  • 24

3 Answers3

1

Very odd error.

Try adding another y onto you code as the year has 4 didgets. works for me.

return DateTime.ParseExact(value, "ddd MMM dd hh:mm:ss yyyy", culture, styles);
skyfoot
  • 20,629
  • 8
  • 49
  • 71
  • While that does work sort of, the issue is that the remote time is being read as `Wed Nov 17 10:29:00 2010` or how my format says `ddd MMM dd hh:mm:ss yyy`. So when it does the `TryParse` is fails and skips it. – Tim Meers Nov 17 '10 at 15:30
  • even through the ToString format has 3 y the year is converted to 4 digits. You must match exactly the value being passed in. – skyfoot Nov 17 '10 at 17:24
  • Strange to me how the output of `yyy` is 4 digit, but to parse it you must pass `yyyy`. Never the less, you got it. – Tim Meers Nov 17 '10 at 18:01
  • I was surprised that TryParse did not work. I will have to reassess my trust in TryParse. – skyfoot Nov 18 '10 at 08:52
  • Yesterday presented a problem with this solution, it only handles two digit days. The remote host is responding with only a single day format for days 1-9. Any wildcard usage I could use or a different format to compensate for it and use for 1 and 2 digit days? – Tim Meers Dec 02 '10 at 18:35
0
class RemoteSystemTime 
    {
        static void Main(string[] args)
        {
            try
            {
                string machineName = "vista-pc";

                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.FileName = "net";
                proc.StartInfo.Arguments = @"time \\" + machineName;
                proc.Start();
                proc.WaitForExit();

                List<string> results = new List<string>();
                while (!proc.StandardOutput.EndOfStream)
                {
                    string currentline = proc.StandardOutput.ReadLine();
                    if (!string.IsNullOrEmpty(currentline))
                    {
                        results.Add(currentline);
                    }
                }

                string currentTime = string.Empty;
                if (results.Count > 0 && results[0].ToLower().StartsWith(@"current time at \\" +                                               machineName.ToLower() + " is "))
                {
                    currentTime = results[0].Substring((@"current time at \\" + machineName.ToLower() + " is                             ").Length);

                    Console.WriteLine(DateTime.Parse(currentTime));
                    Console.ReadLine();
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        }
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Kevin M
  • 5,436
  • 4
  • 44
  • 46
0

Add an extra 'y' onto all references of your datetime format strings. That includes this line:

var st = DateTime.Now.ToString("ddd MMM dd hh:mm:ss yyy");

and this line:

return DateTime.ParseExact(value, "ddd MMM dd hh:mm:ss yyy", culture, styles);

and also if/when you when you do a TryParse later on in the code.

Lane
  • 2,669
  • 3
  • 25
  • 38
  • You dont need it on the st variable as it will be converted to yyyy. You need it on ParseExact as the format string must exactly match the input string. – skyfoot Nov 17 '10 at 17:19