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

[plugin-datetime] Support timezone in 'toEpochSecond' expression #1219

Merged
merged 1 commit into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/modules/plugins/pages/plugin-datetime.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,31 @@ TIP: Since parameters of the expression comma-separated, meaningful commas (like
|PT8774591H59M20S
|===

=== `toEpochSecond`

Converts the input date to https://en.wikipedia.org/wiki/Unix_time[the unix epoch time] (the number of seconds from the epoch of 1970-01-01T00:00:00Z).

[source, subs="+quotes"]
----
#{toEpochSecond(*$inputDate*)}
----

* *`$inputDate`* - the date to be formatted, it should be presented in the {iso-date-format-link} format

[cols="2,>1", options="header"]
.Examples of the expressions converting the input date to the unix epoch time
|===
|Expression
|Result

|`#{toEpochSecond(1993-04-16T00:00:00)}`
|734918400

|`#{toEpochSecond(2020-12-11T18:43:05+05:30)}`
|1607692385

|===


== Steps

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public EpochExpressionProcessors(DateUtils dateUtils)
{
super(List.of(
new UnaryExpressionProcessor("toEpochSecond",
arg -> String.valueOf(dateUtils.toEpochSecond(arg, DateTimeFormatter.ISO_DATE_TIME))),
arg -> String.valueOf(dateUtils.parseDateTime(arg, DateTimeFormatter.ISO_DATE_TIME).toEpochSecond())),
new UnaryExpressionProcessor("fromEpochSecond",
arg -> DateTimeFormatter.ISO_DATE_TIME.format(dateUtils.fromEpochSecond(parseLong(arg))))
));
Copy link
Member

Choose a reason for hiding this comment

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

don't you mind to add docs for these expressions?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

that will be added in another PR

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,6 @@ public ZonedDateTime parseDateTime(String dateTime, DateTimeFormatter formatter)
return zonedDateTime;
}

/**
* Converts the <b>dateTime</b> to the number of seconds from the epoch
* of 1970-01-01T00:00:00Z.
* @param dateTime A date-time text string
* @param formatter A formatter for parsing date-time strings
* @return the number of seconds from the epoch of 1970-01-01T00:00:00Z
*/
public long toEpochSecond(String dateTime, DateTimeFormatter formatter)
{
return parseDateTime(dateTime, formatter).toLocalDateTime().toEpochSecond(getZoneOffset());
}

/**
* Obtains an instance of {@code LocalDateTime} using seconds from the
* epoch of 1970-01-01T00:00:00Z.
Expand All @@ -99,11 +87,7 @@ public long toEpochSecond(String dateTime, DateTimeFormatter formatter)
*/
public LocalDateTime fromEpochSecond(long seconds)
{
return LocalDateTime.ofEpochSecond(seconds, 0, getZoneOffset());
}

private ZoneOffset getZoneOffset()
{
return zoneId.getRules().getOffset(Instant.now());
ZoneOffset zoneOffset = zoneId.getRules().getOffset(Instant.now());
return LocalDateTime.ofEpochSecond(seconds, 0, zoneOffset);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.vividus.util.DateUtils;

class EpochExpressionProcessorsTests
Expand All @@ -31,10 +33,14 @@ class EpochExpressionProcessorsTests

private final EpochExpressionProcessors processor = new EpochExpressionProcessors(new DateUtils(ZoneId.of("UTC")));

@Test
void testExecuteMatchingExpressionToEpoch()
@ParameterizedTest
@CsvSource({
ISO_DATE_TIME + ", " + EPOCH,
"2020-12-11T18:43:05+05:30, 1607692385"
})
void testExecuteMatchingExpressionToEpoch(String date, String expectedEpoch)
{
assertEquals(Optional.of(EPOCH), processor.execute(String.format("toEpochSecond(%s)", ISO_DATE_TIME)));
assertEquals(Optional.of(expectedEpoch), processor.execute(String.format("toEpochSecond(%s)", date)));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ When I initialize the SCENARIO variable `date` with value `#{generateDate(P, yyy
When I initialize the SCENARIO variable `epoch` with value `#{toEpochSecond(${date})}`
Then `${date}` is equal to `#{fromEpochSecond(${epoch})}`

Scenario: Verify epoch generation with timezone
When I initialize the SCENARIO variable `epoch` with value `#{toEpochSecond(2020-12-11T18:43:05+05:30)}`
Then `${epoch}` is equal to `1607692385`

Scenario: Verify anyOf expression
Then `#{anyOf(1, 2\,3,3)}` matches `1|2,3|3`

Expand Down