time_parser.py
This module provides interfaces to parse and convert timestamp.
__all__ = ['TimeParser']
module-attribute
¶
InvalidTimeFormatException
¶
Bases: Exception
Exception for invalid time format.
Source code in solnlib/time_parser.py
31 32 33 34 |
|
TimeParser
¶
Datetime parser.
Use splunkd rest to parse datetime.
Examples:
>>> from solnlib import time_parser
>>> tp = time_parser.TimeParser(session_key)
>>> tp.to_seconds('2011-07-06T21:54:23.000-07:00')
>>> tp.to_utc('2011-07-06T21:54:23.000-07:00')
>>> tp.to_local('2011-07-06T21:54:23.000-07:00')
Source code in solnlib/time_parser.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
URL = '/services/search/timeparser'
class-attribute
instance-attribute
¶
__init__(session_key, scheme=None, host=None, port=None, **context)
¶
Initializes TimeParser.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session_key |
str
|
Splunk access token. |
required |
scheme |
str
|
(optional) The access scheme, default is None. |
None
|
host |
str
|
(optional) The host name, default is None. |
None
|
port |
int
|
(optional) The port number, default is None. |
None
|
context |
Any
|
Other configurations for Splunk rest client. |
{}
|
Raises:
Type | Description |
---|---|
ValueError
|
if scheme, host or port are invalid. |
Source code in solnlib/time_parser.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|
to_local(time_str)
¶
Parse time_str
and convert to local timestamp.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
time_str |
str
|
ISO8601 format timestamp, example: 2011-07-06T21:54:23.000-07:00. |
required |
Raises:
Type | Description |
---|---|
binding.HTTPError
|
rest client returns an exception (everything else than 400 code). |
InvalidTimeFormatException
|
when time format is invalid (rest client returns 400 code). |
Returns:
Type | Description |
---|---|
str
|
Local timestamp in ISO8601 format. |
Source code in solnlib/time_parser.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
to_seconds(time_str)
¶
Parse time_str
and convert to seconds since epoch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
time_str |
str
|
ISO8601 format timestamp, example: 2011-07-06T21:54:23.000-07:00. |
required |
Raises:
Type | Description |
---|---|
binding.HTTPError
|
rest client returns an exception (everything else than 400 code). |
InvalidTimeFormatException
|
when time format is invalid (rest client returns 400 code). |
Returns:
Type | Description |
---|---|
float
|
Seconds since epoch. |
Source code in solnlib/time_parser.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|
to_utc(time_str)
¶
Parse time_str
and convert to UTC timestamp.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
time_str |
str
|
ISO8601 format timestamp, example: 2011-07-06T21:54:23.000-07:00. |
required |
Raises:
Type | Description |
---|---|
binding.HTTPError
|
rest client returns an exception (everything else than 400 code). |
InvalidTimeFormatException
|
when time format is invalid (rest client returns 400 code). |
Returns:
Type | Description |
---|---|
datetime.datetime
|
UTC timestamp. |
Source code in solnlib/time_parser.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|