Issues (227)

awips/DateTimeConverter.py (2 issues)

1
#
2
# Functions for converting between the various "Java" dynamic serialize types
3
# used by EDEX to the native python time datetime.
4
#
5
#
6
#     SOFTWARE HISTORY
7
#
8
#    Date            Ticket#       Engineer       Description
9
#    ------------    ----------    -----------    --------------------------
10
#    06/24/15         #4480        dgilling       Initial Creation.
11
#
12
13
import datetime
14
import time
15
16
from dynamicserialize.dstypes.java.util import Date
17
from dynamicserialize.dstypes.java.sql import Timestamp
18
from dynamicserialize.dstypes.com.raytheon.uf.common.time import TimeRange
19
20
MAX_TIME = pow(2, 31) - 1
21
MICROS_IN_SECOND = 1000000
22
23
24 View Code Duplication
def convertToDateTime(timeArg):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
25
    """
26
    Converts the given object to a python datetime object. Supports native
27
    python representations like datetime and struct_time, but also
28
    the dynamicserialize types like Date and Timestamp. Raises TypeError
29
    if no conversion can be performed.
30
31
    Args:
32
            timeArg: a python object representing a date and time. Supported
33
            types include datetime, struct_time, float, int, long and the
34
            dynamicserialize types Date and Timestamp.
35
36
    Returns:
37
        A datetime that represents the same date/time as the passed in object.
38
    """
39
    if isinstance(timeArg, datetime.datetime):
40
        return timeArg
41
    elif isinstance(timeArg, time.struct_time):
42
        return datetime.datetime(*timeArg[:6])
43
    elif isinstance(timeArg, float):
44
        # seconds as float, should be avoided due to floating point errors
45
        totalSecs = int(timeArg)
46
        micros = int((timeArg - totalSecs) * MICROS_IN_SECOND)
47
        return _convertSecsAndMicros(totalSecs, micros)
48
    elif isinstance(timeArg, int):
49
        # seconds as integer
50
        totalSecs = timeArg
51
        return _convertSecsAndMicros(totalSecs, 0)
52
    elif isinstance(timeArg, (Date, Timestamp)):
53
        totalSecs = timeArg.getTime()
54
        return _convertSecsAndMicros(totalSecs, 0)
55
    else:
56
        objType = str(type(timeArg))
57
        raise TypeError("Cannot convert object of type " + objType + " to datetime.")
58
59
60
def _convertSecsAndMicros(seconds, micros):
61
    if seconds < MAX_TIME:
62
        rval = datetime.datetime.utcfromtimestamp(seconds)
63
    else:
64
        extraTime = datetime.timedelta(seconds=(seconds - MAX_TIME))
65
        rval = datetime.datetime.utcfromtimestamp(MAX_TIME) + extraTime
66
    return rval.replace(microsecond=micros)
67
68
69 View Code Duplication
def constructTimeRange(*args):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
70
    """
71
    Builds a python dynamicserialize TimeRange object from the given
72
    arguments.
73
74
    Args:
75
            args*: must be a TimeRange or a pair of objects that can be
76
                   converted to a datetime via convertToDateTime().
77
78
    Returns:
79
        A TimeRange.
80
    """
81
82
    if len(args) == 1 and isinstance(args[0], TimeRange):
83
        return args[0]
84
    if len(args) != 2:
85
        raise TypeError("constructTimeRange takes exactly 2 arguments, " + str(len(args)) + " provided.")
86
    startTime = convertToDateTime(args[0])
87
    endTime = convertToDateTime(args[1])
88
    return TimeRange(startTime, endTime)
89