Skip to content

Commit

Permalink
add create_time
Browse files Browse the repository at this point in the history
  • Loading branch information
SAKURA-CAT committed Jun 19, 2024
1 parent b3ea73d commit 5240477
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
13 changes: 13 additions & 0 deletions docs/wiki/第1部分:环境变量.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ modes = SwanLabMode.list()
| API | 名称 | 描述 |
|-------------------------------------|-----------------------|-------------------|
| [is_windows](#is_windows) | 是否为windows系统 | 分为windows和类unix系统 |
| [create_time](#create_time) | 获取当前时间 | 统一为UTC时间 |
| [get_save_dir](#get_save_dir) | 获取存放swanlab全局文件的文件夹路径 | 确保路径存在 |
| [get_swanlog_dir](#get_swanlog_dir) | 获取存放swanlog日志文件的文件夹路径 | 不保证文件夹是否存在 |
| [get_mode](#get_mode) | 获取当前的swanlab解析模式 | 如果没有设置,默认为cloud |
Expand Down Expand Up @@ -120,6 +121,18 @@ else:
print("当前系统为类unix系统")
```

### create_time

本函数用于获取当前时间,是一个符合[ISO 8601标准](https://zh.wikipedia.org/zh-hans/ISO_8601)的字符串,时区为UTC时间。

#### 示例

```python
from swankit.env import create_time

print(create_time()) # 类似的格式:2024-06-19T06:19:04.548711+00:00
```

### get_save_dir

**此函数对应环境变量`SWANLAB_SAVE_DIR`**,用于获取存放swanlab全局文件的文件夹路径,执行此函数将自动创建相关文件夹。
Expand Down
2 changes: 1 addition & 1 deletion swankit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
为了语意方便,应该通过swankit的不同模块调用各个功能函数
"""

__all__ = ["env", "error", "callback", "log"]
__all__ = ["env", "callback", "log"]
7 changes: 7 additions & 0 deletions swankit/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import sys
from enum import Enum
from typing import List
import datetime


class SwanLabMode(Enum):
Expand Down Expand Up @@ -75,6 +76,12 @@ def is_windows() -> bool:
raise OSError("Unknown system, not windows or unix-like system")


def create_time() -> str:
"""获取当前时间(UTC时区)
"""
return datetime.datetime.now(datetime.UTC).isoformat()


def get_save_dir() -> str:
"""
获取存放swanlab全局文件的文件夹路径,如果不存在就创建
Expand Down
8 changes: 8 additions & 0 deletions test/unit/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@
import pytest
import shutil
import nanoid
import datetime
import os


def test_create_time():
t = E.create_time()
assert t.endswith("+00:00")
d = datetime.datetime.fromisoformat(t)
assert d.tzinfo == datetime.UTC


class TestGetFolder:
"""
测试获取全局保存文件夹
Expand Down

0 comments on commit 5240477

Please sign in to comment.