I am using the C# Object Persistence model in my application, and am populating the table from an external source. I insert a row with the following date (in a String column): 2018-12-12T22:27:14.73Z
. This is generated from a Timestamp using the following Go code:
&dynamodb.AttributeValue{S: aws.String(entity.TimeStamp.UTC().Format("2006-01-02T15:04:05.999Z"))}
However, the DynamoDB Object Persistence model chokes when trying to convert it to a System.DateTime
, with the following error: System.InvalidOperationException: Unable to convert [2018-12-12T22:27:14.73Z] of type Amazon.DynamoDBv2.DocumentModel.Primitive to System.DateTime
If I let my service write a System.DateTime (using a POCO that contains a DateTime property), it looks something like this: 2018-12-19T07:45:36.431Z
. What am I missing that prevents AWS from properly deserializing my dates? It looks like I'm writing them in the same format that Amazon is writing them?
Ok, I found the answer. DynamoDB expects the column to always have 3 digits of precision in the fractional part of the timestamp. When I formatted the time from Golang, I used "2006-01-02T15:04:05.999Z"
as my format string, but this causes time.Format
to truncate the trailing zeros (when they exist). Changing my time format string to always print the full precision, I was able to fix my issues.
That said, the documentation could be a little more clear about this precision requirement. Or the exception could be a little more explicit. Heres hoping this question/answer makes that exception searchable on Google!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With