OmniSciDB  a7179b2938
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
_utils.py
Go to the documentation of this file.
1 import datetime
2 import numpy as np
3 
4 
5 def seconds_to_time(seconds):
6  """Convert seconds since midnight to a datetime.time"""
7  m, s = divmod(seconds, 60)
8  h, m = divmod(m, 60)
9  return datetime.time(h, m, s)
10 
11 
12 def time_to_seconds(time):
13  """Convert a datetime.time to seconds since midnight"""
14  if time is None:
15  return None
16  return 3600 * time.hour + 60 * time.minute + time.second
17 
18 
19 def datetime_in_precisions(epoch, precision):
20  """Convert epoch time value into s, ms, us, ns"""
21  base = datetime.datetime(1970, 1, 1)
22  if precision == 0:
23  return base + datetime.timedelta(seconds=epoch)
24  elif precision == 3:
25  seconds, modulus = divmod(epoch, 1000)
26  return base + datetime.timedelta(seconds=seconds, milliseconds=modulus)
27  elif precision == 6:
28  seconds, modulus = divmod(epoch, 1000000)
29  return base + datetime.timedelta(seconds=seconds, microseconds=modulus)
30  elif precision == 9:
31  return np.datetime64(epoch, 'ns')
32  else:
33  raise TypeError("Invalid timestamp precision: {}".format(precision))
34 
35 
36 def date_to_seconds(arr):
37  """Converts date into seconds"""
38 
39  return arr.apply(lambda x: np.datetime64(x, "s").astype(int))
40 
41 
42 mapd_to_slot = {
43  'BOOL': 'int_col',
44  'BOOLEAN': 'int_col',
45  'SMALLINT': 'int_col',
46  'INT': 'int_col',
47  'INTEGER': 'int_col',
48  'BIGINT': 'int_col',
49  'FLOAT': 'real_col',
50  'DECIMAL': 'int_col',
51  'DOUBLE': 'real_col',
52  'TIMESTAMP': 'int_col',
53  'DATE': 'int_col',
54  'TIME': 'int_col',
55  'STR': 'str_col',
56  'POINT': 'str_col',
57  'LINESTRING': 'str_col',
58  'POLYGON': 'str_col',
59  'MULTIPOLYGON': 'str_col',
60  'TINYINT': 'int_col',
61  'GEOMETRY': 'str_col',
62  'GEOGRAPHY': 'str_col',
63 }
64 
65 
66 mapd_to_na = {
67  'BOOL': -128,
68  'BOOLEAN': -128,
69  'SMALLINT': -32768,
70  'INT': -2147483648,
71  'INTEGER': -2147483648,
72  'BIGINT': -9223372036854775808,
73  'FLOAT': 0,
74  'DECIMAL': 0,
75  'DOUBLE': 0,
76  'TIMESTAMP': -9223372036854775808,
77  'DATE': -9223372036854775808,
78  'TIME': -9223372036854775808,
79  'STR': '',
80  'POINT': '',
81  'LINESTRING': '',
82  'POLYGON': '',
83  'MULTIPOLYGON': '',
84  'TINYINT': -128,
85  'GEOMETRY': '',
86  'GEOGRAPHY': '',
87 }
def datetime_in_precisions
Definition: _utils.py:19
def time_to_seconds
Definition: _utils.py:12
def date_to_seconds
Definition: _utils.py:36
def seconds_to_time
Definition: _utils.py:5