gct

package module
v0.0.0-...-998631a Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2022 License: MulanPSL-2.0 Imports: 13 Imported by: 1

README

Golang Common Tools

Coverage Status

安装使用

安装

go get -u github.com/xbmlz/gct

使用

import . "github.com/xbmlz/gct"

功能

日期时间操作

支持的常用格式

  • yyyy/MM/dd HH:mm:ss
  • yyyy.MM.dd HH:mm:ss
  • yyyy年MM月dd日 HH时mm分ss秒
  • yyyy-MM-dd
  • yyyy/MM/dd
  • yyyy.MM.dd
  • HH:mm:ss
  • HH时mm分ss秒
  • yyyy-MM-dd HH:mm
  • yyyy-MM-dd HH:mm:ss.SSS
  • yyyyMMddHHmmss
  • yyyyMMddHHmmssSSS
  • yyyyMMdd

根据特定格式格式化日期

s, err := DateUtils.Format(time.Now(), "yyyy-MM-dd")

将日期字符串转换为Time

t, err := DateUtils.Parse("2006-01-02", "yyyy-MM-dd")

日期时间偏移

// 按天偏移
t, err := DateUtils.OffsetDay(time.Now(), -2)
// 按小时偏移
t, err := DateUtils.OffsetHour(time.Now(), -2)
// 按分钟偏移
t, err := DateUtils.OffsetMinute(time.Now(), -2)
// 按秒偏移
t, err := DateUtils.OffsetSecond(time.Now(), -2)
// 按毫秒偏移
t, err := DateUtils.OffsetMillisecond(time.Now(), -2)

日期时间差

// SubDays 日期差
days := DateUtils.SubDays(time.Now(), time.Now()) // n=0
// SubHours 小时差
hours := DateUtils.SubHours(time.Now(), time.Now()) // n=0
// SubMinutes 分钟差
minutes := DateUtils.SubMinutes(time.Now(), time.Now()) // n=0
// SubSeconds 秒差
seconds := DateUtils.SubSeconds(time.Now(), time.Now()) // n=0
// SubMilliseconds 毫秒差
milliseconds := DateUtils.SubMilliseconds(time.Now(), time.Now()) // n=0

年龄

// 根据身份证号计算年龄
age := DateUtils.AgeOfIDCard("123456199501170016") // 26

星座和属相

// 星座
zodiac := DateUtils.GetZodiac(1, 17) // 摩羯座
// 属相
chineseZodiac := DateUtils.GetChineseZodiac(1995) // 猪

文件操作

将字符串写入文件,追加模式

f, err := FileUtils.AppendString("test content", "E:/test.txt")

判断文件是否存在

is := FileUtils.Exist("E:/test.txt")

移除后缀名

s := FileUtils.RemoveSuffix("main.go") // main.go -> main

移除前缀

s := FileUtils.RemovePrefix("main.go") // main.go -> go

压缩zip

err := FileUtils.Zip("./testdata/archive.zip", "./testdata/csv", "./testdata/file.txt")

解压zip

err := FileUtils.Unzip("./testdata/archive.zip", "./testdata/unzip")

字符串操作

判断字符串是否为空

is := StringUtils.IsBlank(" ")

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DateUtils   TDate
	FileUtils   TFile
	StringUtils TString
)

Functions

This section is empty.

Types

type TDate

type TDate byte

func (*TDate) AgeOfIDCard

func (td *TDate) AgeOfIDCard(num string) int

AgeOfIDNumber 根据身份证号计算年龄

func (*TDate) Format

func (td *TDate) Format(date time.Time, format string) (string, error)

Format 根据特定格式格式化日期

func (*TDate) GetChineseZodiac

func (td *TDate) GetChineseZodiac(year int) string

GetChineseZodiac 根据生日计算生肖

func (*TDate) GetZodiac

func (td *TDate) GetZodiac(month, day int) string

GetZodiac 根据生日计算星座

