common.go 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901
  1. package utils
  2. import (
  3. "bufio"
  4. "crypto/md5"
  5. "crypto/sha1"
  6. "encoding/base64"
  7. "encoding/hex"
  8. "encoding/json"
  9. "errors"
  10. "fmt"
  11. "github.com/shopspring/decimal"
  12. "gorm.io/gorm"
  13. "image"
  14. "image/png"
  15. "io"
  16. "math"
  17. "math/rand"
  18. "net"
  19. "net/http"
  20. "os"
  21. "path"
  22. "regexp"
  23. "runtime"
  24. "strconv"
  25. "strings"
  26. "time"
  27. "unicode"
  28. )
  29. func GetRandString(size int) string {
  30. allLetterDigit := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "!", "@", "#", "$", "%", "^", "&", "*"}
  31. randomSb := ""
  32. digitSize := len(allLetterDigit)
  33. // 随机数种子
  34. rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
  35. for i := 0; i < size; i++ {
  36. randomSb += allLetterDigit[rnd.Intn(digitSize)]
  37. }
  38. return randomSb
  39. }
  40. func GetRandStringNoSpecialChar(size int) string {
  41. allLetterDigit := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
  42. randomSb := ""
  43. digitSize := len(allLetterDigit)
  44. // 随机数种子
  45. rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
  46. for i := 0; i < size; i++ {
  47. randomSb += allLetterDigit[rnd.Intn(digitSize)]
  48. }
  49. return randomSb
  50. }
  51. func StringsToJSON(str string) string {
  52. rs := []rune(str)
  53. jsons := ""
  54. for _, r := range rs {
  55. rint := int(r)
  56. if rint < 128 {
  57. jsons += string(r)
  58. } else {
  59. jsons += "\\u" + strconv.FormatInt(int64(rint), 16) // json
  60. }
  61. }
  62. return jsons
  63. }
  64. // 序列化
  65. func ToString(v interface{}) string {
  66. data, _ := json.Marshal(v)
  67. return string(data)
  68. }
  69. // md5加密
  70. func MD5(data string) string {
  71. m := md5.Sum([]byte(data))
  72. return hex.EncodeToString(m[:])
  73. }
  74. // 获取数字随机字符
  75. func GetRandDigit(n int) string {
  76. // 随机数种子
  77. rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
  78. return fmt.Sprintf("%0"+strconv.Itoa(n)+"d", rnd.Intn(int(math.Pow10(n))))
  79. }
  80. func GetToday(format string) string {
  81. today := time.Now().Format(format)
  82. return today
  83. }
  84. // 获取今天剩余秒数
  85. func GetTodayLastSecond() time.Duration {
  86. today := GetToday(FormatDate) + " 23:59:59"
  87. end, _ := time.ParseInLocation(FormatDateTime, today, time.Local)
  88. return time.Duration(end.Unix()-time.Now().Local().Unix()) * time.Second
  89. }
  90. // 处理出生日期函数
  91. func GetBrithDate(idcard string) string {
  92. l := len(idcard)
  93. var s string
  94. if l == 15 {
  95. s = "19" + idcard[6:8] + "-" + idcard[8:10] + "-" + idcard[10:12]
  96. return s
  97. }
  98. if l == 18 {
  99. s = idcard[6:10] + "-" + idcard[10:12] + "-" + idcard[12:14]
  100. return s
  101. }
  102. return GetToday(FormatDate)
  103. }
  104. // 处理性别
  105. func WhichSexByIdcard(idcard string) string {
  106. var sexs = [2]string{"女", "男"}
  107. length := len(idcard)
  108. if length == 18 {
  109. sex, _ := strconv.Atoi(string(idcard[16]))
  110. return sexs[sex%2]
  111. } else if length == 15 {
  112. sex, _ := strconv.Atoi(string(idcard[14]))
  113. return sexs[sex%2]
  114. }
  115. return "男"
  116. }
  117. // 截取小数点后几位
  118. func SubFloatToString(f float64, m int) string {
  119. n := strconv.FormatFloat(f, 'f', -1, 64)
  120. if n == "" {
  121. return ""
  122. }
  123. if m >= len(n) {
  124. return n
  125. }
  126. newn := strings.Split(n, ".")
  127. if m == 0 {
  128. return newn[0]
  129. }
  130. if len(newn) < 2 || m >= len(newn[1]) {
  131. return n
  132. }
  133. return newn[0] + "." + newn[1][:m]
  134. }
  135. // 截取小数点后几位
  136. func SubFloatToFloat(f float64, m int) float64 {
  137. newn := SubFloatToString(f, m)
  138. newf, _ := strconv.ParseFloat(newn, 64)
  139. return newf
  140. }
  141. // 截取小数点后几位
  142. func SubFloatToFloatStr(f float64, m int) string {
  143. newn := SubFloatToString(f, m)
  144. return newn
  145. }
  146. // 获取相差时间-年
  147. func GetYearDiffer(start_time, end_time string) int {
  148. t1, _ := time.ParseInLocation("2006-01-02", start_time, time.Local)
  149. t2, _ := time.ParseInLocation("2006-01-02", end_time, time.Local)
  150. age := t2.Year() - t1.Year()
  151. if t2.Month() < t1.Month() || (t2.Month() == t1.Month() && t2.Day() < t1.Day()) {
  152. age--
  153. }
  154. return age
  155. }
  156. // 获取相差时间-秒
  157. func GetSecondDifferByTime(start_time, end_time time.Time) int64 {
  158. diff := end_time.Unix() - start_time.Unix()
  159. return diff
  160. }
  161. func FixFloat(f float64, m int) float64 {
  162. newn := SubFloatToString(f+0.00000001, m)
  163. newf, _ := strconv.ParseFloat(newn, 64)
  164. return newf
  165. }
  166. // 将字符串数组转化为逗号分割的字符串形式 ["str1","str2","str3"] >>> "str1,str2,str3"
  167. func StrListToString(strList []string) (str string) {
  168. if len(strList) > 0 {
  169. for k, v := range strList {
  170. if k == 0 {
  171. str = v
  172. } else {
  173. str = str + "," + v
  174. }
  175. }
  176. return
  177. }
  178. return ""
  179. }
  180. // Token
  181. func GetToken() string {
  182. randStr := GetRandString(64)
  183. token := MD5(randStr + Md5Key)
  184. tokenLen := 64 - len(token)
  185. return strings.ToUpper(token + GetRandString(tokenLen))
  186. }
  187. // 数据没有记录
  188. func ErrNoRow() string {
  189. //return "<QuerySeter> no row found"
  190. return "record not found"
  191. }
  192. // 判断文件是否存在
  193. func FileIsExist(filePath string) bool {
  194. _, err := os.Stat(filePath)
  195. return err == nil || os.IsExist(err)
  196. }
  197. // 获取图片扩展名
  198. func GetImgExt(file string) (ext string, err error) {
  199. var headerByte []byte
  200. headerByte = make([]byte, 8)
  201. fd, err := os.Open(file)
  202. if err != nil {
  203. return "", err
  204. }
  205. defer fd.Close()
  206. _, err = fd.Read(headerByte)
  207. if err != nil {
  208. return "", err
  209. }
  210. xStr := fmt.Sprintf("%x", headerByte)
  211. switch {
  212. case xStr == "89504e470d0a1a0a":
  213. ext = ".png"
  214. case xStr == "0000010001002020":
  215. ext = ".ico"
  216. case xStr == "0000020001002020":
  217. ext = ".cur"
  218. case xStr[:12] == "474946383961" || xStr[:12] == "474946383761":
  219. ext = ".gif"
  220. case xStr[:10] == "0000020000" || xStr[:10] == "0000100000":
  221. ext = ".tga"
  222. case xStr[:8] == "464f524d":
  223. ext = ".iff"
  224. case xStr[:8] == "52494646":
  225. ext = ".ani"
  226. case xStr[:4] == "4d4d" || xStr[:4] == "4949":
  227. ext = ".tiff"
  228. case xStr[:4] == "424d":
  229. ext = ".bmp"
  230. case xStr[:4] == "ffd8":
  231. ext = ".jpg"
  232. case xStr[:2] == "0a":
  233. ext = ".pcx"
  234. default:
  235. ext = ""
  236. }
  237. return ext, nil
  238. }
  239. // 保存图片
  240. func SaveImage(path string, img image.Image) (err error) {
  241. //需要保持的文件
  242. imgfile, err := os.Create(path)
  243. defer imgfile.Close()
  244. // 以PNG格式保存文件
  245. err = png.Encode(imgfile, img)
  246. return err
  247. }
  248. // 下载图片
  249. func DownloadImage(imgUrl string) (filePath string, err error) {
  250. imgPath := "./static/imgs/"
  251. fileName := path.Base(imgUrl)
  252. res, err := http.Get(imgUrl)
  253. if err != nil {
  254. fmt.Println("A error occurred!")
  255. return
  256. }
  257. defer res.Body.Close()
  258. // 获得get请求响应的reader对象
  259. reader := bufio.NewReaderSize(res.Body, 32*1024)
  260. filePath = imgPath + fileName
  261. file, err := os.Create(filePath)
  262. if err != nil {
  263. return
  264. }
  265. // 获得文件的writer对象
  266. writer := bufio.NewWriter(file)
  267. written, _ := io.Copy(writer, reader)
  268. fmt.Printf("Total length: %d \n", written)
  269. return
  270. }
  271. // 保存base64数据为文件
  272. func SaveBase64ToFile(content, path string) error {
  273. data, err := base64.StdEncoding.DecodeString(content)
  274. if err != nil {
  275. return err
  276. }
  277. f, err := os.Create(path)
  278. defer f.Close()
  279. if err != nil {
  280. return err
  281. }
  282. f.Write(data)
  283. return nil
  284. }
  285. func SaveBase64ToFileBySeek(content, path string) (err error) {
  286. data, err := base64.StdEncoding.DecodeString(content)
  287. exist, err := PathExists(path)
  288. if err != nil {
  289. return
  290. }
  291. if !exist {
  292. f, err := os.Create(path)
  293. if err != nil {
  294. return err
  295. }
  296. n, _ := f.Seek(0, 2)
  297. // 从末尾的偏移量开始写入内容
  298. _, err = f.WriteAt([]byte(data), n)
  299. defer f.Close()
  300. } else {
  301. f, err := os.OpenFile(path, os.O_WRONLY, 0644)
  302. if err != nil {
  303. return err
  304. }
  305. n, _ := f.Seek(0, 2)
  306. // 从末尾的偏移量开始写入内容
  307. _, err = f.WriteAt([]byte(data), n)
  308. defer f.Close()
  309. }
  310. return nil
  311. }
  312. func PathExists(path string) (bool, error) {
  313. _, err := os.Stat(path)
  314. if err == nil {
  315. return true, nil
  316. }
  317. if os.IsNotExist(err) {
  318. return false, nil
  319. }
  320. return false, err
  321. }
  322. func StartIndex(page, pagesize int) int {
  323. if page > 1 {
  324. return (page - 1) * pagesize
  325. }
  326. return 0
  327. }
  328. func PageCount(count, pagesize int) int {
  329. if count%pagesize > 0 {
  330. return count/pagesize + 1
  331. } else {
  332. return count / pagesize
  333. }
  334. }
  335. func TrimHtml(src string) string {
  336. //将HTML标签全转换成小写
  337. re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
  338. src = re.ReplaceAllStringFunc(src, strings.ToLower)
  339. re, _ = regexp.Compile("\\<img[\\S\\s]+?\\>")
  340. src = re.ReplaceAllString(src, "[图片]")
  341. re, _ = regexp.Compile("class[\\S\\s]+?>")
  342. src = re.ReplaceAllString(src, "")
  343. re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
  344. src = re.ReplaceAllString(src, "")
  345. return strings.TrimSpace(src)
  346. }
  347. //1556164246 -> 2019-04-25 03:50:46 +0000
  348. //timestamp
  349. func TimeToTimestamp() {
  350. fmt.Println(time.Unix(1556164246, 0).Format("2006-01-02 15:04:05"))
  351. }
  352. func ToUnicode(text string) string {
  353. textQuoted := strconv.QuoteToASCII(text)
  354. textUnquoted := textQuoted[1 : len(textQuoted)-1]
  355. return textUnquoted
  356. }
  357. func VersionToInt(version string) int {
  358. version = strings.Replace(version, ".", "", -1)
  359. n, _ := strconv.Atoi(version)
  360. return n
  361. }
  362. func IsCheckInList(list []int, s int) bool {
  363. for _, v := range list {
  364. if v == s {
  365. return true
  366. }
  367. }
  368. return false
  369. }
  370. func round(num float64) int {
  371. return int(num + math.Copysign(0.5, num))
  372. }
  373. func toFixed(num float64, precision int) float64 {
  374. output := math.Pow(10, float64(precision))
  375. return float64(round(num*output)) / output
  376. }
  377. // GetWilsonScore returns Wilson Score
  378. func GetWilsonScore(p, n float64) float64 {
  379. if p == 0 && n == 0 {
  380. return 0
  381. }
  382. return toFixed(((p+1.9208)/(p+n)-1.96*math.Sqrt(p*n/(p+n)+0.9604)/(p+n))/(1+3.8416/(p+n)), 2)
  383. }
  384. // 将中文数字转化成数字,比如 第三百四十五章,返回第345章 不支持一亿及以上
  385. func ChangeWordsToNum(str string) (numStr string) {
  386. words := ([]rune)(str)
  387. num := 0
  388. n := 0
  389. for i := 0; i < len(words); i++ {
  390. word := string(words[i : i+1])
  391. switch word {
  392. case "万":
  393. if n == 0 {
  394. n = 1
  395. }
  396. n = n * 10000
  397. num = num*10000 + n
  398. n = 0
  399. case "千":
  400. if n == 0 {
  401. n = 1
  402. }
  403. n = n * 1000
  404. num += n
  405. n = 0
  406. case "百":
  407. if n == 0 {
  408. n = 1
  409. }
  410. n = n * 100
  411. num += n
  412. n = 0
  413. case "十":
  414. if n == 0 {
  415. n = 1
  416. }
  417. n = n * 10
  418. num += n
  419. n = 0
  420. case "一":
  421. n += 1
  422. case "二":
  423. n += 2
  424. case "三":
  425. n += 3
  426. case "四":
  427. n += 4
  428. case "五":
  429. n += 5
  430. case "六":
  431. n += 6
  432. case "七":
  433. n += 7
  434. case "八":
  435. n += 8
  436. case "九":
  437. n += 9
  438. case "零":
  439. default:
  440. if n > 0 {
  441. num += n
  442. n = 0
  443. }
  444. if num == 0 {
  445. numStr += word
  446. } else {
  447. numStr += strconv.Itoa(num) + word
  448. num = 0
  449. }
  450. }
  451. }
  452. if n > 0 {
  453. num += n
  454. n = 0
  455. }
  456. if num != 0 {
  457. numStr += strconv.Itoa(num)
  458. }
  459. return
  460. }
  461. func Sha1(data string) string {
  462. sha1 := sha1.New()
  463. sha1.Write([]byte(data))
  464. return hex.EncodeToString(sha1.Sum([]byte("")))
  465. }
  466. func GetMaxTradeCode(tradeCode string) (maxTradeCode string, err error) {
  467. tradeCode = strings.Replace(tradeCode, "W", "", -1)
  468. tradeCode = strings.Trim(tradeCode, " ")
  469. tradeCodeInt, err := strconv.Atoi(tradeCode)
  470. if err != nil {
  471. return
  472. }
  473. tradeCodeInt = tradeCodeInt + 1
  474. maxTradeCode = fmt.Sprintf("W%06d", tradeCodeInt)
  475. return
  476. }
  477. // excel日期字段格式化 yyyy-mm-dd
  478. func ConvertToFormatDay(excelDaysString string) string {
  479. // 2006-01-02 距离 1900-01-01的天数
  480. baseDiffDay := 38719 //在网上工具计算的天数需要加2天,什么原因没弄清楚
  481. curDiffDay := excelDaysString
  482. b, _ := strconv.Atoi(curDiffDay)
  483. // 获取excel的日期距离2006-01-02的天数
  484. realDiffDay := b - baseDiffDay
  485. //fmt.Println("realDiffDay:",realDiffDay)
  486. // 距离2006-01-02 秒数
  487. realDiffSecond := realDiffDay * 24 * 3600
  488. //fmt.Println("realDiffSecond:",realDiffSecond)
  489. // 2006-01-02 15:04:05距离1970-01-01 08:00:00的秒数 网上工具可查出
  490. baseOriginSecond := 1136185445
  491. resultTime := time.Unix(int64(baseOriginSecond+realDiffSecond), 0).Format("2006-01-02")
  492. return resultTime
  493. }
  494. func CheckPwd(pwd string) bool {
  495. compile := `([0-9a-z]+){6,12}|(a-z0-9]+){6,12}`
  496. reg := regexp.MustCompile(compile)
  497. flag := reg.MatchString(pwd)
  498. return flag
  499. }
  500. func GetMonthStartAndEnd(myYear string, myMonth string) (startDate, endDate string) {
  501. // 数字月份必须前置补零
  502. if len(myMonth) == 1 {
  503. myMonth = "0" + myMonth
  504. }
  505. yInt, _ := strconv.Atoi(myYear)
  506. timeLayout := "2006-01-02 15:04:05"
  507. loc, _ := time.LoadLocation("Local")
  508. theTime, _ := time.ParseInLocation(timeLayout, myYear+"-"+myMonth+"-01 00:00:00", loc)
  509. newMonth := theTime.Month()
  510. t1 := time.Date(yInt, newMonth, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  511. t2 := time.Date(yInt, newMonth+1, 0, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  512. return t1, t2
  513. }
  514. // 移除字符串中的空格
  515. func TrimStr(str string) (str2 string) {
  516. if str == "" {
  517. return str
  518. }
  519. return strings.Replace(str, " ", "", -1)
  520. }
  521. // 字符串转换为time
  522. func StrTimeToTime(strTime string) time.Time {
  523. timeLayout := "2006-01-02 15:04:05" //转化所需模板
  524. loc, _ := time.LoadLocation("Local") //重要:获取时区
  525. resultTime, _ := time.ParseInLocation(timeLayout, strTime, loc)
  526. return resultTime
  527. }
  528. // 字符串类型时间转周几
  529. func StrDateTimeToWeek(strTime string) string {
  530. var WeekDayMap = map[string]string{
  531. "Monday": "周一",
  532. "Tuesday": "周二",
  533. "Wednesday": "周三",
  534. "Thursday": "周四",
  535. "Friday": "周五",
  536. "Saturday": "周六",
  537. "Sunday": "周日",
  538. }
  539. var ctime = StrTimeToTime(strTime).Format("2006-01-02")
  540. startday, _ := time.ParseInLocation("2006-01-02", ctime, time.Local)
  541. staweek_int := startday.Weekday().String()
  542. return WeekDayMap[staweek_int]
  543. }
  544. // 时间格式转年月日字符串
  545. func TimeToStrYmd(time2 time.Time) string {
  546. var Ymd string
  547. year := time2.Year()
  548. month := time2.Format("1")
  549. day1 := time.Now().Day()
  550. Ymd = strconv.Itoa(year) + "年" + month + "月" + strconv.Itoa(day1) + "日"
  551. return Ymd
  552. }
  553. // 时间格式去掉时分秒
  554. func TimeRemoveHms(strTime string) string {
  555. var Ymd string
  556. var resultTime = StrTimeToTime(strTime)
  557. year := resultTime.Year()
  558. month := resultTime.Format("01")
  559. day1 := resultTime.Day()
  560. Ymd = strconv.Itoa(year) + "." + month + "." + strconv.Itoa(day1)
  561. return Ymd
  562. }
  563. // 时间格式去掉时分秒
  564. func TimeRemoveHms2(strTime string) string {
  565. var Ymd string
  566. var resultTime = StrTimeToTime(strTime)
  567. year := resultTime.Year()
  568. month := resultTime.Format("01")
  569. day1 := resultTime.Day()
  570. Ymd = strconv.Itoa(year) + "-" + month + "-" + strconv.Itoa(day1)
  571. return Ymd
  572. }
  573. // 文章上一次编辑时间
  574. func ArticleLastTime(strTime string) string {
  575. var newTime string
  576. stamp, _ := time.ParseInLocation("2006-01-02 15:04:05", strTime, time.Local)
  577. diffTime := time.Now().Unix() - stamp.Unix()
  578. if diffTime <= 60 {
  579. newTime = "当前"
  580. } else if diffTime < 60*60 {
  581. newTime = strconv.FormatInt(diffTime/60, 10) + "分钟前"
  582. } else if diffTime < 24*60*60 {
  583. newTime = strconv.FormatInt(diffTime/(60*60), 10) + "小时前"
  584. } else if diffTime < 30*24*60*60 {
  585. newTime = strconv.FormatInt(diffTime/(24*60*60), 10) + "天前"
  586. } else if diffTime < 12*30*24*60*60 {
  587. newTime = strconv.FormatInt(diffTime/(30*24*60*60), 10) + "月前"
  588. } else {
  589. newTime = "1年前"
  590. }
  591. return newTime
  592. }
  593. // 人民币小写转大写
  594. func ConvertNumToCny(num float64) (str string, err error) {
  595. strNum := strconv.FormatFloat(num*100, 'f', 0, 64)
  596. sliceUnit := []string{"仟", "佰", "拾", "亿", "仟", "佰", "拾", "万", "仟", "佰", "拾", "元", "角", "分"}
  597. // log.Println(sliceUnit[:len(sliceUnit)-2])
  598. s := sliceUnit[len(sliceUnit)-len(strNum):]
  599. upperDigitUnit := map[string]string{"0": "零", "1": "壹", "2": "贰", "3": "叁", "4": "肆", "5": "伍", "6": "陆", "7": "柒", "8": "捌", "9": "玖"}
  600. for k, v := range strNum[:] {
  601. str = str + upperDigitUnit[string(v)] + s[k]
  602. }
  603. reg, err := regexp.Compile(`零角零分$`)
  604. str = reg.ReplaceAllString(str, "整")
  605. reg, err = regexp.Compile(`零角`)
  606. str = reg.ReplaceAllString(str, "零")
  607. reg, err = regexp.Compile(`零分$`)
  608. str = reg.ReplaceAllString(str, "整")
  609. reg, err = regexp.Compile(`零[仟佰拾]`)
  610. str = reg.ReplaceAllString(str, "零")
  611. reg, err = regexp.Compile(`零{2,}`)
  612. str = reg.ReplaceAllString(str, "零")
  613. reg, err = regexp.Compile(`零亿`)
  614. str = reg.ReplaceAllString(str, "亿")
  615. reg, err = regexp.Compile(`零万`)
  616. str = reg.ReplaceAllString(str, "万")
  617. reg, err = regexp.Compile(`零*元`)
  618. str = reg.ReplaceAllString(str, "元")
  619. reg, err = regexp.Compile(`亿零{0, 3}万`)
  620. str = reg.ReplaceAllString(str, "^元")
  621. reg, err = regexp.Compile(`零元`)
  622. str = reg.ReplaceAllString(str, "零")
  623. return
  624. }
  625. // GetNowWeekMonday 获取本周周一的时间
  626. func GetNowWeekMonday() time.Time {
  627. offset := int(time.Monday - time.Now().Weekday())
  628. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  629. offset = -6
  630. }
  631. mondayTime := time.Now().AddDate(0, 0, offset)
  632. mondayTime = time.Date(mondayTime.Year(), mondayTime.Month(), mondayTime.Day(), 0, 0, 0, 0, mondayTime.Location())
  633. return mondayTime
  634. }
  635. // GetNowWeekLastDay 获取本周最后一天的时间
  636. func GetNowWeekLastDay() time.Time {
  637. offset := int(time.Monday - time.Now().Weekday())
  638. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  639. offset = -6
  640. }
  641. firstDayTime := time.Now().AddDate(0, 0, offset)
  642. firstDayTime = time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 0, 0, 0, 0, firstDayTime.Location()).AddDate(0, 0, 6)
  643. lastDayTime := time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 23, 59, 59, 0, firstDayTime.Location())
  644. return lastDayTime
  645. }
  646. // GetNowMonthFirstDay 获取本月第一天的时间
  647. func GetNowMonthFirstDay() time.Time {
  648. nowMonthFirstDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location())
  649. return nowMonthFirstDay
  650. }
  651. // GetNowMonthLastDay 获取本月最后一天的时间
  652. func GetNowMonthLastDay() time.Time {
  653. nowMonthLastDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  654. nowMonthLastDay = time.Date(nowMonthLastDay.Year(), nowMonthLastDay.Month(), nowMonthLastDay.Day(), 23, 59, 59, 0, nowMonthLastDay.Location())
  655. return nowMonthLastDay
  656. }
  657. // GetNowQuarterFirstDay 获取本季度第一天的时间
  658. func GetNowQuarterFirstDay() time.Time {
  659. month := int(time.Now().Month())
  660. var nowQuarterFirstDay time.Time
  661. if month >= 1 && month <= 3 {
  662. //1月1号
  663. nowQuarterFirstDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  664. } else if month >= 4 && month <= 6 {
  665. //4月1号
  666. nowQuarterFirstDay = time.Date(time.Now().Year(), 4, 1, 0, 0, 0, 0, time.Now().Location())
  667. } else if month >= 7 && month <= 9 {
  668. nowQuarterFirstDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
  669. } else {
  670. nowQuarterFirstDay = time.Date(time.Now().Year(), 10, 1, 0, 0, 0, 0, time.Now().Location())
  671. }
  672. return nowQuarterFirstDay
  673. }
  674. // GetNowQuarterLastDay 获取本季度最后一天的时间
  675. func GetNowQuarterLastDay() time.Time {
  676. month := int(time.Now().Month())
  677. var nowQuarterLastDay time.Time
  678. if month >= 1 && month <= 3 {
  679. //03-31 23:59:59
  680. nowQuarterLastDay = time.Date(time.Now().Year(), 3, 31, 23, 59, 59, 0, time.Now().Location())
  681. } else if month >= 4 && month <= 6 {
  682. //06-30 23:59:59
  683. nowQuarterLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
  684. } else if month >= 7 && month <= 9 {
  685. //09-30 23:59:59
  686. nowQuarterLastDay = time.Date(time.Now().Year(), 9, 30, 23, 59, 59, 0, time.Now().Location())
  687. } else {
  688. //12-31 23:59:59
  689. nowQuarterLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  690. }
  691. return nowQuarterLastDay
  692. }
  693. // GetNowHalfYearFirstDay 获取当前半年的第一天的时间
  694. func GetNowHalfYearFirstDay() time.Time {
  695. month := int(time.Now().Month())
  696. var nowHalfYearLastDay time.Time
  697. if month >= 1 && month <= 6 {
  698. //03-31 23:59:59
  699. nowHalfYearLastDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  700. } else {
  701. //12-31 23:59:59
  702. nowHalfYearLastDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
  703. }
  704. return nowHalfYearLastDay
  705. }
  706. // GetNowHalfYearLastDay 获取当前半年的最后一天的时间
  707. func GetNowHalfYearLastDay() time.Time {
  708. month := int(time.Now().Month())
  709. var nowHalfYearLastDay time.Time
  710. if month >= 1 && month <= 6 {
  711. //03-31 23:59:59
  712. nowHalfYearLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
  713. } else {
  714. //12-31 23:59:59
  715. nowHalfYearLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  716. }
  717. return nowHalfYearLastDay
  718. }
  719. // GetNowYearFirstDay 获取当前年的最后一天的时间
  720. func GetNowYearFirstDay() time.Time {
  721. //12-31 23:59:59
  722. nowYearFirstDay := time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  723. return nowYearFirstDay
  724. }
  725. // GetNowYearLastDay 获取当前年的最后一天的时间
  726. func GetNowYearLastDay() time.Time {
  727. //12-31 23:59:59
  728. nowYearLastDay := time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  729. return nowYearLastDay
  730. }
  731. // CalculationDate 计算两个日期之间相差n年m月y天
  732. // FormatPrice 格式化展示金额数字(财务金额展示,小数点前,每三位用,隔开) 1,234,567,898.55
  733. func FormatPrice(price float64) (str string) {
  734. str = decimal.NewFromFloat(price).String()
  735. length := len(str)
  736. if length < 4 {
  737. return str
  738. }
  739. arr := strings.Split(str, ".") //用小数点符号分割字符串,为数组接收
  740. length1 := len(arr[0])
  741. if length1 < 4 {
  742. return str
  743. }
  744. count := (length1 - 1) / 3
  745. for i := 0; i < count; i++ {
  746. arr[0] = arr[0][:length1-(i+1)*3] + "," + arr[0][length1-(i+1)*3:]
  747. }
  748. return strings.Join(arr, ".") //将一系列字符串连接为一个字符串,之间用sep来分隔。
  749. }
  750. // getMonthDay 获取某年某月有多少天
  751. func getMonthDay(year, month int) (days int) {
  752. if month != 2 {
  753. if month == 4 || month == 6 || month == 9 || month == 11 {
  754. days = 30
  755. } else {
  756. days = 31
  757. }
  758. } else {
  759. if ((year%4) == 0 && (year%100) != 0) || (year%400) == 0 {
  760. days = 29
  761. } else {
  762. days = 28
  763. }
  764. }
  765. return
  766. }
  767. func SaveToFile(content, path string) error {
  768. f, err := os.Create(path)
  769. defer f.Close()
  770. if err != nil {
  771. return err
  772. }
  773. f.Write([]byte(content))
  774. return nil
  775. }
  776. // HideString 给字段加***(从字符串中间替换,少于需要替换的长度,那么就补全*的长度)
  777. // src 待*字符串
  778. // hideLen 需要加*的长度
  779. func HideString(src string, hideLen int) string {
  780. if src == "" {
  781. return src
  782. }
  783. str := []rune(src)
  784. if hideLen == 0 {
  785. hideLen = 4
  786. }
  787. hideStr := ""
  788. for i := 0; i < hideLen; i++ {
  789. hideStr += "*"
  790. }
  791. strLen := len(str)
  792. // 字符长度是1
  793. if strLen == 1 {
  794. return string(str[:1]) + hideStr
  795. }
  796. //字符长度大于1,但是小于等于需要隐藏的字符长度,那么就隐藏中间,保留前后各一位字符
  797. if strLen <= hideLen+2 {
  798. return string(str[:1]) + hideStr + string(str[strLen-1:])
  799. }
  800. subLen := strLen - hideLen //剩余需要展示的字符长度
  801. decimal.NewFromFloat(2)
  802. frontLenDecimal := decimal.NewFromInt(int64(subLen)).Div(decimal.NewFromInt(2)) //前面需要展示的字符的长度
  803. frontLen := frontLenDecimal.Floor().IntPart()
  804. return string(str[:frontLen]) + hideStr + string(str[frontLen+int64(hideLen):])
  805. }
  806. // 用户参会时间转换
  807. func GetAttendanceDetailSeconds(secondNum int) string {
  808. var timeStr string
  809. if secondNum <= 60 {
  810. if secondNum < 10 {
  811. timeStr = "0" + strconv.Itoa(secondNum) + "''"
  812. } else {
  813. timeStr = strconv.Itoa(secondNum) + "''"
  814. }
  815. } else {
  816. var remainderStr string
  817. remainderNum := secondNum % 60
  818. minuteNum := secondNum / 60
  819. if remainderNum < 10 {
  820. remainderStr = "0" + strconv.Itoa(remainderNum) + "''"
  821. } else {
  822. remainderStr = strconv.Itoa(remainderNum) + "''"
  823. }
  824. if minuteNum < 10 {
  825. timeStr = "0" + strconv.Itoa(minuteNum) + "'" + remainderStr
  826. } else {
  827. timeStr = strconv.Itoa(minuteNum) + "'" + remainderStr
  828. }
  829. }
  830. return timeStr
  831. }
  832. // SubStr 截取字符串(中文)
  833. func SubStr(str string, subLen int) string {
  834. strRune := []rune(str)
  835. bodyRuneLen := len(strRune)
  836. if bodyRuneLen > subLen {
  837. bodyRuneLen = subLen
  838. }
  839. str = string(strRune[:bodyRuneLen])
  840. return str
  841. }
  842. func GetLocalIP() (ip string, err error) {
  843. addrs, err := net.InterfaceAddrs()
  844. if err != nil {
  845. return
  846. }
  847. for _, addr := range addrs {
  848. ipAddr, ok := addr.(*net.IPNet)
  849. if !ok {
  850. continue
  851. }
  852. if ipAddr.IP.IsLoopback() {
  853. continue
  854. }
  855. if !ipAddr.IP.IsGlobalUnicast() {
  856. continue
  857. }
  858. return ipAddr.IP.String(), nil
  859. }
  860. return
  861. }
  862. func PrintLog(params ...string) {
  863. _, file, line, ok := runtime.Caller(1)
  864. fmt.Println(file, line, ok, params)
  865. }
  866. // InArrayByStr php中的in_array(判断String类型的切片中是否存在该string值)
  867. func InArrayByStr(idStrList []string, searchId string) (has bool) {
  868. for _, id := range idStrList {
  869. if id == searchId {
  870. has = true
  871. return
  872. }
  873. }
  874. return
  875. }
  876. // InArrayByInt php中的in_array(判断Int类型的切片中是否存在该Int值)
  877. func InArrayByInt(idStrList []int, searchId int) (has bool) {
  878. for _, id := range idStrList {
  879. if id == searchId {
  880. has = true
  881. return
  882. }
  883. }
  884. return
  885. }
  886. // GetOrmInReplace 获取orm的in查询替换?的方法
  887. func GetOrmInReplace(num int) string {
  888. /*template := make([]string, num)
  889. for i := 0; i < num; i++ {
  890. template[i] = "?"
  891. }
  892. return strings.Join(template, ",")*/
  893. return "?"
  894. }
  895. // GetTimeSubDay 计算两个时间的自然日期差
  896. func GetTimeSubDay(t1, t2 time.Time) int {
  897. var day int
  898. swap := false
  899. if t1.Unix() > t2.Unix() {
  900. t1, t2 = t2, t1
  901. swap = true
  902. }
  903. t1_ := t1.Add(time.Duration(t2.Sub(t1).Milliseconds()%86400000) * time.Millisecond)
  904. day = int(t2.Sub(t1).Hours() / 24)
  905. // 计算在t1+两个时间的余数之后天数是否有变化
  906. if t1_.Day() != t1.Day() {
  907. day += 1
  908. }
  909. if swap {
  910. day = -day
  911. }
  912. return day
  913. }
  914. // GetFrequencyEndDay 根据当前时间和频度,获取该频度下最后一天的日期
  915. func GetFrequencyEndDay(currDate time.Time, frequency string) (endDate time.Time) {
  916. switch frequency {
  917. case "周度":
  918. // 如果当前就是最后一天,那么就直接返回本日期就好了
  919. if currDate.Weekday() == 0 {
  920. endDate = currDate
  921. } else {
  922. endDate = currDate.AddDate(0, 0, 7-int(currDate.Weekday()))
  923. }
  924. case "旬度":
  925. nextDay := currDate.AddDate(0, 0, 1)
  926. if nextDay.Day() == 1 || currDate.Day() == 10 || currDate.Day() == 20 {
  927. //如果是每月10、20、最后一天,那么就直接返回本日期就好了
  928. endDate = currDate
  929. } else {
  930. if currDate.Day() < 10 { // 每月10号
  931. endDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, time.Local)
  932. } else if currDate.Day() < 20 { // 每月10号
  933. endDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, time.Local)
  934. } else {
  935. // 下旬,多种可能,最大天数可能存在8天,9天,10天,11天,
  936. tmpNextMonth := currDate.AddDate(0, 0, 13)
  937. endDate = time.Date(tmpNextMonth.Year(), tmpNextMonth.Month(), 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  938. }
  939. }
  940. case "月度":
  941. nextDay := currDate.AddDate(0, 0, 1)
  942. if nextDay.Day() == 1 {
  943. //如果是每月的最后一天,那么就直接返回本日期就好了
  944. endDate = currDate
  945. } else {
  946. endDate = time.Date(nextDay.Year(), nextDay.Month()+1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  947. }
  948. case "季度":
  949. nextDay := currDate.AddDate(0, 0, 1)
  950. if (nextDay.Month() == 1 || nextDay.Month() == 4 || nextDay.Month() == 7 || nextDay.Month() == 10) && nextDay.Day() == 1 {
  951. //如果是每季的最后一天,那么就直接返回本日期就好了
  952. endDate = currDate
  953. } else {
  954. if currDate.Month() < 4 { // 1季度
  955. endDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, time.Local)
  956. } else if currDate.Month() < 7 { // 2季度
  957. endDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, time.Local)
  958. } else if currDate.Month() < 10 { // 3季度
  959. endDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, time.Local)
  960. } else {
  961. // 4季度
  962. endDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, time.Local)
  963. }
  964. }
  965. case "年度":
  966. endDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, time.Local)
  967. default:
  968. endDate = currDate
  969. return
  970. }
  971. return
  972. }
  973. // CheckFrequency 获取两个频度之间是否相对低高频
  974. // 大于0,代表左侧是高频(例:左侧:日度,右侧:周度)
  975. // 等于0,代表同频
  976. // 小于0,代表右侧是高频(例:左侧:周度,右侧:日度)
  977. func CheckFrequency(leftFrequency, rightFrequency string) int {
  978. frequencyMap := map[string]int{
  979. "年度": 0,
  980. "半年度": 1,
  981. "季度": 2,
  982. "月度": 3,
  983. "旬度": 4,
  984. "周度": 5,
  985. "日度": 6,
  986. }
  987. return frequencyMap[leftFrequency] - frequencyMap[rightFrequency]
  988. }
  989. // 将下划线命名转为驼峰命名
  990. func SnakeToCamel(s string) string {
  991. var result string
  992. upper := true
  993. for _, c := range s {
  994. if c == '_' {
  995. upper = true
  996. continue
  997. }
  998. if upper {
  999. result += string(unicode.ToUpper(c))
  1000. upper = false
  1001. } else {
  1002. result += string(c)
  1003. }
  1004. }
  1005. return result
  1006. }
  1007. // 将驼峰命名转为下划线命名
  1008. func CamelToSnake(s string) string {
  1009. var result string
  1010. for i, c := range s {
  1011. if unicode.IsUpper(c) {
  1012. if i > 0 {
  1013. result += "_"
  1014. }
  1015. result += string(unicode.ToLower(c))
  1016. } else {
  1017. result += string(c)
  1018. }
  1019. }
  1020. return result
  1021. }
  1022. func TimeTransferString(format string, t time.Time) string {
  1023. str := t.Format(format)
  1024. if t.IsZero() {
  1025. return ""
  1026. }
  1027. if strings.Contains(str, "0001-01-01") {
  1028. return ""
  1029. }
  1030. return str
  1031. }
  1032. // GetEdbRefreshStartDate
  1033. // @Description: 获取开始刷新时间
  1034. // @author: Roc
  1035. // @datetime 2024-02-05 11:23:55
  1036. // @param startDate string
  1037. // @return string
  1038. func GetEdbRefreshStartDate(startDate string) string {
  1039. // 没有传入日期,或者日期异常的话,那么就是从1990-01-01开始
  1040. if startDate == `` || strings.Contains(startDate, "0000-") {
  1041. return "1990-01-01"
  1042. }
  1043. return startDate
  1044. }
  1045. // GetEdbRefreshEndDate
  1046. // @Description: 获取结束刷新时间
  1047. // @author: Roc
  1048. // @datetime 2024-02-05 11:23:55
  1049. // @param startDate string
  1050. // @return string
  1051. func GetEdbRefreshEndDate(endDate string) string {
  1052. // 没有传入日期,或者日期异常的话,那么就是从1990-01-01开始
  1053. if endDate == `` || strings.Contains(endDate, "0000-") {
  1054. return time.Now().Format(FormatDate)
  1055. }
  1056. return endDate
  1057. }
  1058. func DealExcelDate(date string) (newDate time.Time, err error) {
  1059. /*newDate, err = dateparse.ParseAny(date)
  1060. if err != nil {
  1061. return
  1062. }
  1063. newDate = time.Date(newDate.Year(), newDate.Month(), newDate.Day(), 0, 0, 0, 0, time.Local)*/
  1064. if strings.Contains(date, "/") {
  1065. newDate, err = time.ParseInLocation("2006/01/02", date, time.Local)
  1066. return
  1067. } else {
  1068. newDate, err = time.ParseInLocation(FormatDate, date, time.Local)
  1069. return
  1070. }
  1071. return
  1072. }
  1073. // FloatAlmostEqual 判断两个浮点数是否相等
  1074. func FloatAlmostEqual(a, b float64) bool {
  1075. epsilon := 1e-9 // 容差值
  1076. return math.Abs(a-b) <= epsilon
  1077. }
  1078. // VerifyFrequency
  1079. // @Description: 校验频度是否合规
  1080. // @author: Roc
  1081. // @datetime 2024-04-26 13:30:22
  1082. // @param frequency string 待校验的频度
  1083. // @return bool
  1084. func VerifyFrequency(frequency string) bool {
  1085. return InArrayByStr([]string{"年度", "半年度", "季度", "月度", "旬度", "周度", "日度"}, frequency)
  1086. }
  1087. // DateConvMysqlConvMongo
  1088. // @Description: 将mysql中的日期比较符转换成mongo中的日期比较符
  1089. // @author: Roc
  1090. // @datetime 2024-05-08 11:03:26
  1091. // @param dateCon string
  1092. func DateConvMysqlConvMongo(dateCon string) string {
  1093. cond := ""
  1094. switch dateCon {
  1095. case "=":
  1096. cond = "$eq"
  1097. case "<":
  1098. cond = "$lt"
  1099. case "<=":
  1100. cond = "$lte"
  1101. case ">":
  1102. cond = "$gt"
  1103. case ">=":
  1104. cond = "$gte"
  1105. }
  1106. return cond
  1107. }
  1108. // GenerateEdbCodeMap 当前已经生成的指标编码map(暂时不做定时数据清理了,因为数据不大,我们至少每个月会重启一次,所以暂时不做定时数据清理)
  1109. var GenerateEdbCodeMap = map[string]bool{}
  1110. // GenerateEdbCode
  1111. // @Description: 生成指标编码
  1112. // @author: Roc
  1113. // @datetime 2024-06-05 09:49:53
  1114. // @param num int
  1115. // @param pre string 前缀
  1116. // @return edbCode string
  1117. // @return err error
  1118. func GenerateEdbCode(num int, pre string) (edbCode string, err error) {
  1119. if num >= 10 {
  1120. err = errors.New("指标编码生成失败,请重新生成")
  1121. return
  1122. }
  1123. // 4位随机数
  1124. randStr := GetRandDigit(4)
  1125. // 年月日时分秒+4位随机数
  1126. edbCode = `C` + pre + time.Now().Format(FormatShortDateTimeUnSpace) + randStr
  1127. if _, ok := GenerateEdbCodeMap[edbCode]; ok {
  1128. num++
  1129. edbCode, err = GenerateEdbCode(num, pre)
  1130. }
  1131. GenerateEdbCodeMap[edbCode] = true
  1132. return
  1133. }
  1134. // InsertStr2StrIdx 可分隔的字符串中插入指定字符串, 例如: CO1 Comdty插入V => CO1 V Comdty
  1135. func InsertStr2StrIdx(str, sep string, idx int, value string) string {
  1136. str = strings.TrimSpace(str)
  1137. // 默认以空格作为分隔符
  1138. if sep == "" {
  1139. sep = " "
  1140. }
  1141. slice := strings.Split(str, sep)
  1142. if len(slice) < 2 {
  1143. return str
  1144. }
  1145. // 如果idx不在切片的有效范围内,直接返回原字符串
  1146. if idx < 0 || idx > len(slice) {
  1147. return str
  1148. }
  1149. slice = append(slice[:idx], append([]string{value}, slice[idx:]...)...)
  1150. return strings.Join(slice, sep)
  1151. }
  1152. // FormatFloatPlaces 格式化浮点数位数
  1153. func FormatFloatPlaces(val float64, places int32) (newVal float64, err error) {
  1154. if places <= 0 {
  1155. places = 4
  1156. }
  1157. strNewVal := decimal.NewFromFloat(val).Round(places).String()
  1158. di, e := decimal.NewFromString(strNewVal)
  1159. if e != nil {
  1160. err = fmt.Errorf("NewFromString err: %v", e)
  1161. return
  1162. }
  1163. newVal, _ = di.Float64()
  1164. return
  1165. }
  1166. // handleSystemAppointDateT
  1167. // @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  1168. // @author: Roc
  1169. // @datetime2023-10-27 09:31:35
  1170. // @param Frequency string
  1171. // @param Day string
  1172. // @return date string
  1173. // @return err error
  1174. // @return errMsg string
  1175. func HandleSystemAppointDateT(currDate time.Time, appointDay, frequency string) (date string, err error, errMsg string) {
  1176. //currDate := time.Now()
  1177. switch frequency {
  1178. case "本周":
  1179. day := int(currDate.Weekday())
  1180. if day == 0 { // 周日
  1181. day = 7
  1182. }
  1183. num := 0
  1184. switch appointDay {
  1185. case "周一":
  1186. num = 1
  1187. case "周二":
  1188. num = 2
  1189. case "周三":
  1190. num = 3
  1191. case "周四":
  1192. num = 4
  1193. case "周五":
  1194. num = 5
  1195. case "周六":
  1196. num = 6
  1197. case "周日":
  1198. num = 7
  1199. }
  1200. day = num - day
  1201. date = currDate.AddDate(0, 0, day).Format(FormatDate)
  1202. case "本旬":
  1203. day := currDate.Day()
  1204. var tmpDate time.Time
  1205. switch appointDay {
  1206. case "第一天":
  1207. if day <= 10 {
  1208. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  1209. } else if day <= 20 {
  1210. tmpDate = time.Date(currDate.Year(), currDate.Month(), 11, 0, 0, 0, 0, currDate.Location())
  1211. } else {
  1212. tmpDate = time.Date(currDate.Year(), currDate.Month(), 21, 0, 0, 0, 0, currDate.Location())
  1213. }
  1214. case "最后一天":
  1215. if day <= 10 {
  1216. tmpDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, currDate.Location())
  1217. } else if day <= 20 {
  1218. tmpDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, currDate.Location())
  1219. } else {
  1220. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  1221. }
  1222. }
  1223. date = tmpDate.Format(FormatDate)
  1224. case "本月":
  1225. var tmpDate time.Time
  1226. switch appointDay {
  1227. case "第一天":
  1228. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  1229. case "最后一天":
  1230. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  1231. }
  1232. date = tmpDate.Format(FormatDate)
  1233. case "本季":
  1234. month := currDate.Month()
  1235. var tmpDate time.Time
  1236. switch appointDay {
  1237. case "第一天":
  1238. if month <= 3 {
  1239. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  1240. } else if month <= 6 {
  1241. tmpDate = time.Date(currDate.Year(), 4, 1, 0, 0, 0, 0, currDate.Location())
  1242. } else if month <= 9 {
  1243. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  1244. } else {
  1245. tmpDate = time.Date(currDate.Year(), 10, 1, 0, 0, 0, 0, currDate.Location())
  1246. }
  1247. case "最后一天":
  1248. if month <= 3 {
  1249. tmpDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, currDate.Location())
  1250. } else if month <= 6 {
  1251. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  1252. } else if month <= 9 {
  1253. tmpDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, currDate.Location())
  1254. } else {
  1255. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  1256. }
  1257. }
  1258. date = tmpDate.Format(FormatDate)
  1259. case "本半年":
  1260. month := currDate.Month()
  1261. var tmpDate time.Time
  1262. switch appointDay {
  1263. case "第一天":
  1264. if month <= 6 {
  1265. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  1266. } else {
  1267. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  1268. }
  1269. case "最后一天":
  1270. if month <= 6 {
  1271. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  1272. } else {
  1273. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  1274. }
  1275. }
  1276. date = tmpDate.Format(FormatDate)
  1277. case "本年":
  1278. var tmpDate time.Time
  1279. switch appointDay {
  1280. case "第一天":
  1281. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  1282. case "最后一天":
  1283. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  1284. }
  1285. date = tmpDate.Format(FormatDate)
  1286. default:
  1287. errMsg = "错误的日期频度:" + frequency
  1288. err = errors.New(errMsg)
  1289. return
  1290. }
  1291. return
  1292. }
  1293. func CompareFloatByOpStrings(op string, a, b float64) bool {
  1294. switch op {
  1295. case "=":
  1296. return a == b
  1297. case ">":
  1298. return a > b
  1299. case ">=":
  1300. return a >= b
  1301. case "<=":
  1302. return a <= b
  1303. case "<":
  1304. return a < b
  1305. }
  1306. return false
  1307. }
  1308. // IsDivideZero
  1309. // @Description: 判断是否分母为0的bug
  1310. // @author: Roc
  1311. // @datetime 2024-08-23 11:21:25
  1312. // @param err error
  1313. // @return bool
  1314. func IsDivideZero(err error) bool {
  1315. if err == nil {
  1316. return false
  1317. }
  1318. //if strings.Contains(err.Error(), "divide by zero") {
  1319. // return true
  1320. //}
  1321. if strings.Contains(err.Error(), "division by zero") {
  1322. return true
  1323. }
  1324. return false
  1325. }
  1326. // GetTradingDays 获取开始时间至结束时间之间的交易日期(日度)
  1327. func GetTradingDays(startDate, endDate time.Time) []time.Time {
  1328. var tradingDays []time.Time
  1329. for curr := startDate; !curr.After(endDate); curr = curr.AddDate(0, 0, 1) {
  1330. if curr.Weekday() >= time.Monday && curr.Weekday() <= time.Friday {
  1331. tradingDays = append(tradingDays, curr)
  1332. }
  1333. }
  1334. return tradingDays
  1335. }
  1336. // CalculateTradingDays 计算天数 跳过周末
  1337. func CalculateTradingDays(baseDate time.Time, tradingDays int, resultMap map[string]float64, moveType int) int {
  1338. oldDate := baseDate
  1339. // Move to the next day
  1340. var moveDays int
  1341. if moveType != 2 {
  1342. moveDays = tradingDays
  1343. } else {
  1344. moveDays = -tradingDays
  1345. }
  1346. daysMoved := 0 // 实际移动的工作日数
  1347. for daysMoved < tradingDays {
  1348. // 根据 moveType,决定是前进一天还是后退一天
  1349. if moveDays > 0 {
  1350. baseDate = baseDate.AddDate(0, 0, 1) // 向后移动一天
  1351. } else {
  1352. baseDate = baseDate.AddDate(0, 0, -1) // 向前移动一天
  1353. }
  1354. // 如果当前日期不是周六或周日,则计入交易日
  1355. weekday := baseDate.Weekday()
  1356. if weekday != time.Saturday && weekday != time.Sunday {
  1357. daysMoved++
  1358. }
  1359. }
  1360. // 计算实际天数差(包含跳过周末后的移动天数)
  1361. subDays := baseDate.Sub(oldDate)
  1362. days := int(math.Abs(subDays.Hours() / 24))
  1363. return days
  1364. }
  1365. // getLastDayOfMonth 获取某个月的最后一天
  1366. func getLastDayOfMonth(t time.Time) time.Time {
  1367. // 移动到下个月的第一天,然后回退一天得到当前月的最后一天
  1368. return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location()).AddDate(0, 1, -1)
  1369. }
  1370. // 获取某年某月的天数
  1371. func daysInMonth(year int, month time.Month) int {
  1372. if month == time.February {
  1373. // 闰年处理
  1374. if (year%4 == 0 && year%100 != 0) || (year%400 == 0) {
  1375. return 29
  1376. }
  1377. return 28
  1378. }
  1379. if month == time.April || month == time.June || month == time.September || month == time.November {
  1380. return 30
  1381. }
  1382. return 31
  1383. }
  1384. // CalculateEndOfMonth 使用天数计算未来月末的天数差
  1385. /*func CalculateEndOfMonth(baseDate time.Time, months, moveType int) int {
  1386. // 假设每个月28天,然后算到目标月的下个月
  1387. var daysToAdd int
  1388. // 计算目标月的下个月月初
  1389. if moveType == 2 {
  1390. daysToAdd = -(28 * months)
  1391. } else {
  1392. daysToAdd = 28 * (months + 2)
  1393. }
  1394. nextMonth := baseDate.AddDate(0, 0, daysToAdd)
  1395. // 获取目标月月初的第一天
  1396. firstDayOfNextMonth := time.Date(nextMonth.Year(), nextMonth.Month(), 1, 0, 0, 0, 0, nextMonth.Location())
  1397. // 获取目标月的最后一天(即月初减去1天)
  1398. lastDayOfTargetMonth := firstDayOfNextMonth.AddDate(0, 0, -1)
  1399. // 计算天数差
  1400. daysDifference := int(math.Abs(lastDayOfTargetMonth.Sub(baseDate).Hours() / 24))
  1401. return daysDifference
  1402. }*/
  1403. // CalculateEndOfMonth 计算从 baseDate 开始经过 months 后目标月的最后一天距离 baseDate 的天数差
  1404. func CalculateEndOfMonth(baseDate time.Time, months, moveType int) int {
  1405. // 初始化目标日期为当前日期
  1406. targetDate := baseDate
  1407. // 如果 moveType == 2,表示倒退月份;否则为前进月份
  1408. if moveType == 2 {
  1409. months = -months
  1410. }
  1411. // 手动通过天数加减月份
  1412. for i := 0; i < int(math.Abs(float64(months))); i++ {
  1413. // 首先将日期调整到当前月份的第一天
  1414. targetDate = time.Date(targetDate.Year(), targetDate.Month(), 1, 0, 0, 0, 0, targetDate.Location())
  1415. // 根据 moveType 来前进或倒退到下一个月的第一天
  1416. if months > 0 {
  1417. // 前进到下一个月的第一天
  1418. targetDate = targetDate.AddDate(0, 1, 0)
  1419. } else {
  1420. // 倒退到上一个月的第一天
  1421. targetDate = targetDate.AddDate(0, -1, 0)
  1422. }
  1423. // 如果是倒退,调整为目标月的最后一天
  1424. if months < 0 {
  1425. daysInCurrentMonth := daysInMonth(targetDate.Year(), targetDate.Month())
  1426. targetDate = time.Date(targetDate.Year(), targetDate.Month(), daysInCurrentMonth, 0, 0, 0, 0, targetDate.Location())
  1427. }
  1428. }
  1429. // 获取目标月的下个月月初第一天
  1430. firstDayOfNextMonth := time.Date(targetDate.Year(), targetDate.Month()+1, 1, 0, 0, 0, 0, targetDate.Location())
  1431. // 获取目标月的最后一天(即下个月月初减去一天)
  1432. lastDayOfTargetMonth := firstDayOfNextMonth.AddDate(0, 0, -1)
  1433. // 计算天数差
  1434. daysDifference := int(math.Abs(lastDayOfTargetMonth.Sub(baseDate).Hours() / 24))
  1435. return daysDifference
  1436. }
  1437. // CalculateDekadTime 计算旬度时间
  1438. func CalculateDekadTime(baseDate time.Time, tradingDays, moveType int) int {
  1439. // 记录原始日期
  1440. oldDate := baseDate
  1441. // 计算移动的旬数,1 旬为 10 天
  1442. var moveDekads int
  1443. if moveType != 2 {
  1444. moveDekads = tradingDays
  1445. } else {
  1446. moveDekads = -tradingDays
  1447. }
  1448. // 移动的天数为旬数 * 10,初步移动日期
  1449. baseDate = baseDate.AddDate(0, 0, moveDekads*10)
  1450. // 调整日期到最近的旬:10号、20号或月末
  1451. baseDate = adjustToNearestDekad(baseDate)
  1452. // 计算时间差
  1453. subDays := baseDate.Sub(oldDate)
  1454. days := int(math.Abs(subDays.Hours() / 24))
  1455. fmt.Printf("最终日期: %s, 总天数差: %d 天\n", baseDate.Format("2006-01-02"), days)
  1456. return days
  1457. }
  1458. // adjustToNearestDekad 调整日期到最近的旬
  1459. func adjustToNearestDekad(date time.Time) time.Time {
  1460. day := date.Day()
  1461. lastDayOfMonth := getLastDayOfMonth(date).Day()
  1462. // 这里有些无可奈何了,暂时这么写吧。。。需要跟据润 平年根据每个月进行单独处理
  1463. if day < 5 {
  1464. dateOneMonthAgo := date.AddDate(0, -1, 0)
  1465. lastDayOfMonth2 := getLastDayOfMonth(dateOneMonthAgo).Day()
  1466. return time.Date(date.Year(), date.Month()-1, lastDayOfMonth2, 0, 0, 0, 0, date.Location())
  1467. } else if day > 5 && day <= 15 {
  1468. return time.Date(date.Year(), date.Month(), 10, 0, 0, 0, 0, date.Location())
  1469. } else if day > 11 && day <= 25 {
  1470. return time.Date(date.Year(), date.Month(), 20, 0, 0, 0, 0, date.Location())
  1471. } else {
  1472. return time.Date(date.Year(), date.Month(), lastDayOfMonth, 0, 0, 0, 0, date.Location())
  1473. }
  1474. }
  1475. /*// getLastDayOfMonth 返回指定日期所在月份的最后一天
  1476. func getLastDayOfMonth(date time.Time) time.Time {
  1477. // 获取下个月的第一天
  1478. nextMonth := date.AddDate(0, 1, -date.Day()+1)
  1479. // 下个月第一天减去一天即为当前月的最后一天
  1480. lastDay := nextMonth.AddDate(0, 0, -1)
  1481. return lastDay
  1482. }*/
  1483. // CalculateEndOfQuarter 计算从当前到未来的季度末的天数差
  1484. func CalculateEndOfQuarter(baseDate time.Time, quarters, moveType int) int {
  1485. // Move forward `quarters` quarters (3 months per quarter)
  1486. return CalculateEndOfMonth(baseDate, quarters*3, moveType)
  1487. }
  1488. // CalculateEndOfYear 计算从当前到未来的年末的天数差
  1489. func CalculateEndOfYear(baseDate time.Time, years, moveType int) int {
  1490. // Move forward `years` years
  1491. return CalculateEndOfMonth(baseDate, years*12, moveType)
  1492. }
  1493. // CalculateEndOfHalfYear 计算从当前到未来的半年的天数差
  1494. func CalculateEndOfHalfYear(baseDate time.Time, years, moveType int) int {
  1495. // Move forward `half years` years
  1496. return CalculateEndOfMonth(baseDate, years*6, moveType)
  1497. }
  1498. // GetCurrentTime 获取当前时间 格式为 2024-08-07 15:29:58
  1499. func GetCurrentTime() string {
  1500. return time.Now().Format("2006-01-02 15:04:05")
  1501. }
  1502. // ParseDateTime 尝试解析不同的日期格式
  1503. func ParseDateTime(dateStr string) (time.Time, error) {
  1504. // 检查是否是只有年份的格式 "1971"
  1505. if len(dateStr) == 4 {
  1506. return parseYearOnlyDate(dateStr)
  1507. } else if strings.Contains(dateStr, "-") {
  1508. // 检查是否是季度格式 "Q1-1970"
  1509. if strings.HasPrefix(dateStr, "Q") {
  1510. return parseQuarterDate(dateStr)
  1511. }
  1512. // 检查是否是 "28-Jun-2019" 格式
  1513. if len(dateStr) > 9 {
  1514. return time.Parse("2-Jan-2006", dateStr)
  1515. }
  1516. } else {
  1517. excelDate, err := strconv.Atoi(dateStr)
  1518. if err != nil {
  1519. return time.Time{}, err
  1520. }
  1521. // 创建time.Time对象,这里使用的是UTC时区
  1522. t := time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC).AddDate(0, 0, excelDate)
  1523. return t, nil
  1524. }
  1525. return time.Time{}, fmt.Errorf("unsupported date format: %s", dateStr)
  1526. }
  1527. // parseQuarterDate 解析季度日期 "Q1-1970"
  1528. func parseQuarterDate(dateStr string) (time.Time, error) {
  1529. parts := strings.Split(dateStr, "-")
  1530. if len(parts) != 2 {
  1531. return time.Time{}, fmt.Errorf("invalid quarter date format: %s", dateStr)
  1532. }
  1533. q, err := strconv.Atoi(parts[0][1:])
  1534. if err != nil {
  1535. return time.Time{}, err
  1536. }
  1537. year, err := strconv.Atoi(parts[1])
  1538. if err != nil {
  1539. return time.Time{}, err
  1540. }
  1541. // 根据季度计算月份
  1542. month := time.January + time.Month((q-1)*3)
  1543. return time.Date(year, month, 1, 0, 0, 0, 0, time.UTC), nil
  1544. }
  1545. // parseYearOnlyDate 解析只有年份的日期 "1971"
  1546. func parseYearOnlyDate(dateStr string) (time.Time, error) {
  1547. year, err := strconv.Atoi(dateStr)
  1548. if err != nil {
  1549. return time.Time{}, err
  1550. }
  1551. return time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC), nil
  1552. }
  1553. // ReverseTimeSlice 反转时间类型切片
  1554. func ReverseTimeSlice(s []time.Time) []time.Time {
  1555. for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
  1556. s[i], s[j] = s[j], s[i]
  1557. }
  1558. return s
  1559. }
  1560. // ForwardPars
  1561. // @Description: 向前插入参数
  1562. // @param pars
  1563. // @param values
  1564. // @return []interface{}
  1565. func ForwardPars(pars []interface{}, values ...interface{}) []interface{} {
  1566. if len(values) <= 0 {
  1567. return pars
  1568. }
  1569. return append(values, pars...)
  1570. }
  1571. func IsErrNoRow(err error) bool {
  1572. if err == nil {
  1573. return false
  1574. }
  1575. return errors.Is(err, gorm.ErrRecordNotFound)
  1576. }
  1577. // GormDateStrToDateTimeStr
  1578. // @Description: gorm日期字符串格式转正常显示的日期时间字符串
  1579. // @param originalString
  1580. // @return formatStr
  1581. func GormDateStrToDateTimeStr(originalString string) (formatStr string) {
  1582. if originalString == `` {
  1583. return
  1584. }
  1585. if strings.Contains(originalString, "0001-01-01") {
  1586. return
  1587. }
  1588. formatStr = originalString
  1589. if !strings.Contains(originalString, "T") {
  1590. return
  1591. }
  1592. // 解析原始字符串
  1593. t, err := time.Parse(FormatDateWallWithLoc, originalString)
  1594. if err != nil {
  1595. fmt.Println("Error parsing time:", err)
  1596. return
  1597. }
  1598. // 重新格式化时间
  1599. formatStr = t.Format(FormatDateTime)
  1600. return
  1601. }
  1602. // GormDateStrToDateStr
  1603. // @Description: gorm日期字符串格式转正常显示的日期字符串
  1604. // @param originalString
  1605. // @return formatStr
  1606. func GormDateStrToDateStr(originalString string) (formatStr string) {
  1607. if originalString == `` {
  1608. return
  1609. }
  1610. if strings.Contains(originalString, "0001-01-01") {
  1611. return
  1612. }
  1613. formatStr = originalString
  1614. if !strings.Contains(originalString, "T") {
  1615. return
  1616. }
  1617. // 解析原始字符串
  1618. t, err := time.Parse(FormatDateWallWithLoc, originalString)
  1619. if err != nil {
  1620. fmt.Println("Error parsing time:", err)
  1621. return
  1622. }
  1623. // 重新格式化时间
  1624. formatStr = t.Format(FormatDate)
  1625. return
  1626. }
  1627. func FormatZhDateBak(dateTimeStr string) (newDateTimeStr string, err error) {
  1628. if dateTimeStr == `` {
  1629. return
  1630. }
  1631. if strings.Contains(dateTimeStr, "年") {
  1632. dateTimeStr = strings.ReplaceAll(dateTimeStr, "年", "-")
  1633. }
  1634. if strings.Contains(dateTimeStr, "月份") {
  1635. dateTimeStr = strings.ReplaceAll(dateTimeStr, "月份", "-")
  1636. dateTimeStrList := strings.Split(dateTimeStr, "-")
  1637. if len(dateTimeStrList) == 3 {
  1638. monthInt, tmpErr := strconv.Atoi(dateTimeStrList[1])
  1639. if tmpErr != nil {
  1640. return ``, tmpErr
  1641. }
  1642. if monthInt < 10 {
  1643. dateTimeStrList[1] = fmt.Sprint("0", dateTimeStrList[1])
  1644. dateTimeStr = strings.Join(dateTimeStrList, `-`)
  1645. }
  1646. }
  1647. } else if strings.Contains(dateTimeStr, "月") {
  1648. dateTimeStr = strings.ReplaceAll(dateTimeStr, "月", "-")
  1649. dateTimeStrList := strings.Split(dateTimeStr, "-")
  1650. if len(dateTimeStrList) == 3 {
  1651. monthInt, tmpErr := strconv.Atoi(dateTimeStrList[1])
  1652. if tmpErr != nil {
  1653. return ``, tmpErr
  1654. }
  1655. if monthInt < 10 {
  1656. dateTimeStrList[1] = fmt.Sprint("0", dateTimeStrList[1])
  1657. dateTimeStr = strings.Join(dateTimeStrList, `-`)
  1658. }
  1659. }
  1660. }
  1661. if strings.Contains(dateTimeStr, "上旬") {
  1662. dateTimeStr = strings.ReplaceAll(dateTimeStr, "上旬", "10")
  1663. } else if strings.Contains(dateTimeStr, "中旬") {
  1664. dateTimeStr = strings.ReplaceAll(dateTimeStr, "中旬", "20")
  1665. } else if strings.Contains(dateTimeStr, "本月") {
  1666. dateTimeStr = strings.ReplaceAll(dateTimeStr, "本月", "")
  1667. dateTime, tmpErr := time.ParseInLocation(FormatYearMonthDate, dateTimeStr, time.Local)
  1668. if tmpErr != nil {
  1669. err = tmpErr
  1670. return
  1671. }
  1672. dateTimeStr = time.Date(dateTime.Year(), dateTime.Month(), 1, 0, 0, 0, 0, time.Local).AddDate(0, 1, -1).Format(FormatDate)
  1673. }
  1674. return dateTimeStr, nil
  1675. }
  1676. func FormatZhDate(dateStr string) (newDateTimeStr string, err error) {
  1677. // Regular expression to extract year, month, and period
  1678. re := regexp.MustCompile(`(\d{4})年?(\d{1,2})月(?:份)?(上旬|中旬|本月)`)
  1679. matches := re.FindStringSubmatch(dateStr)
  1680. if len(matches) != 4 {
  1681. return "", fmt.Errorf("unsupported date string format: %s", dateStr)
  1682. }
  1683. year, err := strconv.Atoi(matches[1])
  1684. if err != nil {
  1685. return
  1686. }
  1687. month, err := strconv.Atoi(matches[2])
  1688. if err != nil {
  1689. return
  1690. }
  1691. period := matches[3]
  1692. var day int
  1693. switch period {
  1694. case "上旬":
  1695. day = 10
  1696. case "中旬":
  1697. day = 20
  1698. case "本月":
  1699. // Get the last day of the month
  1700. lastDayOfMonth := time.Date(year, time.Month(month+1), 0, 0, 0, 0, 0, time.UTC).Day()
  1701. day = lastDayOfMonth
  1702. default:
  1703. return "", fmt.Errorf("unsupported period in date string: %s", dateStr)
  1704. }
  1705. targetDate := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
  1706. return targetDate.Format(FormatDate), nil
  1707. }