I use the following approach that takes a time zone offset value e.g. GMT-3 and returns the list of time zoneId for the given offset.
private static List<String> getTimeZonesByZoneOffset(final int offset) {
return ZoneId.getAvailableZoneIds()
.stream()
.filter(zoneId -> ZoneId.of(zoneId)
.getRules()
.getOffset(Instant.now())
.equals(ZoneOffset.ofHours(offset)))
.sorted()
.collect(Collectors.toList());
}
Then I retrieve corresponding records that have the same zoneId as my zone list from my database. I use an approach something like this in my service:
public List<Product> getByOffset(int offset) {
final List<String> zones = getTimeZonesByZoneOffset(offset);
final List<Product> products = productRepository.findAllByZone(zones);
return getProductList(products);
}
I want to test these 2 methods in a Unit Test. But I am not sure how should I set the test mechanism. Is there any Unit Test example for that?