Ejemplo n.º 1
0
 def _get_timestamp(self, time_overwrite=None):
     ts = Timestamp()
     if time_overwrite is not None:
         time = self._convert_element(time_overwrite)
         if not isinstance(time_overwrite, int):
             raise TypeError(
                 f'time_overwrite {time_overwrite} is type {type(time_overwrite)}, but expects int. (Unix epoch time in seconds)'
             )
         ts.FromSeconds(time)
     else:
         ts.GetCurrentTime()
     return ts
Ejemplo n.º 2
0
def proto_time(time):
    t = Timestamp()
    if not time:
        return
    elif isinstance(time, datetime):
        return t.FromDatetime(time)
    elif isinstance(time, int):
        return t.FromSeconds(time)
    elif isinstance(time, Timestamp):
        return time
    else:
        logger.error(
            f'{time} is not a valid timestamp. Please provide a datetime, '
            f'unix_timestamp, or google.protobuf.Timestamp format of '
            f'time.')
Ejemplo n.º 3
0
def to_pb_timestamp(v):
  """Try to convert value `v` to a Protobuf `Timestamp` instance."""
  if isinstance(v, Timestamp):
    return v
  elif isinstance(v, (list, tuple)) and len(v) == 2:
    return Timestamp(seconds=v[0], nanos=v[1])
  elif isinstance(v, datetime.datetime):
    ts = Timestamp()
    ts.FromDatetime(v)
    return ts
  elif isinstance(v, int):
    ts = Timestamp()
    ts.FromSeconds(v)
    return ts
  elif isinstance(v, float):
    sec = int(v)
    nsec = int((v - sec) * 1e9)
    ts = Timestamp(seconds=sec, nanos=nsec)
    return ts
  else:
    raise ValueError(
      "Don't know what to do with timestamp %s" % (v,))
Ejemplo n.º 4
0
 def FinGetAccount(self, request, context):
     account = fin.get_account(self, request.name, request.fullName,
                               request.accountType)
     ret = moniek_pb2.FinGetAccountResp(total=account['total'],
                                        mtime=account['mtime'])
     for accountName in account['accounts']:
         ret.accounts[accountName] = True  # this is a set, not a map
     for transaction in account['trs']:
         mutations = []
         for mutation in transaction['muts']:
             mutations.append(moniek_pb2.FinMutation(**mutation))
         timestamp = Timestamp()
         timestamp.FromSeconds(int(transaction['date']['timestamp']))
         ret.transactions.append(
             moniek_pb2.FinTransaction(
                 date=timestamp,
                 description=transaction['description'],
                 mutations=mutations,
                 num=transaction['num'],
                 sum=transaction['sum'],
                 value=transaction['value'],
             ))
     return ret
def seconds_to_timestamp(seconds):
    timestamp = Timestamp()
    timestamp.FromSeconds(seconds)
    return timestamp