Statistics
| Branch: | Tag: | Revision:

root / snf-common / synnefo / lib / utils.py @ 5f6ad491

History | View | Annotate | Download (2.5 kB)

1 c4e55622 Christos Stavrakakis
# Copyright 2012 GRNET S.A. All rights reserved.
2 c4e55622 Christos Stavrakakis
#
3 c4e55622 Christos Stavrakakis
# Redistribution and use in source and binary forms, with or
4 c4e55622 Christos Stavrakakis
# without modification, are permitted provided that the following
5 c4e55622 Christos Stavrakakis
# conditions are met:
6 c4e55622 Christos Stavrakakis
#
7 c4e55622 Christos Stavrakakis
#   1. Redistributions of source code must retain the above
8 c4e55622 Christos Stavrakakis
#      copyright notice, this list of conditions and the following
9 c4e55622 Christos Stavrakakis
#      disclaimer.
10 c4e55622 Christos Stavrakakis
#
11 c4e55622 Christos Stavrakakis
#   2. Redistributions in binary form must reproduce the above
12 c4e55622 Christos Stavrakakis
#      copyright notice, this list of conditions and the following
13 c4e55622 Christos Stavrakakis
#      disclaimer in the documentation and/or other materials
14 c4e55622 Christos Stavrakakis
#      provided with the distribution.
15 c4e55622 Christos Stavrakakis
#
16 c4e55622 Christos Stavrakakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 c4e55622 Christos Stavrakakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 c4e55622 Christos Stavrakakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 c4e55622 Christos Stavrakakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 c4e55622 Christos Stavrakakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 c4e55622 Christos Stavrakakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 c4e55622 Christos Stavrakakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 c4e55622 Christos Stavrakakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 c4e55622 Christos Stavrakakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 c4e55622 Christos Stavrakakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 c4e55622 Christos Stavrakakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 c4e55622 Christos Stavrakakis
# POSSIBILITY OF SUCH DAMAGE.
28 c4e55622 Christos Stavrakakis
#
29 c4e55622 Christos Stavrakakis
# The views and conclusions contained in the software and
30 c4e55622 Christos Stavrakakis
# documentation are those of the authors and should not be
31 c4e55622 Christos Stavrakakis
# interpreted as representing official policies, either expressed
32 c4e55622 Christos Stavrakakis
# or implied, of GRNET S.A.
33 c4e55622 Christos Stavrakakis
34 c4e55622 Christos Stavrakakis
import datetime
35 c4e55622 Christos Stavrakakis
36 c4e55622 Christos Stavrakakis
def split_time(value):
37 c4e55622 Christos Stavrakakis
  """Splits time as floating point number into a tuple.
38 c4e55622 Christos Stavrakakis

39 c4e55622 Christos Stavrakakis
  @param value: Time in seconds
40 c4e55622 Christos Stavrakakis
  @type value: int or float
41 c4e55622 Christos Stavrakakis
  @return: Tuple containing (seconds, microseconds)
42 c4e55622 Christos Stavrakakis

43 c4e55622 Christos Stavrakakis
  """
44 c4e55622 Christos Stavrakakis
  (seconds, microseconds) = divmod(int(value * 1000000), 1000000)
45 c4e55622 Christos Stavrakakis
46 c4e55622 Christos Stavrakakis
  assert 0 <= seconds, \
47 c4e55622 Christos Stavrakakis
    "Seconds must be larger than or equal to 0, but are %s" % seconds
48 c4e55622 Christos Stavrakakis
  assert 0 <= microseconds <= 999999, \
49 c4e55622 Christos Stavrakakis
    "Microseconds must be 0-999999, but are %s" % microseconds
50 c4e55622 Christos Stavrakakis
51 c4e55622 Christos Stavrakakis
  return (int(seconds), int(microseconds))
52 c4e55622 Christos Stavrakakis
53 c4e55622 Christos Stavrakakis
54 c4e55622 Christos Stavrakakis
def merge_time(timetuple):
55 c4e55622 Christos Stavrakakis
  """Merges a tuple into a datetime object
56 c4e55622 Christos Stavrakakis

57 c4e55622 Christos Stavrakakis
  @param timetuple: Time as tuple, (seconds, microseconds)
58 c4e55622 Christos Stavrakakis
  @type timetuple: tuple
59 c4e55622 Christos Stavrakakis
  @return: Time as a datetime object
60 c4e55622 Christos Stavrakakis

61 c4e55622 Christos Stavrakakis
  """
62 c4e55622 Christos Stavrakakis
  (seconds, microseconds) = timetuple
63 c4e55622 Christos Stavrakakis
64 c4e55622 Christos Stavrakakis
  assert 0 <= seconds, \
65 c4e55622 Christos Stavrakakis
    "Seconds must be larger than or equal to 0, but are %s" % seconds
66 c4e55622 Christos Stavrakakis
  assert 0 <= microseconds <= 999999, \
67 c4e55622 Christos Stavrakakis
    "Microseconds must be 0-999999, but are %s" % microseconds
68 c4e55622 Christos Stavrakakis
69 c4e55622 Christos Stavrakakis
  t1 = float(seconds) + (float(microseconds) * 0.000001)
70 c4e55622 Christos Stavrakakis
  return datetime.datetime.fromtimestamp(t1)