func (*TDate) Offset

func (td *TDate) Offset(date time.Time, format string) (time.Time, error)

Offset 时间偏移

func (*TDate) OffsetDay

func (td *TDate) OffsetDay(date time.Time, day int) (time.Time, error)

OffsetDay 时间日期偏移

func (*TDate) OffsetHour

func (td *TDate) OffsetHour(date time.Time, hour int) (time.Time, error)

OffsetHour 按小时偏移

func (*TDate) OffsetMillisecond

func (td *TDate) OffsetMillisecond(date time.Time, ms int) (time.Time, error)

OffsetMillisecond 按毫秒偏移

func (*TDate) OffsetMinute

func (td *TDate) OffsetMinute(date time.Time, minute int) (time.Time, error)

OffsetMinute 按分钟偏移

func (*TDate) OffsetSecond

func (td *TDate) OffsetSecond(date time.Time, second int) (time.Time, error)

OffsetSecond 按秒偏移

func (*TDate) Parse

func (td *TDate) Parse(dateStr, format string) (time.Time, error)

Parse 将日期字符串转换为Time

func (*TDate) SubDays

func (td *TDate) SubDays(date1, date2 time.Time) int

SubDays 日期差

func (*TDate) SubHours

func (td *TDate) SubHours(date1, date2 time.Time) int

SubHours 小时差

func (*TDate) SubMilliseconds

func (td *TDate) SubMilliseconds(date1, date2 time.Time) int

SubMilliseconds 毫秒差

func (*TDate) SubMinutes

func (td *TDate) SubMinutes(date1, date2 time.Time) int

SubMinutes 分钟差

func (*TDate) SubSeconds

func (td *TDate) SubSeconds(date1, date2 time.Time) int

SubSeconds 秒差

type TFile

type TFile byte

func (*TFile) AppendString

func (tf *TFile) AppendString(content string, path string) (*os.File, error)

AppendString 将String写入文件,追加模式

func (*TFile) Gzip

func (tf *TFile) Gzip(dest string, paths ...string) error

压缩tar文件

func (*TFile) IsDirExists

func (tf *TFile) IsDirExists(dirname string) bool

IsDirExists 判断目录是否存在

func (*TFile) IsFileExists

func (tf *TFile) IsFileExists(path string) bool

IsFileExists 判断文件是否存在

func (*TFile) RemovePrefix

func (tf *TFile) RemovePrefix(path string) string

RemovePrefix 删除文件前缀

func (*TFile) RemoveSuffix

func (tf *TFile) RemoveSuffix(path string) string

RemoveSuffix 删除文件后缀

func (*TFile) UnGzip

func (tf *TFile) UnGzip(src, dest string) error

解压tar文件

func (*TFile) Unzip

func (tf *TFile) Unzip(src string, dest string) error

Unzip 解压文件

func (*TFile) Zip

func (tf *TFile) Zip(dest string, paths ...string) error

Zip 压缩文件

type TString

type TString byte

func (*TString) IsBlank

func (ts *TString) IsBlank(str string) bool

IsBlank 是否空(空白)字符串.

func (*TString) IsBlankOrEmpty

func (ts *TString) IsBlankOrEmpty(str string) bool

IsBlankOrEmpty 是否空白或空字符串.

func (*TString) IsEmpty

func (ts *TString) IsEmpty(str string) bool

IsEmpty 是否空字符串.

func (*TString) IsNotBlank

func (ts *TString) IsNotBlank(str string) bool

IsNotBlank 是否非空(非空白)字符串.

func (*TString) IsNotBlankOrEmpty

func (ts *TString) IsNotBlankOrEmpty(str string) bool

IsNotBlankOrEmpty 是否非空白或非空字符串.

func (*TString) IsNotEmpty

func (ts *TString) IsNotEmpty(str string) bool

IsNotEmpty 是否非空字符串.

Jump to

Keyboard shortcuts

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