Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle when Lambda events are sent unix epoch dates in milliseconds #1710

Merged
merged 5 commits into from
Apr 4, 2024

Conversation

normj
Copy link
Member

@normj normj commented Mar 29, 2024

Issue #, if available:
#839

Description of changes:
When Kinesis streams are used to handle DynamoDB events they send the approximate date a unix epoch in milliseconds. When coming from a DynamoDB stream event they are seconds which is the normal format.

Since there isn't a way to detect the source of the event the serializers are updated to look to see if the long in the event would be greater then the number of seconds for the year 5000. If so then assume the value is in milliseconds. The other alternative I could have done is try to create the DateTime with the millisecond epoch and if I got an out of range exception fallback to creating the DateTime from milliseconds but I wanted to avoid control flow by exceptions.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@normj normj changed the base branch from master to dev March 29, 2024 23:44
@normj normj changed the base branch from dev to master March 29, 2024 23:45
@normj normj requested review from ashishdhingra and ashovlin March 29, 2024 23:48
@@ -12,6 +12,7 @@ namespace Amazon.Lambda.Serialization.Json
/// </summary>
internal class JsonNumberToDateTimeDataConverter : JsonConverter
{
private const long YEAR_5000_IN_SECONDS = 157753180800;
Copy link
Member

@ashovlin ashovlin Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got a little confused when checking the math. At first I assumed this was seconds-since-epoch, but appears to be since year 1. https://dotnetfiddle.net/0VesSV

Not a big deal functionally though, same principle applies. 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment to make it clearer.

@normj normj changed the base branch from master to dev April 4, 2024 20:33
@normj normj merged commit b65a233 into dev Apr 4, 2024
4 checks passed
@normj normj deleted the normj/handle-milli-epochs branch April 4, 2024 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants