diff --git a/saas/backend/api/management/v2/serializers.py b/saas/backend/api/management/v2/serializers.py index 19a222065..eaad5f30b 100644 --- a/saas/backend/api/management/v2/serializers.py +++ b/saas/backend/api/management/v2/serializers.py @@ -167,6 +167,7 @@ class ManagementGroupMemberSLZ(serializers.Serializer): id = serializers.CharField(label="成员id") name = serializers.CharField(label="名称") expired_at = serializers.IntegerField(label="过期时间戳(单位秒)") + created_at = serializers.IntegerField(label="创建时间戳(单位秒)") class ManagementGroupMemberDeleteSLZ(serializers.Serializer): diff --git a/saas/backend/api/management/v2/views/group.py b/saas/backend/api/management/v2/views/group.py index 2e0801901..3f948eaa5 100644 --- a/saas/backend/api/management/v2/views/group.py +++ b/saas/backend/api/management/v2/views/group.py @@ -362,7 +362,9 @@ def list(self, request, *args, **kwargs): limit, offset = CompatiblePagination().get_limit_offset_pair(request) count, group_members = self.biz.list_paging_thin_group_member(group.id, limit, offset) - results = [one.dict(include={"type", "id", "name", "expired_at"}) for one in group_members] + results = [one.dict(include={"type", "id", "name", "expired_at", "created_time"}) for one in group_members] + for result in results: + result['created_at'] = int(result.pop('created_time').timestamp()) return Response({"count": count, "results": results}) @swagger_auto_schema( diff --git a/saas/backend/util/time.py b/saas/backend/util/time.py index ed53eeba1..d61d854d9 100644 --- a/saas/backend/util/time.py +++ b/saas/backend/util/time.py @@ -33,8 +33,11 @@ def utc_string_to_timestamp(str_time: str) -> int: """ 后端UTC时间转换为时间戳 """ - t = string_to_datetime(str_time, fmt="%Y-%m-%dT%H:%M:%SZ") - return int(t.timestamp()) + # Note: 该转换后是 naive datetime,即不带时区 + naive_t = string_to_datetime(str_time, fmt="%Y-%m-%dT%H:%M:%SZ") + # 由于 str_time 本身就是 utc 时间字符串,所以可以设置时区为 UTC,这样就得到 aware datetime + aware_t = naive_t.replace(tzinfo=datetime.timezone.utc) + return int(aware_t.timestamp()) def utc_to_local(utc_time): diff --git a/saas/tests/util/time_tests.py b/saas/tests/util/time_tests.py new file mode 100644 index 000000000..11cc8c463 --- /dev/null +++ b/saas/tests/util/time_tests.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +""" +TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-权限中心(BlueKing-IAM) available. +Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. +Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. +You may obtain a copy of the License at http://opensource.org/licenses/MIT +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the +specific language governing permissions and limitations under the License. +""" +import pytest + +from backend.util.time import utc_string_to_timestamp + + +@pytest.mark.parametrize( + "utc_string, expected", + [ + ("2025-01-07T08:41:18Z", 1736239278), + ("2025-01-07T09:03:44Z", 1736240624), + ("2025-01-02T10:00:15Z", 1735812015), + ("2025-01-08T03:42:02Z", 1736307722), + ], +) +def test_utc_string_to_timestamp(utc_string: str, expected: int): + assert utc_string_to_timestamp(utc_string) == expected