3
votes

I'm trying to store an ISO formatted DateTime in my Azure Table Storage. But every time I try, it results in this String format: https://i.stack.imgur.com/kcrlN.png

This is a summary of the effective code I'm running:

import jsons
from azure.functions import Out

def main(parameterTableOutputJSON: Out[str]):
    row = {'ReadOn': '2021-04-28T09:35:26.123456Z'}
    parameterTableOutputJSON.set(jsons.dumps(row))

Of course my real code also has the PartitionKey and RowKey columns, but I can't show those here.

How can I insert the DateTime in the Azure Table Storage like this: 2021-04-28T09:35:26.123456Z
Instead of this: 04/28/2021 09:35:26

The Azure Table Storage docs tell me it supports ISO formatted date times...

2
Please share the complete code where you're writing the entity into Table Storage.Gaurav Mantri
@GauravMantri I'm writing the entity into Table Storage by calling the set function. The JSON String that goes into this function is the payload that gets written to Table Storage.Stefan Rang

2 Answers

1
votes

Using the newest Azure Data Tables library you can use the Python datetime object directly within your entity and submit it for creation. Here is an example snippet:

from azure.data.tables import TableClient
from datetime import datetime

client = TableClient.from_connection_string(<my_conn_str>)
my_entity = {
    "PartitionKey": <my_pk>,
    "RowKey": <my_rk>,
    "ReadOn": datetime(2021, 4, 28, 9, 36, 26, 123456)
}
client.upsert_entity(my_entity)

Here is what the entity looks like in the Storage Explorer in the portal

Disclaimer: I work on the Azure SDK for Python team.

0
votes

I found the solution to my own problem.

The jsons.dumps method strips the milliseconds by default. This results in a datetime that's formatted like this: 2021-04-28T09:35:26Z
This format is not supported as a DateTime in Azure Table Storage.

The problem is solved by writing the last line of my code like this:

parameterTableOutputJSON.set(jsons.dumps(row, strip_microseconds=False))