Skip to content

Commit

Permalink
Merge pull request #32 from xingcxb/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
xingcxb authored Oct 31, 2023
2 parents 378cf96 + 9036eba commit 0cb34ff
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 1 deletion.
4 changes: 4 additions & 0 deletions _examples/core/dateKit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,7 @@ func TestMs(t *testing.T) {
func TestConstellation(t *testing.T) {
fmt.Println(dateKit.Constellation(time.Now()))
}

func TestFormatWeChatTimeStr(t *testing.T) {
fmt.Println(dateKit.FormatWeChatTimeStr("2023-10-31 14:26:00"))
}
5 changes: 5 additions & 0 deletions _examples/core/file_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package core

import (
"context"
"fmt"
"github.com/xingcxb/goKit/core/fileKit"
"testing"
)

func TestHomeDir(t *testing.T) {
fmt.Println(fileKit.HomeDir(context.Background()))
}

func TestExists(t *testing.T) {
fmt.Println(fileKit.Exists("/Users/symbol/Desktop/1.txt"))
}
Expand Down
2 changes: 1 addition & 1 deletion _examples/core/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestHttpProxyPostFull(t *testing.T) {
func TestHttpProxyBasic(t *testing.T) {
fmt.Println(httpKit.HttpProxyBasic("https://cip.cc", http.MethodGet,
nil, nil, "", 300, "http",
"", "", "255.255.255.255:52724"))
"", "", "255.255.255.255:29093"))
}

func TestUA(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions _examples/core/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ func TestLength(t *testing.T) {
fmt.Println(strKit.Length("👩‍👩‍👦‍👦"))
}

func TestIsNum(t *testing.T) {
fmt.Println(strKit.IsNum("123"))
fmt.Println(strKit.IsNum("123.123"))
fmt.Println(strKit.IsNum("123.123.123"))
}

func TestSplicing(t *testing.T) {
fmt.Println(strKit.Splicing("aa", "bb", "cc"))
}
Expand Down
67 changes: 67 additions & 0 deletions core/dateKit/formatDateKit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package dateKit

import (
"fmt"
"time"
)

// FormatWeChatTimeStr 格式化时间字符串为微信时间
/*
* @param chatTimeStr 时间字符串
* @return 仿微信时间
*/
func FormatWeChatTimeStr(chatTimeStr string) string {
// 将时间字符串转换为时间类型
chatTime := ParseDateTime(chatTimeStr)
// 获取当前时间
now := time.Now()
// 获取间隔时间
duration := now.Sub(chatTime)
// 获取传入时间的年月日
year, month, day := chatTime.Date()
// 获取传入时间的时分
hour, minute, _ := chatTime.Clock()
// 获取当前时间的年月日
nowYear, nowMonth, nowDay := now.Date()
if duration < time.Minute {
// 如果一分钟以内就是现在
return "现在"
}
if duration < time.Hour {
// 如果聊天时间在一小时之内,返回"x分前"
return fmt.Sprintf("%d分钟前", int(duration.Minutes()))
}
if year == nowYear && month == nowMonth && day == nowDay {
// 如果聊天时间在今天之内,则返回"HH:MM"
return fmt.Sprintf("%02d:%02d", hour, minute)
}
if year == nowYear && month == nowMonth && day == nowDay-1 {
//如果聊天时间在昨天之内,请返回"昨天HH:MM"
return fmt.Sprintf("昨天 %02d:%02d", hour, minute)
}
//如果聊天时间在本周内,请返回"周xHH:MM"
if year == nowYear && month == nowMonth && day > nowDay-7 {
weekday := chatTime.Weekday().String()
switch weekday {
case "Monday":
weekday = "一"
case "Tuesday":
weekday = "二"
case "Wednesday":
weekday = "三"
case "Thursday":
weekday = "四"
case "Friday":
weekday = "五"
case "Saturday":
weekday = "六"
case "Sunday":
weekday = "日"
default:
return ""
}
return fmt.Sprintf("周%s %02d:%02d", weekday, hour, minute)
}
// 其它,直接返回 "YYYY/MM/DD HH:MM"
return fmt.Sprintf("%04d/%02d/%02d %02d:%02d", year, month, day, hour, minute)
}
33 changes: 33 additions & 0 deletions core/fileKit/fileKit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,32 @@ package fileKit

import (
"bufio"
"context"
"errors"
"github.com/xingcxb/goKit/core/strKit"
"log"
"os"
"os/user"
"path"
"path/filepath"
"runtime"
"strings"
)

// HomeDir 获取系统当前使用的用户的主目录
/*
* eg: /Users/symbol
* @param ctx 上下文
* @return string,error
*/
func HomeDir(ctx context.Context) (string, error) {
user, err := user.Current()
if err != nil {
return "", err
}
return user.HomeDir, nil
}

// Exists 文件或文件夹是否存在
/**
* @param path 文件或文件夹路径
Expand Down Expand Up @@ -44,6 +60,23 @@ func CreateFile(filePath string) error {
return nil
}

// CreateLazyFile 一次性创建好文件和文件夹
/*
* @param filePath 文件路径
*/
func CreateLazyFile(filePath string) error {
dirPath := filepath.Dir(filePath)
if err := os.MkdirAll(dirPath, os.ModePerm); err != nil {
return err
}
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()
return nil
}

// SaveFile 保存信息到文件
/**
* @param filePath 文件夹路径
Expand Down
8 changes: 8 additions & 0 deletions core/httpKit/httpClientKit.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func HttpBasic(urlString, httpMethod string, headers, paramMap map[string]string
}
bodyReader := strings.NewReader(body)
req, _ := http.NewRequest(httpMethod, urlString, bodyReader)
if headers == nil {
// 如果headers为空,初始化
headers = make(map[string]string, 0)
}
for k, v := range headers {
req.Header.Add(k, v)
}
Expand Down Expand Up @@ -275,6 +279,10 @@ func HttpProxyBasic(urlStr, httpMethod string, headers, paramMap map[string]stri
return headers, "", err
}
// 获取新的headers数据
if headers == nil {
// 如果headers为空,初始化
headers = make(map[string]string, 0)
}
for k, v := range res.Header {
headers[k] = v[0]
}
Expand Down
10 changes: 10 additions & 0 deletions core/strKit/strKit.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ func Splicing(str ...string) string {
return newStr.String()
}

// IsNum 判断字符串是否为数字
/*
* @param s 字符串
* @return 返回是否为数字 true为数字;false为非数字
*/
func IsNum(s string) bool {
_, err := strconv.ParseFloat(s, 64)
return err == nil
}

// SubString 字符串截断
/**
* @param s 原始字符串
Expand Down

0 comments on commit 0cb34ff

Please sign in to comment.