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

Unix epoch string support #458

Open
wants to merge 21 commits into
base: master
Choose a base branch
from

Conversation

robin-parker
Copy link
Contributor

@robin-parker robin-parker commented Sep 28, 2021

  • date: adds support for UNIX Epoch timestamps as strings - resolves UNIX Epoch timestamp in strings ignored by Date filter. #457.
  • unix_ms: new extended filter, which converts a UNIX Epoch timestamp in milliseconds to a DateTimeOffset.
  • time_zone: new extended filter, which converts a DateTimeOffset or ISO-8601 date-string to a DateTimeOffset in a target timezone.
    • Throws a SyntaxException for an unknown timezone identifier
    • Only Windows time zone identifiers are currently supported (IANA timezone identifiers could be supported later with a move to .NET6)

Documentation

@codecov
Copy link

codecov bot commented Sep 28, 2021

Codecov Report

Merging #458 (91cad73) into master (9a194d3) will increase coverage by 0.11%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #458      +/-   ##
==========================================
+ Coverage   87.27%   87.39%   +0.11%     
==========================================
  Files          67       67              
  Lines        2632     2657      +25     
  Branches      633      642       +9     
==========================================
+ Hits         2297     2322      +25     
  Misses        212      212              
  Partials      123      123              
Impacted Files Coverage Δ
src/DotLiquid/ExtendedFilters.cs 100.00% <100.00%> (ø)
src/DotLiquid/StandardFilters.cs 93.71% <100.00%> (+0.10%) ⬆️
src/DotLiquid/Context.cs 94.85% <0.00%> (+0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9a194d3...91cad73. Read the comment docs.

src/DotLiquid/StandardFilters.cs Show resolved Hide resolved
src/DotLiquid/StandardFilters.cs Outdated Show resolved Hide resolved
src/DotLiquid/StandardFilters.cs Outdated Show resolved Hide resolved
@microalps
Copy link
Contributor

microalps commented Oct 4, 2021

Personally I think this needs to be broken down to three 'extended' filters:

  1. unix_ms - accepts numeric or string with numeric value - converts milliseconds to datetimeoffset
  2. unix_sec - accepts numeric or string with numeric value - converts seconds to datetimeoffset
  3. zone - accepts datetime (marked utc) , datetime (unspecified or local), date string, or datetimeoffset and converts to datetimeoffset in the specified zone.
    a. numerics can be processed with unix_ms | zone: '[zone]' or unix_sec | zone '[zone]'
    b. now and today can be processed with date | zone: '[zone]'
    c. formatting to a human readable string would chain zone: '[zone]' | date: 'yyyy/MM/dd'

The test cases would be greatly limited and the functionality understandable. Happy to debate naming of these filters. Also, date itself can depend on unix_sec to process numerics.

@microalps
Copy link
Contributor

@robin-parker please advise if you intend to implement my suggestion. I can't approve as is.

Robin Parker added 2 commits March 1, 2022 17:31
* unix_ms - convert epoch to DateTimeOffset
* time_zone - convert date-time to another timezone
@robin-parker robin-parker marked this pull request as draft March 2, 2022 11:28
Copy link
Contributor

@microalps microalps left a comment

Choose a reason for hiding this comment

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

Looks pretty good to me. Local testing is an issue though.

{
// Convert the input to a DateTimeOffset
if (input is DateTimeOffset dateTimeOffset ||
(input is string stringInput && DateTimeOffset.TryParse(stringInput, out dateTimeOffset)))
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't agree with parsing in here, created extra overhead/testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@microalps, the date filter returns a string, so without string parsing time_zone cannot be chained after a date filter, which limits it's usability.
How about if I changed it to use TryParseExact with a fixed format of say ISO-8601 with explicit timezone specifier.
That would reduce the overhead and simplify testing to a narrower set of input formats.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good idea. I'd accept it if it was limited in scope as you describe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

src/DotLiquid/ExtendedFilters.cs Outdated Show resolved Hide resolved
src/DotLiquid/StandardFilters.cs Show resolved Hide resolved
src/DotLiquid.Tests/ExtendedFilterTests.cs Outdated Show resolved Hide resolved
@robin-parker robin-parker marked this pull request as ready for review March 29, 2022 17:02
src/DotLiquid/ExtendedFilters.cs Outdated Show resolved Hide resolved
@@ -74,42 +74,30 @@ public void TestTimeZone()
{
Context context = new Context(CultureInfo.CurrentCulture);

// Test UTC specified DateTime
Copy link
Contributor

Choose a reason for hiding this comment

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

These tests between the type should result in a different value. On a machine NOT in UTC, converting a datetime that is EXPLICITLY local to UTC would change the hour, but a datetime that is EXPLICITLY UTC would not. This functionality is missing I guess.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@microalps, I'm not clear what functionality is missing. Could you advise/write a test case for the scenario you're thinking of?

Copy link
Contributor

Choose a reason for hiding this comment

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

Say you have a hash that includes a date - from DateTime.Now - and the local machine is not in UTC. Calling TimeZone with this parameter and 'UTC' as the destination should change the time. HOWEVER, the same with DateTime.UtcNow should not as it is already in UTC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UNIX Epoch timestamp in strings ignored by Date filter.
2 participants