Check clock synchronization between DUT and packet tracer.

- use Linux 'date' command to get local time since epoch
- if clock skew >= 1 sec, we attempt to force tlsdate restart on both
  DUT and tracer (exactly once)
- if clock skew is still >= 1 sec after tlsdate restart, a TestError
  is raised

BUG=chromium:222447
TEST=Manual (modified network_WiFiSimpleConnectionServer/ and tested
both success and failure cases by manually manipulating system clocks
on DUT and tracer to verify correct behavior)

Change-Id: I2bc858830a5604157795ae1974331b9831027a22
Reviewed-on: https://gerrit.chromium.org/gerrit/46033
Commit-Queue: Tan Gao <tgao@chromium.org>
Reviewed-by: Tan Gao <tgao@chromium.org>
Tested-by: Tan Gao <tgao@chromium.org>
diff --git a/server/cros/time_util.py b/server/cros/time_util.py
new file mode 100644
index 0000000..e339637
--- /dev/null
+++ b/server/cros/time_util.py
@@ -0,0 +1,42 @@
+# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+from autotest_lib.client.common_lib import error
+
+
+def get_datetime_float(host):
+    """
+    Returns host's system time in seconds since epoch.
+
+    @param host: an Autotest host object.
+    @returns a float, timestamp since epoch in <seconds>.<nanoseconds>.
+
+    @raises TestError: if error reading datetime or converting its value
+                       from string to float.
+    """
+    r = host.run('date +%s.%N')
+    if r.exit_status > 0:
+        err = ('Error reading datetime from capturer (%r): %r' %
+               (r.exit_status, r.stderr))
+        raise error.TestError(err)
+    try:
+        return float(r.stdout)
+    except ValueError as e:
+        raise error.TestError('Error converting datetime string: %r', e)
+
+
+def force_tlsdate_restart(host):
+    """
+    Invokes 'tlsdate restart' command.
+
+    @param host: an Autotest host object.
+
+    @raises TestError: if command results in a non-zero return code.
+    """
+    r = host.run('tlsdate restart')
+    if r.exit_status > 0:
+        err = ('Error restarting tlsdated on tracer (%r): %r' %
+               (r.exit_status, r.stderr))
+        raise error.TestError(err)
+