0

I’m using GridDB Cloud (Free Tier) with the official Python client (latest pip release) on Ubuntu 22.04.

My container schema looks like this:

device_id STRING,
created_at TIMESTAMP,
temperature DOUBLE

I’m trying to insert rows with the current timestamp. My code:

from datetime import datetime

row = ["sensor_1", datetime.now(), 21.5]
container.put(row)

But this throws the following error:

griddb.TimeConversionError: Invalid format for TIMESTAMP

I also tried converting the datetime into ISO format:

row = ["sensor_1", datetime.now().isoformat(), 21.5]

What I tried & expected:

I expected datetime.now() to be accepted directly, since the column type is TIMESTAMP. I also tried using .isoformat() strings, but that didn’t work either. I checked the docs, but couldn’t find a clear example for inserting Python datetime values.

Question:

What’s the correct way to insert Python datetime objects into a TIMESTAMP column in GridDB Cloud? Do I need to convert it into a specific string format, or is there native support for datetime in the Python client?

1
  • I found a page from GridDB Cloud Quick Start Guide in which there's a sample of python code representing the timestamp: "2024-01-09T10:00:01.234Z". It might help. Commented Sep 25 at 16:12

1 Answer 1

0

From the GridDB Docs, a timestamp in milliseconds is required and they provide a tool for generating them (see below).

Datetimes and timestamps both store time information but in different ways. Time stamps are numeric only values typically based on the Unix epoch and UTC.

GridDb TimestampUtils

The below code snippet is a built in function with key documentation to understanding the issue.

class TimestampUtils
Bases: object

Provides the utilities for manipulating time data.

get_time_millis(timestamp)
Caluculate int-type timestamp in millisecond from datetime.timestamp()

Parameters: timestamp (float) – timestamp for Python
Returns:    int-type timestamp in millisecond
Return type:    int

I don't have GridDB set up, so I could not fully test, but I think something like the below would work with datetime.

import datetime
from griddb_python import TimestampUtils

utc_now = datetime.datetime.now(datetime.timezone.utc)
utc_ts = utc_now.timestamp()
millis = TimestampUtils.get_time_millis()

Python Solution with Time

In Python, I use time instead of datetime for this. Avoids confusion with timezones and datetime->timestamp conversion.

import time
millis = int(time.time() * 1000)

time.time() produces a float type timestamp, multiplying by 1000 converts to expected milliseconds, and int coverts to correct type and drops float tails.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.