Skip to content

Commit

Permalink
fix tz as discussed at #51
Browse files Browse the repository at this point in the history
  • Loading branch information
maxandersen committed Feb 28, 2020
1 parent 450c5c1 commit 84d2dd2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 25 deletions.
10 changes: 1 addition & 9 deletions examples/tz.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,13 @@ include::tz.java[tags=parameters,indent=0]
----
<.> Time is a timestamp and picocli will automatically convert it to a `LocalTime`.
<.> For the timezones we need a Custom converter to ensure more short hand ids will work.

<.> Default values for a list of strings are possible using `split=","`
[source,java]
----
include::tz.java[tags=zoneconverter,indent=0]
----
<.> `SHORT_IDS` here adds more mapping for common timezone shorthand ids.

Picocli don't have a good way of defining default values for a list thus we 'cheat' by settig values if the list is empty indicating nothing was passed in by the user.

[source,java]
----
include::tz.java[tags=defaultzones,indent=0]
----
<.> Mapping of a array of strings to ZoneId list using collection stream API.

At the end the final conversion is done and printed out.

[source,java]
Expand Down
23 changes: 7 additions & 16 deletions examples/tz.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS info.picocli:picocli:4.2.0 info.picocli:picocli-codegen:4.2.0
//DEPS info.picocli:picocli:4.2.0

import picocli.CommandLine;
import picocli.CommandLine.Command;
Expand Down Expand Up @@ -27,8 +27,12 @@ class tz implements Callable<Integer> {
@Parameters(index = "0", description = "time as hh:mm")
private LocalTime time; // <.>

@Parameters(converter = ZoneIdConverter.class, // <.>
description = "List of timezone ids to convert time stamp to")
@Parameters(index = "1..*", split = ",",
converter = ZoneIdConverter.class, // <.>
defaultValue = "America/Los_Angeles,America/Detroit," + // <.>
"Europe/London,Europe/Zurich,Asia/Kolkata,Australia/Brisbane",
description = "Time zones. Defaults: ${DEFAULT-VALUE}.",
hideParamSyntax = true, paramLabel = "[<zones>[,<zones>...]...]")
List<ZoneId> zones;
// end::parameters[]

Expand All @@ -42,19 +46,6 @@ public Integer call() throws Exception { // your business logic goes here...

var t = LocalDateTime.now().with(time);

// tag::defaultzones[]
if(zones.isEmpty()) {
zones = Arrays.asList("America/Los_Angeles",
"America/Detroit",
"Europe/London",
"Europe/Zurich",
"Asia/Kolkata",
"Australia/Brisbane").stream()
.map(tz -> ZoneId.of(tz, ZoneId.SHORT_IDS))
.collect(Collectors.toList()); // <.>
}
// end::defaultzones[]

// tag::conversion[]
String result = zones.stream().map(zone -> {
var from = t.atZone(ZoneId.systemDefault());
Expand Down

0 comments on commit 84d2dd2

Please sign in to comment.