Skip to content

Commit 256fb90

Browse files
committed
refactor(basis): 优化时间工具类
1 parent 6b23770 commit 256fb90

File tree

3 files changed

+48
-10
lines changed
  • mumu-basis/src
  • mumu-services/mumu-authentication/authentication-infrastructure/src/main/java/baby/mumu/authentication/infrastructure/account/gatewayimpl

3 files changed

+48
-10
lines changed

mumu-basis/src/main/kotlin/baby/mumu/basis/kotlin/tools/TimeUtils.kt

+6-9
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
package baby.mumu.basis.kotlin.tools
1717

1818
import baby.mumu.basis.dto.BaseDataTransferObject
19-
import baby.mumu.basis.exception.MuMuException
20-
import baby.mumu.basis.response.ResponseCode
2119
import org.apiguardian.api.API
2220
import java.time.LocalDateTime
2321
import java.time.OffsetDateTime
@@ -173,13 +171,12 @@ object TimeUtils {
173171
*/
174172
@API(status = API.Status.INTERNAL, since = "2.7.0")
175173
@JvmStatic
176-
fun validateTimezone(timezone: String) {
177-
if (timezone.isNotBlank()) {
178-
try {
179-
ZoneId.of(timezone)
180-
} catch (e: Exception) {
181-
throw MuMuException(ResponseCode.TIME_ZONE_IS_NOT_AVAILABLE, e)
182-
}
174+
fun isValidTimeZone(timezone: String): Boolean {
175+
try {
176+
ZoneId.of(timezone)
177+
return true
178+
} catch (_: Exception) {
179+
return false
183180
}
184181
}
185182
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2024-2025, the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package baby.mumu.basis.tools;
17+
18+
import baby.mumu.basis.kotlin.tools.TimeUtils;
19+
import org.junit.jupiter.api.Assertions;
20+
import org.junit.jupiter.api.Test;
21+
22+
/**
23+
* 日期工具类测试
24+
*
25+
* @author <a href="mailto:[email protected]">kaiyu.shan</a>
26+
* @since 2.7.0
27+
*/
28+
public class TimeUtilsTest {
29+
30+
@Test
31+
public void isValidTimeZone() {
32+
boolean test = TimeUtils.isValidTimeZone("test");
33+
Assertions.assertFalse(test);
34+
boolean test1 = TimeUtils.isValidTimeZone("Asia/Shanghai");
35+
Assertions.assertTrue(test1);
36+
}
37+
}

mumu-services/mumu-authentication/authentication-infrastructure/src/main/java/baby/mumu/authentication/infrastructure/account/gatewayimpl/AccountGatewayImpl.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ public void register(Account account) {
140140
account.getUsername(), account.getEmail())
141141
&& !accountArchivedRepository.existsByIdOrUsernameOrEmail(account.getId(),
142142
account.getUsername(), account.getEmail())).ifPresentOrElse(accountPO -> {
143-
TimeUtils.validateTimezone(accountPO.getTimezone());
143+
// 验证时区是否为有效时区类型
144+
if (StringUtils.isNotBlank(accountPO.getTimezone()) && !TimeUtils.isValidTimeZone(
145+
accountPO.getTimezone())) {
146+
throw new MuMuException(ResponseCode.TIME_ZONE_IS_NOT_AVAILABLE);
147+
}
144148
accountPO.setPassword(passwordEncoder.encode(accountPO.getPassword()));
145149
accountRepository.persist(accountPO);
146150
account.setId(accountPO.getId());

0 commit comments

Comments
 (0)