mirror of
https://github.com/skyle1995/NetworkAuth.git
synced 2026-05-25 02:24:05 +08:00
更新底层架构
This commit is contained in:
@@ -5,51 +5,40 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// ============================================================================
|
||||
// 全局变量
|
||||
// ============================================================================
|
||||
|
||||
// serverStartTime 记录进程启动时间(近似服务器启动时间)
|
||||
var serverStartTime = time.Now()
|
||||
|
||||
// ============================================================================
|
||||
// 公共函数
|
||||
// ============================================================================
|
||||
|
||||
// GetServerStartTime 获取服务器启动时间
|
||||
// 返回: 服务器启动的时间戳
|
||||
// - 返回进程初始化时记录的时间点
|
||||
func GetServerStartTime() time.Time {
|
||||
return serverStartTime
|
||||
}
|
||||
|
||||
// GetServerUptime 获取服务器运行时长
|
||||
// 返回: 从服务器启动到现在的时间间隔
|
||||
// GetServerUptime 获取服务器已运行时长
|
||||
// - 通过当前时间与启动时间差计算
|
||||
func GetServerUptime() time.Duration {
|
||||
return time.Since(serverStartTime)
|
||||
}
|
||||
|
||||
// GetServerUptimeString 获取服务器运行时长的字符串表示
|
||||
// 返回: 格式化的运行时长字符串(中文单位)
|
||||
// GetServerUptimeString 获取格式化的服务器运行时长字符串
|
||||
// - 返回不带小数点的时长格式,如 "1h23m45s"
|
||||
func GetServerUptimeString() string {
|
||||
duration := time.Since(serverStartTime)
|
||||
|
||||
// 获取总秒数并转换为整数
|
||||
totalSeconds := int(duration.Seconds())
|
||||
|
||||
// 计算天、小时、分钟、秒
|
||||
days := totalSeconds / 86400
|
||||
hours := (totalSeconds % 86400) / 3600
|
||||
// 计算小时、分钟、秒
|
||||
hours := totalSeconds / 3600
|
||||
minutes := (totalSeconds % 3600) / 60
|
||||
seconds := totalSeconds % 60
|
||||
|
||||
// 根据时长长度选择合适的格式
|
||||
if days > 0 {
|
||||
return fmt.Sprintf("%d天%d小时%d分钟", days, hours, minutes)
|
||||
} else if hours > 0 {
|
||||
return fmt.Sprintf("%d小时%d分钟%d秒", hours, minutes, seconds)
|
||||
if hours > 0 {
|
||||
return fmt.Sprintf("%dh%dm%ds", hours, minutes, seconds)
|
||||
} else if minutes > 0 {
|
||||
return fmt.Sprintf("%d分钟%d秒", minutes, seconds)
|
||||
return fmt.Sprintf("%dm%ds", minutes, seconds)
|
||||
} else {
|
||||
return fmt.Sprintf("%d秒", seconds)
|
||||
return fmt.Sprintf("%ds", seconds)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user