time

package
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2025 License: MIT Imports: 1 Imported by: 0

README

Time 包

time 包基于 carbon 库提供了一组简单的时间处理工具。

特性

  • 基于 carbon 库的时间处理
  • 支持编译时配置(时区、格式、语言等)
  • 提供常用的相对时间获取功能

快速开始

基本使用
package main

import (
    "fmt"
    "github.com/fsyyft-go/kit/time"
)

func main() {
    // 获取当前时间
    now := time.Now()
    fmt.Println(now.ToDateTimeString())

    // 获取相对时间
    yesterday := time.Yesterday()
    tomorrow := time.Tomorrow()
    lastWeek := time.LastWeek()
    nextMonth := time.NextMonth()
}
编译时配置

可以通过 -ldflags 参数在编译时配置包的默认行为:

go build -ldflags "
    -X 'github.com/fsyyft/fsyyft-go/time.defaultDateTimeLayout=2006-01-02 15:04:05'
    -X 'github.com/fsyyft/fsyyft-go/time.defaultTimezone=UTC'
    -X 'github.com/fsyyft/fsyyft-go/time.defaultWeekStartAt=Sunday'
    -X 'github.com/fsyyft/fsyyft-go/time.defaultLocale=en'
"

功能列表

相对时间
  • 日级别

    • Yesterday() - 昨天
    • Tomorrow() - 明天
    • DayBeforeYesterday() - 前天
    • DayAfterTomorrow() - 后天
  • 周级别

    • LastWeek() - 上周
    • NextWeek() - 下周
  • 月级别

    • LastMonth() - 上月
    • NextMonth() - 下月
  • 年级别

    • LastYear() - 去年
    • NextYear() - 明年
配置选项
  • defaultDateTimeLayout - 默认日期时间格式

    • 默认值:2006-01-02T15:04:05.000Z
  • defaultTimezone - 默认时区

    • 默认值:PRC(中国标准时间)
    • 可选值:UTCAsia/ShanghaiAmerica/New_York
  • defaultWeekStartAt - 每周起始日

    • 默认值:Monday(周一)
    • 可选值:Sunday(周日)
  • defaultLocale - 语言环境

    • 默认值:zh-CN(简体中文)
    • 可选值:en(英语)、ja(日语)等

最佳实践

  1. 时区处理

    • 明确指定时区,避免依赖系统默认时区
    • 在跨时区应用中使用 UTC 时间
  2. 格式化

    • 使用标准的时间格式
    • 注意时区信息的保留
  3. 性能优化

    • 重用 Carbon 实例
    • 避免频繁的时区转换

贡献

欢迎提交 Issue 和 Pull Request 来帮助改进这个包。

许可证

本项目采用 MIT License 许可证。详见 LICENSE 文件。

Documentation

Overview

Package time 提供了基于 carbon 库的时间处理工具包。

包配置: 本包提供了多个可在编译时配置的变量,可以通过 go build 的 -ldflags 参数进行修改。 完整的编译示例:

go build -ldflags "
	-X 'github.com/fsyyft/fsyyft-go/time.defaultDateTimeLayout=2006-01-02 15:04:05'
	-X 'github.com/fsyyft/fsyyft-go/time.defaultTimezone=UTC'
	-X 'github.com/fsyyft/fsyyft-go/time.defaultWeekStartAt=Sunday'
	-X 'github.com/fsyyft/fsyyft-go/time.defaultLocale=en'
"

注意:修改这些值会影响整个应用程序中使用此包的所有时间处理行为。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DayAfterTomorrow

func DayAfterTomorrow() carbon.Carbon

DayAfterTomorrow 返回后天同一时间的 Carbon 实例。

返回值:

  • carbon.Carbon: 表示后天同一时间的 Carbon 实例

示例:

dayAfterTomorrow := time.DayAfterTomorrow()
fmt.Println(dayAfterTomorrow.ToDateTimeString()) // 输出后天的日期和当前时间

func DayBeforeYesterday

func DayBeforeYesterday() carbon.Carbon

DayBeforeYesterday 返回前天同一时间的 Carbon 实例。

返回值:

  • carbon.Carbon: 表示前天同一时间的 Carbon 实例

示例:

dayBeforeYesterday := time.DayBeforeYesterday()
fmt.Println(dayBeforeYesterday.ToDateTimeString()) // 输出前天的日期和当前时间

func LastMonth

func LastMonth() carbon.Carbon

LastMonth 返回上个月同一时间的 Carbon 实例。

返回值:

  • carbon.Carbon: 表示上个月同一时间的 Carbon 实例

示例:

lastMonth := time.LastMonth()
fmt.Println(lastMonth.ToDateTimeString()) // 输出上个月的日期和当前时间

func LastWeek

func LastWeek() carbon.Carbon

LastWeek 返回上周同一时间的 Carbon 实例。

返回值:

  • carbon.Carbon: 表示上周同一时间的 Carbon 实例

示例:

lastWeek := time.LastWeek()
fmt.Println(lastWeek.ToDateTimeString()) // 输出上周的日期和当前时间

func LastYear

func LastYear() carbon.Carbon

LastYear 返回去年同一时间的 Carbon 实例。

返回值:

  • carbon.Carbon: 表示去年同一时间的 Carbon 实例

示例:

lastYear := time.LastYear()
fmt.Println(lastYear.ToDateTimeString()) // 输出去年的日期和当前时间

func NextMonth

func NextMonth() carbon.Carbon

NextMonth 返回下个月同一时间的 Carbon 实例。

返回值:

  • carbon.Carbon: 表示下个月同一时间的 Carbon 实例

示例:

nextMonth := time.NextMonth()
fmt.Println(nextMonth.ToDateTimeString()) // 输出下个月的日期和当前时间

func NextWeek

func NextWeek() carbon.Carbon

NextWeek 返回下周同一时间的 Carbon 实例。

返回值:

  • carbon.Carbon: 表示下周同一时间的 Carbon 实例

示例:

nextWeek := time.NextWeek()
fmt.Println(nextWeek.ToDateTimeString()) // 输出下周的日期和当前时间

func NextYear

func NextYear() carbon.Carbon

NextYear 返回明年同一时间的 Carbon 实例。

返回值:

  • carbon.Carbon: 表示明年同一时间的 Carbon 实例

示例:

nextYear := time.NextYear()
fmt.Println(nextYear.ToDateTimeString()) // 输出明年的日期和当前时间

func Now

func Now() carbon.Carbon

Now 返回当前时间的 Carbon 实例。

返回值:

  • carbon.Carbon: 表示当前时间的 Carbon 实例

示例:

now := time.Now()
fmt.Println(now.ToDateTimeString()) // 输出类似:2025-01-02 15:04:05

func Tomorrow

func Tomorrow() carbon.Carbon

Tomorrow 返回明天同一时间的 Carbon 实例。

返回值:

  • carbon.Carbon: 表示明天同一时间的 Carbon 实例

示例:

tomorrow := time.Tomorrow()
fmt.Println(tomorrow.ToDateTimeString()) // 输出明天的日期和当前时间

func Yesterday

func Yesterday() carbon.Carbon

Yesterday 返回昨天同一时间的 Carbon 实例。

返回值:

  • carbon.Carbon: 表示昨天同一时间的 Carbon 实例

示例:

yesterday := time.Yesterday()
fmt.Println(yesterday.ToDateTimeString()) // 输出昨天的日期和当前时间

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL