Skip to content

Commit

Permalink
Merge pull request #2845 from yunlong098/time
Browse files Browse the repository at this point in the history
feat:获取成员加入用户组的时间
  • Loading branch information
nannan00 authored Jan 9, 2025
2 parents 9c3d409 + c0a5d8c commit 9344c53
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
1 change: 1 addition & 0 deletions saas/backend/api/management/v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion saas/backend/api/management/v2/views/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 5 additions & 2 deletions saas/backend/util/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
26 changes: 26 additions & 0 deletions saas/tests/util/time_tests.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9344c53

Please sign in to comment.