some refactor
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
package ru.ldeloff.aitools.datetime.controller;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import reactor.core.publisher.Mono;
|
||||
import ru.ldeloff.aitools.datetime.dto.TimeResponse;
|
||||
@@ -17,18 +19,27 @@ import java.time.format.DateTimeFormatter;
|
||||
@RestController
|
||||
public class TimeController {
|
||||
|
||||
@GetMapping("/time/moscow")
|
||||
@Operation(summary = "Get current Moscow time", description = "Returns current date and time in Moscow timezone (Europe/Moscow) as ISO 8601 string. Used as a tool for LLMs to get accurate local time.")
|
||||
@GetMapping("/time")
|
||||
@Operation(summary = "Get current time in specified timezone", description = "Returns current date and time in the given timezone. Default: Europe/Moscow. Use IANA timezone IDs (e.g., America/New_York, Asia/Tokyo).")
|
||||
@ApiResponses({
|
||||
@ApiResponse(responseCode = "200", description = "Current time in Moscow in format yyyy-MM-dd HH:mm:ss"),
|
||||
@ApiResponse(responseCode = "500", description = "Internal server error")
|
||||
@ApiResponse(responseCode = "200", description = "Current time in format yyyy-MM-dd HH:mm:ss"),
|
||||
@ApiResponse(responseCode = "400", description = "Invalid timezone ID provided"),
|
||||
@ApiResponse(responseCode = "500", description = "Internal server error")
|
||||
})
|
||||
public Mono<TimeResponse> getMoscowTime() {
|
||||
log.info("getMoscowTime");
|
||||
public Mono<TimeResponse> getTime(
|
||||
@Parameter(description = "IANA timezone ID (e.g., Europe/Moscow, America/New_York)", example = "Europe/Moscow")
|
||||
@RequestParam(name = "timezone", defaultValue = "Europe/Moscow") String timezone) {
|
||||
|
||||
log.info("getTime called for timezone: {}", timezone);
|
||||
return Mono.fromSupplier(() -> {
|
||||
LocalDateTime moscowTime = LocalDateTime.now(ZoneId.of("Europe/Moscow"));
|
||||
try {
|
||||
ZoneId zoneId = ZoneId.of(timezone);
|
||||
LocalDateTime now = LocalDateTime.now(zoneId);
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
return new TimeResponse(moscowTime.format(formatter));
|
||||
});
|
||||
return new TimeResponse(now.format(formatter));
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("Invalid timezone ID: " + timezone);
|
||||
}
|
||||
}).onErrorMap(IllegalArgumentException.class, e -> new RuntimeException(e.getMessage(), e));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user