Skip to content

Commit

Permalink
refactor: Update datetime generator to handle patterns with zoned IDs #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Jun 7, 2020
1 parent d0a3051 commit a20e8d4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.apache.commons.lang3.RandomStringUtils
import org.apache.commons.lang3.RandomUtils
import java.math.BigDecimal
import java.time.OffsetDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.UUID
import java.util.concurrent.ThreadLocalRandom
Expand Down Expand Up @@ -322,7 +323,7 @@ data class DateTimeGenerator @JvmOverloads constructor(
else OffsetDateTime.now()
val datetime = DateTimeExpression.executeExpression(base, expression).getOr { base }
return if (!format.isNullOrEmpty()) {
datetime.format(DateTimeFormatter.ofPattern(format))
datetime.toZonedDateTime().format(DateTimeFormatter.ofPattern(format).withZone(ZoneId.systemDefault()))
} else {
datetime.toString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@ class DateTimeGeneratorSpec extends Specification {
expect:
DateTimeGenerator.@Companion.fromJson(json).generate([baseDateTime: baseDateTime]) == baseDateTime.toString()
}

def 'supports timezones with zone IDs'() {
expect:
new DateTimeGenerator("yyyy-MM-dd'T'HH:mm:ssZ'['VV']'", null).generate([:]) ==~
/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[-+]\d+\[\w+\/\w+]/
}
}

0 comments on commit a20e8d4

Please sign in to comment.