Browse Source

fix:时间相关接口去掉时区

master
白凤吉 2 weeks ago
parent
commit
14714b3978
  1. 7
      src/main/java/com/qyft/ms/app/controller/SystemController.java
  2. 15
      src/main/java/com/qyft/ms/app/service/SystemService.java

7
src/main/java/com/qyft/ms/app/controller/SystemController.java

@ -45,16 +45,15 @@ public class SystemController {
@Operation(summary = "设置系统时间") @Operation(summary = "设置系统时间")
@PostMapping("/set-datetime") @PostMapping("/set-datetime")
public Result<?> setDatetime(@Valid @RequestBody TimeSetDTO timeSetDTO) { public Result<?> setDatetime(@Valid @RequestBody TimeSetDTO timeSetDTO) {
systemService.setSystemTime(timeSetDTO.getEpochMilli(), timeSetDTO.getZone());
systemService.setSystemTime(timeSetDTO.getEpochMilli());
return Result.success(); return Result.success();
} }
@Operation(summary = "获取系统时间") @Operation(summary = "获取系统时间")
@GetMapping("/get-datetime") @GetMapping("/get-datetime")
public Result<?> getDatetime() {
public Result<TimeResponseVO> getDatetime() {
long epochMilli = systemService.getCurrentEpochMilli(); long epochMilli = systemService.getCurrentEpochMilli();
ZoneId systemZone = ZoneId.systemDefault();
return Result.success(new TimeResponseVO(epochMilli, systemZone));
return Result.success(new TimeResponseVO(epochMilli));
} }

15
src/main/java/com/qyft/ms/app/service/SystemService.java

@ -27,23 +27,10 @@ public class SystemService {
* 将系统时区可选和系统时间必选一起设定 * 将系统时区可选和系统时间必选一起设定
* *
* @param epochMilli UTC 毫秒时间戳 * @param epochMilli UTC 毫秒时间戳
* @param zone 可选 IANA 时区 ID若为 null 或空则不修改时区
*/ */
public void setSystemTime(long epochMilli, String zone) {
// 1) 确定要设置的时区客户端没传就用Asia/Shanghai
String effectiveZone = (zone != null && !zone.isBlank())
? zone
: "Asia/Shanghai";
// 2) 先修改系统时区总是执行
runCommand("timedatectl", "set-timezone", effectiveZone);
// 3) 再用 date -s @<秒数> 方式设置系统时间
public void setSystemTime(long epochMilli) {
long epochSecond = epochMilli / 1_000; long epochSecond = epochMilli / 1_000;
runCommand("date", "-s", "@" + epochSecond); runCommand("date", "-s", "@" + epochSecond);
// 4) 将系统时钟写入到硬件时钟RTC
runCommand("hwclock", "--systohc"); runCommand("hwclock", "--systohc");
} }

Loading…
Cancel
Save