get posix data from zoneinfo files
Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
package dev.mars3142.fhq.timezone_service.timezone.service;
|
||||
|
||||
public interface TimeZoneService {
|
||||
String getPosixTimeZone(String timezone);
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
package dev.mars3142.fhq.timezone_service.timezone.service.impl;
|
||||
|
||||
import dev.mars3142.fhq.timezone_service.timezone.service.TimeZoneService;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import lombok.val;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class TimeZoneServiceImpl implements TimeZoneService {
|
||||
|
||||
@Override
|
||||
public String getPosixTimeZone(String timezone) {
|
||||
val filename = Path.of("/usr/share/zoneinfo/" + timezone);
|
||||
try {
|
||||
val bytes = Files.readAllBytes(filename);
|
||||
val content = new String(bytes).split("\n");
|
||||
return content[content.length - 1];
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
@@ -2,6 +2,9 @@ package dev.mars3142.fhq.timezone_service.timezone.web.controllers;
|
||||
|
||||
import dev.mars3142.fhq.timezone_service.timezone.domain.model.request.TimeZoneRequest;
|
||||
import dev.mars3142.fhq.timezone_service.timezone.domain.model.response.TimeZoneResponse;
|
||||
import dev.mars3142.fhq.timezone_service.timezone.service.TimeZoneService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.val;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@@ -9,10 +12,14 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("v1/timezone")
|
||||
@RequiredArgsConstructor
|
||||
public class TimeZoneController {
|
||||
|
||||
private final TimeZoneService timeZoneService;
|
||||
|
||||
@PostMapping()
|
||||
public TimeZoneResponse getTimeZone(@RequestBody TimeZoneRequest request) {
|
||||
return new TimeZoneResponse(request.timezone());
|
||||
val timezone = timeZoneService.getPosixTimeZone(request.timezone());
|
||||
return new TimeZoneResponse(timezone);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user