###############################################################################
##
##  Copyright 2011 Tavendo GmbH
##
##  Licensed under the Apache License, Version 2.0 (the "License");
##  you may not use this file except in compliance with the License.
##  You may obtain a copy of the License at
##
##      http://www.apache.org/licenses/LICENSE-2.0
##
##  Unless required by applicable law or agreed to in writing, software
##  distributed under the License is distributed on an "AS IS" BASIS,
##  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
##  See the License for the specific language governing permissions and
##  limitations under the License.
##
###############################################################################

import datetime
import time
import random

UTC_TIMESTAMP_FORMAT = "%Y-%m-%dT%H:%M:%SZ"


def utcnow():
   """
   Get current time in UTC as ISO 8601 string.
   """
   now = datetime.datetime.utcnow()
   return now.strftime(UTC_TIMESTAMP_FORMAT)


def parseutc(s):
   """
   Parse an ISO 8601 combined date and time string, like i.e. 2011-11-23T12:23Z
   into a UTC datetime instance.
   """
   try:
      return datetime.datetime.strptime(s, UTC_TIMESTAMP_FORMAT)
   except:
      return None


def utcstr(dt):
   """
   Convert an UTC datetime instance into an ISO 8601 combined date and time,
   like i.e. 2011-11-23T12:23Z
   """
   try:
      return dt.strftime(UTC_TIMESTAMP_FORMAT)
   except:
      return None


def newid():
   """
   Generate a new random object ID.
   """
   return ''.join([random.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_") for i in xrange(16)])

@@ -800,16 +800,27 @@
 t random
+%0Aimport sys
 %0A%0AUTC_TI
@@ -1675,8 +1675,2531 @@
 e(16)%5D)%0A
+%0A%0A%0A## Select the most precise walltime measurement function available%0A## on the platform%0A##%0Aif sys.platform.startswith('win'):%0A   ## On Windows, this function returns wall-clock seconds elapsed since the%0A   ## first call to this function, as a floating point number, based on the%0A   ## Win32 function QueryPerformanceCounter(). The resolution is typically%0A   ## better than one microsecond%0A   rtime = time.clock%0A   _ = rtime()%0Aelse:%0A   ## On Unix-like platforms, this used the first available from this list:%0A   ## (1) gettimeofday() -- resolution in microseconds%0A   ## (2) ftime() -- resolution in milliseconds%0A   ## (3) time() -- resolution in seconds%0A   rtime = time.time%0A%0A%0Aclass Stopwatch:%0A   %22%22%22%0A   Stopwatch based on walltime. Can be used to do code timing and uses the%0A   most precise walltime measurement available on the platform. This is%0A   a very light-weight object, so create/dispose is very cheap.%0A   %22%22%22%0A%0A   def __init__(self, start = True):%0A      %22%22%22%0A      Creates a new stopwatch and by default immediately starts (= resumes) it.%0A      %22%22%22%0A      self._elapsed = 0%0A      if start:%0A         self._started = rtime()%0A         self._running = True%0A      else:%0A         self._started = None%0A         self._running = False%0A%0A   def elapsed(self):%0A      %22%22%22%0A      Return total time elapsed in seconds during which the stopwatch was running.%0A      %22%22%22%0A      if self._running:%0A         now = rtime()%0A         return self._elapsed + (now - self._started)%0A      else:%0A         return self._elapsed%0A%0A   def pause(self):%0A      %22%22%22%0A      Pauses the stopwatch and returns total time elapsed in seconds during which%0A      the stopwatch was running.%0A      %22%22%22%0A      if self._running:%0A         now = rtime()%0A         self._elapsed += now - self._started%0A         self._running = False%0A         return self._elapsed%0A      else:%0A         return self._elapsed%0A%0A   def resume(self):%0A      %22%22%22%0A      Resumes a paused stopwatch and returns total elapsed time in seconds%0A      during which the stopwatch was running.%0A      %22%22%22%0A      if not self._running:%0A         self._started = rtime()%0A         self._running = True%0A         return self._elapsed%0A      else:%0A         now = rtime()%0A         return self._elapsed + (now - self._started)%0A%0A   def stop(self):%0A      %22%22%22%0A      Stops the stopwatch and returns total time elapsed in seconds during which%0A      the stopwatch was (previously) running.%0A      %22%22%22%0A      elapsed = self.pause()%0A      self._elapsed = 0%0A      self._started = None%0A      self._running = False%0A      return elapsed%0A

add stopwatch