common.go 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  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. "image"
  13. "image/png"
  14. "io"
  15. "math"
  16. "math/rand"
  17. "net"
  18. "net/http"
  19. "os"
  20. "os/exec"
  21. "path"
  22. "regexp"
  23. "runtime"
  24. "strconv"
  25. "strings"
  26. "time"
  27. "unicode"
  28. )
  29. // 随机数种子
  30. var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
  31. func GetRandString(size int) string {
  32. 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", "!", "@", "#", "$", "%", "^", "&", "*"}
  33. randomSb := ""
  34. digitSize := len(allLetterDigit)
  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. for i := 0; i < size; i++ {
  45. randomSb += allLetterDigit[rnd.Intn(digitSize)]
  46. }
  47. return randomSb
  48. }
  49. func StringsToJSON(str string) string {
  50. rs := []rune(str)
  51. jsons := ""
  52. for _, r := range rs {
  53. rint := int(r)
  54. if rint < 128 {
  55. jsons += string(r)
  56. } else {
  57. jsons += "\\u" + strconv.FormatInt(int64(rint), 16) // json
  58. }
  59. }
  60. return jsons
  61. }
  62. // 序列化
  63. func ToString(v interface{}) string {
  64. data, _ := json.Marshal(v)
  65. return string(data)
  66. }
  67. // md5加密
  68. func MD5(data string) string {
  69. m := md5.Sum([]byte(data))
  70. return hex.EncodeToString(m[:])
  71. }
  72. // 获取数字随机字符
  73. func GetRandDigit(n int) string {
  74. return fmt.Sprintf("%0"+strconv.Itoa(n)+"d", rnd.Intn(int(math.Pow10(n))))
  75. }
  76. // 获取随机数
  77. func GetRandNumber(n int) int {
  78. return rnd.Intn(n)
  79. }
  80. func GetRandInt(min, max int) int {
  81. if min >= max || min == 0 || max == 0 {
  82. return max
  83. }
  84. return rand.Intn(max-min) + min
  85. }
  86. func GetToday(format string) string {
  87. today := time.Now().Format(format)
  88. return today
  89. }
  90. // 获取今天剩余秒数
  91. func GetTodayLastSecond() time.Duration {
  92. today := GetToday(FormatDate) + " 23:59:59"
  93. end, _ := time.ParseInLocation(FormatDateTime, today, time.Local)
  94. return time.Duration(end.Unix()-time.Now().Local().Unix()) * time.Second
  95. }
  96. // 处理出生日期函数
  97. func GetBrithDate(idcard string) string {
  98. l := len(idcard)
  99. var s string
  100. if l == 15 {
  101. s = "19" + idcard[6:8] + "-" + idcard[8:10] + "-" + idcard[10:12]
  102. return s
  103. }
  104. if l == 18 {
  105. s = idcard[6:10] + "-" + idcard[10:12] + "-" + idcard[12:14]
  106. return s
  107. }
  108. return GetToday(FormatDate)
  109. }
  110. // 处理性别
  111. func WhichSexByIdcard(idcard string) string {
  112. var sexs = [2]string{"女", "男"}
  113. length := len(idcard)
  114. if length == 18 {
  115. sex, _ := strconv.Atoi(string(idcard[16]))
  116. return sexs[sex%2]
  117. } else if length == 15 {
  118. sex, _ := strconv.Atoi(string(idcard[14]))
  119. return sexs[sex%2]
  120. }
  121. return "男"
  122. }
  123. // 截取小数点后几位
  124. func SubFloatToString(f float64, m int) string {
  125. n := strconv.FormatFloat(f, 'f', -1, 64)
  126. if n == "" {
  127. return ""
  128. }
  129. if m >= len(n) {
  130. return n
  131. }
  132. newn := strings.Split(n, ".")
  133. if m == 0 {
  134. return newn[0]
  135. }
  136. if len(newn) < 2 || m >= len(newn[1]) {
  137. return n
  138. }
  139. return newn[0] + "." + newn[1][:m]
  140. }
  141. // 截取小数点后几位
  142. func SubFloatToFloat(f float64, m int) float64 {
  143. newn := SubFloatToString(f, m)
  144. newf, _ := strconv.ParseFloat(newn, 64)
  145. return newf
  146. }
  147. // 截取小数点后几位
  148. func SubFloatToFloatStr(f float64, m int) string {
  149. newn := SubFloatToString(f, m)
  150. return newn
  151. }
  152. // 获取相差时间-年
  153. func GetYearDiffer(start_time, end_time string) int {
  154. t1, _ := time.ParseInLocation("2006-01-02", start_time, time.Local)
  155. t2, _ := time.ParseInLocation("2006-01-02", end_time, time.Local)
  156. age := t2.Year() - t1.Year()
  157. if t2.Month() < t1.Month() || (t2.Month() == t1.Month() && t2.Day() < t1.Day()) {
  158. age--
  159. }
  160. return age
  161. }
  162. // 获取相差时间-秒
  163. func GetSecondDifferByTime(start_time, end_time time.Time) int64 {
  164. diff := end_time.Unix() - start_time.Unix()
  165. return diff
  166. }
  167. func FixFloat(f float64, m int) float64 {
  168. newn := SubFloatToString(f+0.00000001, m)
  169. newf, _ := strconv.ParseFloat(newn, 64)
  170. return newf
  171. }
  172. // 将字符串数组转化为逗号分割的字符串形式 ["str1","str2","str3"] >>> "str1,str2,str3"
  173. func StrListToString(strList []string) (str string) {
  174. if len(strList) > 0 {
  175. for k, v := range strList {
  176. if k == 0 {
  177. str = v
  178. } else {
  179. str = str + "," + v
  180. }
  181. }
  182. return
  183. }
  184. return ""
  185. }
  186. // Token
  187. func GetToken() string {
  188. randStr := GetRandString(64)
  189. token := MD5(randStr + Md5Key)
  190. tokenLen := 64 - len(token)
  191. return strings.ToUpper(token + GetRandString(tokenLen))
  192. }
  193. // 数据没有记录
  194. func ErrNoRow() string {
  195. return "<QuerySeter> no row found"
  196. }
  197. // 判断文件是否存在
  198. func FileIsExist(filePath string) bool {
  199. _, err := os.Stat(filePath)
  200. return err == nil || os.IsExist(err)
  201. }
  202. // 获取图片扩展名
  203. func GetImgExt(file string) (ext string, err error) {
  204. var headerByte []byte
  205. headerByte = make([]byte, 8)
  206. fd, err := os.Open(file)
  207. if err != nil {
  208. return "", err
  209. }
  210. defer fd.Close()
  211. _, err = fd.Read(headerByte)
  212. if err != nil {
  213. return "", err
  214. }
  215. xStr := fmt.Sprintf("%x", headerByte)
  216. switch {
  217. case xStr == "89504e470d0a1a0a":
  218. ext = ".png"
  219. case xStr == "0000010001002020":
  220. ext = ".ico"
  221. case xStr == "0000020001002020":
  222. ext = ".cur"
  223. case xStr[:12] == "474946383961" || xStr[:12] == "474946383761":
  224. ext = ".gif"
  225. case xStr[:10] == "0000020000" || xStr[:10] == "0000100000":
  226. ext = ".tga"
  227. case xStr[:8] == "464f524d":
  228. ext = ".iff"
  229. case xStr[:8] == "52494646":
  230. ext = ".ani"
  231. case xStr[:4] == "4d4d" || xStr[:4] == "4949":
  232. ext = ".tiff"
  233. case xStr[:4] == "424d":
  234. ext = ".bmp"
  235. case xStr[:4] == "ffd8":
  236. ext = ".jpg"
  237. case xStr[:2] == "0a":
  238. ext = ".pcx"
  239. default:
  240. ext = ""
  241. }
  242. return ext, nil
  243. }
  244. // 保存图片
  245. func SaveImage(path string, img image.Image) (err error) {
  246. //需要保持的文件
  247. imgfile, err := os.Create(path)
  248. defer imgfile.Close()
  249. // 以PNG格式保存文件
  250. err = png.Encode(imgfile, img)
  251. return err
  252. }
  253. // 下载图片
  254. func DownloadImage(imgUrl string) (filePath string, err error) {
  255. imgPath := "./static/imgs/"
  256. fileName := path.Base(imgUrl)
  257. res, err := http.Get(imgUrl)
  258. if err != nil {
  259. fmt.Println("A error occurred!")
  260. return
  261. }
  262. defer res.Body.Close()
  263. // 获得get请求响应的reader对象
  264. reader := bufio.NewReaderSize(res.Body, 32*1024)
  265. filePath = imgPath + fileName
  266. file, err := os.Create(filePath)
  267. if err != nil {
  268. return
  269. }
  270. // 获得文件的writer对象
  271. writer := bufio.NewWriter(file)
  272. written, _ := io.Copy(writer, reader)
  273. fmt.Printf("Total length: %d \n", written)
  274. return
  275. }
  276. // 保存base64数据为文件
  277. func SaveBase64ToFile(content, path string) error {
  278. data, err := base64.StdEncoding.DecodeString(content)
  279. if err != nil {
  280. return err
  281. }
  282. f, err := os.Create(path)
  283. defer f.Close()
  284. if err != nil {
  285. return err
  286. }
  287. f.Write(data)
  288. return nil
  289. }
  290. func SaveBase64ToFileBySeek(content, path string) (err error) {
  291. data, err := base64.StdEncoding.DecodeString(content)
  292. exist, err := PathExists(path)
  293. if err != nil {
  294. return
  295. }
  296. if !exist {
  297. f, err := os.Create(path)
  298. if err != nil {
  299. return err
  300. }
  301. n, _ := f.Seek(0, 2)
  302. // 从末尾的偏移量开始写入内容
  303. _, err = f.WriteAt([]byte(data), n)
  304. defer f.Close()
  305. } else {
  306. f, err := os.OpenFile(path, os.O_WRONLY, 0644)
  307. if err != nil {
  308. return err
  309. }
  310. n, _ := f.Seek(0, 2)
  311. // 从末尾的偏移量开始写入内容
  312. _, err = f.WriteAt([]byte(data), n)
  313. defer f.Close()
  314. }
  315. return nil
  316. }
  317. func PathExists(path string) (bool, error) {
  318. _, err := os.Stat(path)
  319. if err == nil {
  320. return true, nil
  321. }
  322. if os.IsNotExist(err) {
  323. return false, nil
  324. }
  325. return false, err
  326. }
  327. func StartIndex(page, pagesize int) int {
  328. if page > 1 {
  329. return (page - 1) * pagesize
  330. }
  331. return 0
  332. }
  333. func PageCount(count, pagesize int) int {
  334. if count%pagesize > 0 {
  335. return count/pagesize + 1
  336. } else {
  337. return count / pagesize
  338. }
  339. }
  340. func TrimHtml(src string) string {
  341. //将HTML标签全转换成小写
  342. re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
  343. src = re.ReplaceAllStringFunc(src, strings.ToLower)
  344. re, _ = regexp.Compile("\\<img[\\S\\s]+?\\>")
  345. src = re.ReplaceAllString(src, "[图片]")
  346. re, _ = regexp.Compile("class[\\S\\s]+?>")
  347. src = re.ReplaceAllString(src, "")
  348. re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
  349. src = re.ReplaceAllString(src, "")
  350. return strings.TrimSpace(src)
  351. }
  352. //1556164246 -> 2019-04-25 03:50:46 +0000
  353. //timestamp
  354. func TimeToTimestamp() {
  355. fmt.Println(time.Unix(1556164246, 0).Format("2006-01-02 15:04:05"))
  356. }
  357. func ToUnicode(text string) string {
  358. textQuoted := strconv.QuoteToASCII(text)
  359. textUnquoted := textQuoted[1 : len(textQuoted)-1]
  360. return textUnquoted
  361. }
  362. func VersionToInt(version string) int {
  363. version = strings.Replace(version, ".", "", -1)
  364. n, _ := strconv.Atoi(version)
  365. return n
  366. }
  367. func IsCheckInList(list []int, s int) bool {
  368. for _, v := range list {
  369. if v == s {
  370. return true
  371. }
  372. }
  373. return false
  374. }
  375. func round(num float64) int {
  376. return int(num + math.Copysign(0.5, num))
  377. }
  378. func toFixed(num float64, precision int) float64 {
  379. output := math.Pow(10, float64(precision))
  380. return float64(round(num*output)) / output
  381. }
  382. // GetWilsonScore returns Wilson Score
  383. func GetWilsonScore(p, n float64) float64 {
  384. if p == 0 && n == 0 {
  385. return 0
  386. }
  387. 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)
  388. }
  389. // 将中文数字转化成数字,比如 第三百四十五章,返回第345章 不支持一亿及以上
  390. func ChangeWordsToNum(str string) (numStr string) {
  391. words := ([]rune)(str)
  392. num := 0
  393. n := 0
  394. for i := 0; i < len(words); i++ {
  395. word := string(words[i : i+1])
  396. switch word {
  397. case "万":
  398. if n == 0 {
  399. n = 1
  400. }
  401. n = n * 10000
  402. num = num*10000 + n
  403. n = 0
  404. case "千":
  405. if n == 0 {
  406. n = 1
  407. }
  408. n = n * 1000
  409. num += n
  410. n = 0
  411. case "百":
  412. if n == 0 {
  413. n = 1
  414. }
  415. n = n * 100
  416. num += n
  417. n = 0
  418. case "十":
  419. if n == 0 {
  420. n = 1
  421. }
  422. n = n * 10
  423. num += n
  424. n = 0
  425. case "一":
  426. n += 1
  427. case "二":
  428. n += 2
  429. case "三":
  430. n += 3
  431. case "四":
  432. n += 4
  433. case "五":
  434. n += 5
  435. case "六":
  436. n += 6
  437. case "七":
  438. n += 7
  439. case "八":
  440. n += 8
  441. case "九":
  442. n += 9
  443. case "零":
  444. default:
  445. if n > 0 {
  446. num += n
  447. n = 0
  448. }
  449. if num == 0 {
  450. numStr += word
  451. } else {
  452. numStr += strconv.Itoa(num) + word
  453. num = 0
  454. }
  455. }
  456. }
  457. if n > 0 {
  458. num += n
  459. n = 0
  460. }
  461. if num != 0 {
  462. numStr += strconv.Itoa(num)
  463. }
  464. return
  465. }
  466. func Sha1(data string) string {
  467. sha1 := sha1.New()
  468. sha1.Write([]byte(data))
  469. return hex.EncodeToString(sha1.Sum([]byte("")))
  470. }
  471. func GetVideoPlaySeconds(videoPath string) (playSeconds float64, err error) {
  472. cmd := `ffmpeg -i ` + videoPath + ` 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//`
  473. out, err := exec.Command("bash", "-c", cmd).Output()
  474. if err != nil {
  475. return
  476. }
  477. outTimes := string(out)
  478. fmt.Println("outTimes:", outTimes)
  479. if outTimes != "" {
  480. timeArr := strings.Split(outTimes, ":")
  481. h := timeArr[0]
  482. m := timeArr[1]
  483. s := timeArr[2]
  484. hInt, err := strconv.Atoi(h)
  485. if err != nil {
  486. return playSeconds, err
  487. }
  488. mInt, err := strconv.Atoi(m)
  489. if err != nil {
  490. return playSeconds, err
  491. }
  492. s = strings.Trim(s, " ")
  493. s = strings.Trim(s, "\n")
  494. sInt, err := strconv.ParseFloat(s, 64)
  495. if err != nil {
  496. return playSeconds, err
  497. }
  498. playSeconds = float64(hInt)*3600 + float64(mInt)*60 + float64(sInt)
  499. }
  500. return
  501. }
  502. func GetMaxTradeCode(tradeCode string) (maxTradeCode string, err error) {
  503. tradeCode = strings.Replace(tradeCode, "W", "", -1)
  504. tradeCode = strings.Trim(tradeCode, " ")
  505. tradeCodeInt, err := strconv.Atoi(tradeCode)
  506. if err != nil {
  507. return
  508. }
  509. tradeCodeInt = tradeCodeInt + 1
  510. maxTradeCode = fmt.Sprintf("W%06d", tradeCodeInt)
  511. return
  512. }
  513. // excel日期字段格式化 yyyy-mm-dd
  514. func ConvertToFormatDay(excelDaysString string) string {
  515. // 2006-01-02 距离 1900-01-01的天数
  516. baseDiffDay := 38719 //在网上工具计算的天数需要加2天,什么原因没弄清楚
  517. curDiffDay := excelDaysString
  518. b, _ := strconv.Atoi(curDiffDay)
  519. // 获取excel的日期距离2006-01-02的天数
  520. realDiffDay := b - baseDiffDay
  521. //fmt.Println("realDiffDay:",realDiffDay)
  522. // 距离2006-01-02 秒数
  523. realDiffSecond := realDiffDay * 24 * 3600
  524. //fmt.Println("realDiffSecond:",realDiffSecond)
  525. // 2006-01-02 15:04:05距离1970-01-01 08:00:00的秒数 网上工具可查出
  526. baseOriginSecond := 1136185445
  527. resultTime := time.Unix(int64(baseOriginSecond+realDiffSecond), 0).Format("2006-01-02")
  528. return resultTime
  529. }
  530. func CheckPwd(pwd string) bool {
  531. compile := `([0-9a-z]+){6,12}|(a-z0-9]+){6,12}`
  532. reg := regexp.MustCompile(compile)
  533. flag := reg.MatchString(pwd)
  534. return flag
  535. }
  536. func GetMonthStartAndEnd(myYear string, myMonth string) (startDate, endDate string) {
  537. // 数字月份必须前置补零
  538. if len(myMonth) == 1 {
  539. myMonth = "0" + myMonth
  540. }
  541. yInt, _ := strconv.Atoi(myYear)
  542. timeLayout := "2006-01-02 15:04:05"
  543. loc, _ := time.LoadLocation("Local")
  544. theTime, _ := time.ParseInLocation(timeLayout, myYear+"-"+myMonth+"-01 00:00:00", loc)
  545. newMonth := theTime.Month()
  546. t1 := time.Date(yInt, newMonth, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  547. t2 := time.Date(yInt, newMonth+1, 0, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  548. return t1, t2
  549. }
  550. // 移除字符串中的空格
  551. func TrimStr(str string) (str2 string) {
  552. if str == "" {
  553. return str
  554. }
  555. return strings.Replace(str, " ", "", -1)
  556. }
  557. // 字符串转换为time
  558. func StrTimeToTime(strTime string) time.Time {
  559. timeLayout := "2006-01-02 15:04:05" //转化所需模板
  560. loc, _ := time.LoadLocation("Local") //重要:获取时区
  561. resultTime, _ := time.ParseInLocation(timeLayout, strTime, loc)
  562. return resultTime
  563. }
  564. // 字符串类型时间转周几
  565. func StrDateTimeToWeek(strTime string) string {
  566. var WeekDayMap = map[string]string{
  567. "Monday": "周一",
  568. "Tuesday": "周二",
  569. "Wednesday": "周三",
  570. "Thursday": "周四",
  571. "Friday": "周五",
  572. "Saturday": "周六",
  573. "Sunday": "周日",
  574. }
  575. var ctime = StrTimeToTime(strTime).Format("2006-01-02")
  576. startday, _ := time.ParseInLocation("2006-01-02", ctime, time.Local)
  577. staweek_int := startday.Weekday().String()
  578. return WeekDayMap[staweek_int]
  579. }
  580. // 时间格式转年月日字符串
  581. func TimeToStrYmd(time2 time.Time) string {
  582. var Ymd string
  583. year := time2.Year()
  584. month := time2.Format("1")
  585. day1 := time.Now().Day()
  586. Ymd = strconv.Itoa(year) + "年" + month + "月" + strconv.Itoa(day1) + "日"
  587. return Ymd
  588. }
  589. // 时间格式去掉时分秒
  590. func TimeRemoveHms(strTime string) string {
  591. var Ymd string
  592. var resultTime = StrTimeToTime(strTime)
  593. year := resultTime.Year()
  594. month := resultTime.Format("01")
  595. day1 := resultTime.Day()
  596. Ymd = strconv.Itoa(year) + "." + month + "." + strconv.Itoa(day1)
  597. return Ymd
  598. }
  599. // 时间格式去掉时分秒
  600. func TimeRemoveHms2(strTime string) string {
  601. var Ymd string
  602. var resultTime = StrTimeToTime(strTime)
  603. year := resultTime.Year()
  604. month := resultTime.Format("01")
  605. day1 := resultTime.Day()
  606. Ymd = strconv.Itoa(year) + "-" + month + "-" + strconv.Itoa(day1)
  607. return Ymd
  608. }
  609. // 文章上一次编辑时间
  610. func ArticleLastTime(strTime string) string {
  611. var newTime string
  612. stamp, _ := time.ParseInLocation("2006-01-02 15:04:05", strTime, time.Local)
  613. diffTime := time.Now().Unix() - stamp.Unix()
  614. if diffTime <= 60 {
  615. newTime = "当前"
  616. } else if diffTime < 60*60 {
  617. newTime = strconv.FormatInt(diffTime/60, 10) + "分钟前"
  618. } else if diffTime < 24*60*60 {
  619. newTime = strconv.FormatInt(diffTime/(60*60), 10) + "小时前"
  620. } else if diffTime < 30*24*60*60 {
  621. newTime = strconv.FormatInt(diffTime/(24*60*60), 10) + "天前"
  622. } else if diffTime < 12*30*24*60*60 {
  623. newTime = strconv.FormatInt(diffTime/(30*24*60*60), 10) + "月前"
  624. } else {
  625. newTime = "1年前"
  626. }
  627. return newTime
  628. }
  629. // 人民币小写转大写
  630. func ConvertNumToCny(num float64) (str string, err error) {
  631. strNum := strconv.FormatFloat(num*100, 'f', 0, 64)
  632. sliceUnit := []string{"仟", "佰", "拾", "亿", "仟", "佰", "拾", "万", "仟", "佰", "拾", "元", "角", "分"}
  633. // log.Println(sliceUnit[:len(sliceUnit)-2])
  634. s := sliceUnit[len(sliceUnit)-len(strNum):]
  635. upperDigitUnit := map[string]string{"0": "零", "1": "壹", "2": "贰", "3": "叁", "4": "肆", "5": "伍", "6": "陆", "7": "柒", "8": "捌", "9": "玖"}
  636. for k, v := range strNum[:] {
  637. str = str + upperDigitUnit[string(v)] + s[k]
  638. }
  639. reg, err := regexp.Compile(`零角零分$`)
  640. str = reg.ReplaceAllString(str, "整")
  641. reg, err = regexp.Compile(`零角`)
  642. str = reg.ReplaceAllString(str, "零")
  643. reg, err = regexp.Compile(`零分$`)
  644. str = reg.ReplaceAllString(str, "整")
  645. reg, err = regexp.Compile(`零[仟佰拾]`)
  646. str = reg.ReplaceAllString(str, "零")
  647. reg, err = regexp.Compile(`零{2,}`)
  648. str = reg.ReplaceAllString(str, "零")
  649. reg, err = regexp.Compile(`零亿`)
  650. str = reg.ReplaceAllString(str, "亿")
  651. reg, err = regexp.Compile(`零万`)
  652. str = reg.ReplaceAllString(str, "万")
  653. reg, err = regexp.Compile(`零*元`)
  654. str = reg.ReplaceAllString(str, "元")
  655. reg, err = regexp.Compile(`亿零{0, 3}万`)
  656. str = reg.ReplaceAllString(str, "^元")
  657. reg, err = regexp.Compile(`零元`)
  658. str = reg.ReplaceAllString(str, "零")
  659. return
  660. }
  661. // GetNowWeekMonday 获取本周周一的时间
  662. func GetNowWeekMonday() time.Time {
  663. offset := int(time.Monday - time.Now().Weekday())
  664. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  665. offset = -6
  666. }
  667. mondayTime := time.Now().AddDate(0, 0, offset)
  668. mondayTime = time.Date(mondayTime.Year(), mondayTime.Month(), mondayTime.Day(), 0, 0, 0, 0, mondayTime.Location())
  669. return mondayTime
  670. }
  671. // GetNowWeekLastDay 获取本周最后一天的时间
  672. func GetNowWeekLastDay() time.Time {
  673. offset := int(time.Monday - time.Now().Weekday())
  674. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  675. offset = -6
  676. }
  677. firstDayTime := time.Now().AddDate(0, 0, offset)
  678. firstDayTime = time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 0, 0, 0, 0, firstDayTime.Location()).AddDate(0, 0, 6)
  679. lastDayTime := time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 23, 59, 59, 0, firstDayTime.Location())
  680. return lastDayTime
  681. }
  682. // GetNowMonthFirstDay 获取本月第一天的时间
  683. func GetNowMonthFirstDay() time.Time {
  684. nowMonthFirstDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location())
  685. return nowMonthFirstDay
  686. }
  687. // GetNowMonthLastDay 获取本月最后一天的时间
  688. func GetNowMonthLastDay() time.Time {
  689. nowMonthLastDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  690. nowMonthLastDay = time.Date(nowMonthLastDay.Year(), nowMonthLastDay.Month(), nowMonthLastDay.Day(), 23, 59, 59, 0, nowMonthLastDay.Location())
  691. return nowMonthLastDay
  692. }
  693. // GetNowQuarterFirstDay 获取本季度第一天的时间
  694. func GetNowQuarterFirstDay() time.Time {
  695. month := int(time.Now().Month())
  696. var nowQuarterFirstDay time.Time
  697. if month >= 1 && month <= 3 {
  698. //1月1号
  699. nowQuarterFirstDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  700. } else if month >= 4 && month <= 6 {
  701. //4月1号
  702. nowQuarterFirstDay = time.Date(time.Now().Year(), 4, 1, 0, 0, 0, 0, time.Now().Location())
  703. } else if month >= 7 && month <= 9 {
  704. nowQuarterFirstDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
  705. } else {
  706. nowQuarterFirstDay = time.Date(time.Now().Year(), 10, 1, 0, 0, 0, 0, time.Now().Location())
  707. }
  708. return nowQuarterFirstDay
  709. }
  710. // GetNowQuarterLastDay 获取本季度最后一天的时间
  711. func GetNowQuarterLastDay() time.Time {
  712. month := int(time.Now().Month())
  713. var nowQuarterLastDay time.Time
  714. if month >= 1 && month <= 3 {
  715. //03-31 23:59:59
  716. nowQuarterLastDay = time.Date(time.Now().Year(), 3, 31, 23, 59, 59, 0, time.Now().Location())
  717. } else if month >= 4 && month <= 6 {
  718. //06-30 23:59:59
  719. nowQuarterLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
  720. } else if month >= 7 && month <= 9 {
  721. //09-30 23:59:59
  722. nowQuarterLastDay = time.Date(time.Now().Year(), 9, 30, 23, 59, 59, 0, time.Now().Location())
  723. } else {
  724. //12-31 23:59:59
  725. nowQuarterLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  726. }
  727. return nowQuarterLastDay
  728. }
  729. // GetNowHalfYearFirstDay 获取当前半年的第一天的时间
  730. func GetNowHalfYearFirstDay() time.Time {
  731. month := int(time.Now().Month())
  732. var nowHalfYearLastDay time.Time
  733. if month >= 1 && month <= 6 {
  734. //03-31 23:59:59
  735. nowHalfYearLastDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  736. } else {
  737. //12-31 23:59:59
  738. nowHalfYearLastDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
  739. }
  740. return nowHalfYearLastDay
  741. }
  742. // GetNowHalfYearLastDay 获取当前半年的最后一天的时间
  743. func GetNowHalfYearLastDay() time.Time {
  744. month := int(time.Now().Month())
  745. var nowHalfYearLastDay time.Time
  746. if month >= 1 && month <= 6 {
  747. //03-31 23:59:59
  748. nowHalfYearLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
  749. } else {
  750. //12-31 23:59:59
  751. nowHalfYearLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  752. }
  753. return nowHalfYearLastDay
  754. }
  755. // GetNowYearFirstDay 获取当前年的最后一天的时间
  756. func GetNowYearFirstDay() time.Time {
  757. //12-31 23:59:59
  758. nowYearFirstDay := time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  759. return nowYearFirstDay
  760. }
  761. // GetNowYearLastDay 获取当前年的最后一天的时间
  762. func GetNowYearLastDay() time.Time {
  763. //12-31 23:59:59
  764. nowYearLastDay := time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  765. return nowYearLastDay
  766. }
  767. // CalculationDate 计算两个日期之间相差n年m月y天
  768. // FormatPrice 格式化展示金额数字(财务金额展示,小数点前,每三位用,隔开) 1,234,567,898.55
  769. func FormatPrice(price float64) (str string) {
  770. str = decimal.NewFromFloat(price).String()
  771. length := len(str)
  772. if length < 4 {
  773. return str
  774. }
  775. arr := strings.Split(str, ".") //用小数点符号分割字符串,为数组接收
  776. length1 := len(arr[0])
  777. if length1 < 4 {
  778. return str
  779. }
  780. count := (length1 - 1) / 3
  781. for i := 0; i < count; i++ {
  782. arr[0] = arr[0][:length1-(i+1)*3] + "," + arr[0][length1-(i+1)*3:]
  783. }
  784. return strings.Join(arr, ".") //将一系列字符串连接为一个字符串,之间用sep来分隔。
  785. }
  786. // getMonthDay 获取某年某月有多少天
  787. func getMonthDay(year, month int) (days int) {
  788. if month != 2 {
  789. if month == 4 || month == 6 || month == 9 || month == 11 {
  790. days = 30
  791. } else {
  792. days = 31
  793. }
  794. } else {
  795. if ((year%4) == 0 && (year%100) != 0) || (year%400) == 0 {
  796. days = 29
  797. } else {
  798. days = 28
  799. }
  800. }
  801. return
  802. }
  803. func SaveToFile(content, path string) error {
  804. f, err := os.Create(path)
  805. defer f.Close()
  806. if err != nil {
  807. return err
  808. }
  809. f.Write([]byte(content))
  810. return nil
  811. }
  812. // HideString 给字段加***(从字符串中间替换,少于需要替换的长度,那么就补全*的长度)
  813. // src 待*字符串
  814. // hideLen 需要加*的长度
  815. func HideString(src string, hideLen int) string {
  816. if src == "" {
  817. return src
  818. }
  819. str := []rune(src)
  820. if hideLen == 0 {
  821. hideLen = 4
  822. }
  823. hideStr := ""
  824. for i := 0; i < hideLen; i++ {
  825. hideStr += "*"
  826. }
  827. strLen := len(str)
  828. // 字符长度是1
  829. if strLen == 1 {
  830. return string(str[:1]) + hideStr
  831. }
  832. //字符长度大于1,但是小于等于需要隐藏的字符长度,那么就隐藏中间,保留前后各一位字符
  833. if strLen <= hideLen+2 {
  834. return string(str[:1]) + hideStr + string(str[strLen-1:])
  835. }
  836. subLen := strLen - hideLen //剩余需要展示的字符长度
  837. decimal.NewFromFloat(2)
  838. frontLenDecimal := decimal.NewFromInt(int64(subLen)).Div(decimal.NewFromInt(2)) //前面需要展示的字符的长度
  839. frontLen := frontLenDecimal.Floor().IntPart()
  840. return string(str[:frontLen]) + hideStr + string(str[frontLen+int64(hideLen):])
  841. }
  842. // 用户参会时间转换
  843. func GetAttendanceDetailSeconds(secondNum int) string {
  844. var timeStr string
  845. if secondNum <= 60 {
  846. if secondNum < 10 {
  847. timeStr = "0" + strconv.Itoa(secondNum) + "''"
  848. } else {
  849. timeStr = strconv.Itoa(secondNum) + "''"
  850. }
  851. } else {
  852. var remainderStr string
  853. remainderNum := secondNum % 60
  854. minuteNum := secondNum / 60
  855. if remainderNum < 10 {
  856. remainderStr = "0" + strconv.Itoa(remainderNum) + "''"
  857. } else {
  858. remainderStr = strconv.Itoa(remainderNum) + "''"
  859. }
  860. if minuteNum < 10 {
  861. timeStr = "0" + strconv.Itoa(minuteNum) + "'" + remainderStr
  862. } else {
  863. timeStr = strconv.Itoa(minuteNum) + "'" + remainderStr
  864. }
  865. }
  866. return timeStr
  867. }
  868. // SubStr 截取字符串(中文)
  869. func SubStr(str string, subLen int) string {
  870. strRune := []rune(str)
  871. bodyRuneLen := len(strRune)
  872. if bodyRuneLen > subLen {
  873. bodyRuneLen = subLen
  874. }
  875. str = string(strRune[:bodyRuneLen])
  876. return str
  877. }
  878. func GetLocalIP() (ip string, err error) {
  879. addrs, err := net.InterfaceAddrs()
  880. if err != nil {
  881. return
  882. }
  883. for _, addr := range addrs {
  884. ipAddr, ok := addr.(*net.IPNet)
  885. if !ok {
  886. continue
  887. }
  888. if ipAddr.IP.IsLoopback() {
  889. continue
  890. }
  891. if !ipAddr.IP.IsGlobalUnicast() {
  892. continue
  893. }
  894. return ipAddr.IP.String(), nil
  895. }
  896. return
  897. }
  898. func PrintLog(params ...string) {
  899. _, file, line, ok := runtime.Caller(1)
  900. fmt.Println(file, line, ok, params)
  901. }
  902. // InArrayByStr php中的in_array(判断String类型的切片中是否存在该string值)
  903. func InArrayByStr(idStrList []string, searchId string) (has bool) {
  904. for _, id := range idStrList {
  905. if id == searchId {
  906. has = true
  907. return
  908. }
  909. }
  910. return
  911. }
  912. // InArrayByInt php中的in_array(判断Int类型的切片中是否存在该Int值)
  913. func InArrayByInt(idStrList []int, searchId int) (has bool) {
  914. for _, id := range idStrList {
  915. if id == searchId {
  916. has = true
  917. return
  918. }
  919. }
  920. return
  921. }
  922. // GetOrmInReplace 获取orm的in查询替换?的方法
  923. func GetOrmInReplace(num int) string {
  924. template := make([]string, num)
  925. for i := 0; i < num; i++ {
  926. template[i] = "?"
  927. }
  928. return strings.Join(template, ",")
  929. }
  930. // GetTimeSubDay 计算两个时间的自然日期差
  931. func GetTimeSubDay(t1, t2 time.Time) int {
  932. var day int
  933. swap := false
  934. if t1.Unix() > t2.Unix() {
  935. t1, t2 = t2, t1
  936. swap = true
  937. }
  938. t1_ := t1.Add(time.Duration(t2.Sub(t1).Milliseconds()%86400000) * time.Millisecond)
  939. day = int(t2.Sub(t1).Hours() / 24)
  940. // 计算在t1+两个时间的余数之后天数是否有变化
  941. if t1_.Day() != t1.Day() {
  942. day += 1
  943. }
  944. if swap {
  945. day = -day
  946. }
  947. return day
  948. }
  949. // GetFrequencyEndDay 根据当前时间和频度,获取该频度下最后一天的日期
  950. func GetFrequencyEndDay(currDate time.Time, frequency string) (endDate time.Time) {
  951. switch frequency {
  952. case "周度":
  953. // 如果当前就是最后一天,那么就直接返回本日期就好了
  954. if currDate.Weekday() == 0 {
  955. endDate = currDate
  956. } else {
  957. endDate = currDate.AddDate(0, 0, 7-int(currDate.Weekday()))
  958. }
  959. case "旬度":
  960. nextDay := currDate.AddDate(0, 0, 1)
  961. if nextDay.Day() == 1 || currDate.Day() == 10 || currDate.Day() == 20 {
  962. //如果是每月10、20、最后一天,那么就直接返回本日期就好了
  963. endDate = currDate
  964. } else {
  965. if currDate.Day() < 10 { // 每月10号
  966. endDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, time.Local)
  967. } else if currDate.Day() < 20 { // 每月10号
  968. endDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, time.Local)
  969. } else {
  970. // 下旬,多种可能,最大天数可能存在8天,9天,10天,11天,
  971. tmpNextMonth := currDate.AddDate(0, 0, 13)
  972. endDate = time.Date(tmpNextMonth.Year(), tmpNextMonth.Month(), 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  973. }
  974. }
  975. case "月度":
  976. nextDay := currDate.AddDate(0, 0, 1)
  977. if nextDay.Day() == 1 {
  978. //如果是每月的最后一天,那么就直接返回本日期就好了
  979. endDate = currDate
  980. } else {
  981. endDate = time.Date(nextDay.Year(), nextDay.Month()+1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  982. }
  983. case "季度":
  984. nextDay := currDate.AddDate(0, 0, 1)
  985. if (nextDay.Month() == 1 || nextDay.Month() == 4 || nextDay.Month() == 7 || nextDay.Month() == 10) && nextDay.Day() == 1 {
  986. //如果是每季的最后一天,那么就直接返回本日期就好了
  987. endDate = currDate
  988. } else {
  989. if currDate.Month() < 4 { // 1季度
  990. endDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, time.Local)
  991. } else if currDate.Month() < 7 { // 2季度
  992. endDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, time.Local)
  993. } else if currDate.Month() < 10 { // 3季度
  994. endDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, time.Local)
  995. } else {
  996. // 4季度
  997. endDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, time.Local)
  998. }
  999. }
  1000. case "年度":
  1001. endDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, time.Local)
  1002. default:
  1003. endDate = currDate
  1004. return
  1005. }
  1006. return
  1007. }
  1008. // CheckFrequency 获取两个频度之间是否相对低高频
  1009. // 大于0,代表左侧是高频(例:左侧:日度,右侧:周度)
  1010. // 等于0,代表同频
  1011. // 小于0,代表右侧是高频(例:左侧:周度,右侧:日度)
  1012. func CheckFrequency(leftFrequency, rightFrequency string) int {
  1013. frequencyMap := map[string]int{
  1014. "年度": 0,
  1015. "半年度": 1,
  1016. "季度": 2,
  1017. "月度": 3,
  1018. "旬度": 4,
  1019. "周度": 5,
  1020. "日度": 6,
  1021. }
  1022. return frequencyMap[leftFrequency] - frequencyMap[rightFrequency]
  1023. }
  1024. // 将下划线命名转为驼峰命名
  1025. func SnakeToCamel(s string) string {
  1026. var result string
  1027. upper := true
  1028. for _, c := range s {
  1029. if c == '_' {
  1030. upper = true
  1031. continue
  1032. }
  1033. if upper {
  1034. result += string(unicode.ToUpper(c))
  1035. upper = false
  1036. } else {
  1037. result += string(c)
  1038. }
  1039. }
  1040. return result
  1041. }
  1042. // 将驼峰命名转为下划线命名
  1043. func CamelToSnake(s string) string {
  1044. var result string
  1045. for i, c := range s {
  1046. if unicode.IsUpper(c) {
  1047. if i > 0 {
  1048. result += "_"
  1049. }
  1050. result += string(unicode.ToLower(c))
  1051. } else {
  1052. result += string(c)
  1053. }
  1054. }
  1055. return result
  1056. }
  1057. func TimeTransferString(format string, t time.Time) string {
  1058. str := t.Format(format)
  1059. if t.IsZero() {
  1060. return ""
  1061. }
  1062. return str
  1063. }
  1064. // GetEdbRefreshStartDate
  1065. // @Description: 获取开始刷新时间
  1066. // @author: Roc
  1067. // @datetime 2024-02-05 11:23:55
  1068. // @param startDate string
  1069. // @return string
  1070. func GetEdbRefreshStartDate(startDate string) string {
  1071. // 没有传入日期,或者日期异常的话,那么就是从1990-01-01开始
  1072. if startDate == `` || strings.Contains(startDate, "0000-") {
  1073. return "1990-01-01"
  1074. }
  1075. return startDate
  1076. }
  1077. // GetEdbRefreshEndDate
  1078. // @Description: 获取结束刷新时间
  1079. // @author: Roc
  1080. // @datetime 2024-02-05 11:23:55
  1081. // @param startDate string
  1082. // @return string
  1083. func GetEdbRefreshEndDate(endDate string) string {
  1084. // 没有传入日期,或者日期异常的话,那么就是从1990-01-01开始
  1085. if endDate == `` || strings.Contains(endDate, "0000-") {
  1086. return time.Now().Format(FormatDate)
  1087. }
  1088. return endDate
  1089. }
  1090. func DealExcelDate(date string) (newDate time.Time, err error) {
  1091. /*newDate, err = dateparse.ParseAny(date)
  1092. if err != nil {
  1093. return
  1094. }
  1095. newDate = time.Date(newDate.Year(), newDate.Month(), newDate.Day(), 0, 0, 0, 0, time.Local)*/
  1096. if strings.Contains(date, "/") {
  1097. newDate, err = time.ParseInLocation("2006/01/02", date, time.Local)
  1098. return
  1099. } else {
  1100. newDate, err = time.ParseInLocation(FormatDate, date, time.Local)
  1101. return
  1102. }
  1103. return
  1104. }
  1105. // FloatAlmostEqual 判断两个浮点数是否相等
  1106. func FloatAlmostEqual(a, b float64) bool {
  1107. epsilon := 1e-9 // 容差值
  1108. return math.Abs(a-b) <= epsilon
  1109. }
  1110. // VerifyFrequency
  1111. // @Description: 校验频度是否合规
  1112. // @author: Roc
  1113. // @datetime 2024-04-26 13:30:22
  1114. // @param frequency string 待校验的频度
  1115. // @return bool
  1116. func VerifyFrequency(frequency string) bool {
  1117. return InArrayByStr([]string{"年度", "半年度", "季度", "月度", "旬度", "周度", "日度"}, frequency)
  1118. }
  1119. // DateConvMysqlConvMongo
  1120. // @Description: 将mysql中的日期比较符转换成mongo中的日期比较符
  1121. // @author: Roc
  1122. // @datetime 2024-05-08 11:03:26
  1123. // @param dateCon string
  1124. func DateConvMysqlConvMongo(dateCon string) string {
  1125. cond := ""
  1126. switch dateCon {
  1127. case "=":
  1128. cond = "$eq"
  1129. case "<":
  1130. cond = "$lt"
  1131. case "<=":
  1132. cond = "$lte"
  1133. case ">":
  1134. cond = "$gt"
  1135. case ">=":
  1136. cond = "$gte"
  1137. }
  1138. return cond
  1139. }
  1140. // GenerateEdbCodeMap 当前已经生成的指标编码map(暂时不做定时数据清理了,因为数据不大,我们至少每个月会重启一次,所以暂时不做定时数据清理)
  1141. var GenerateEdbCodeMap = map[string]bool{}
  1142. // GenerateEdbCode
  1143. // @Description: 生成指标编码
  1144. // @author: Roc
  1145. // @datetime 2024-06-05 09:49:53
  1146. // @param num int
  1147. // @param pre string 前缀
  1148. // @return edbCode string
  1149. // @return err error
  1150. func GenerateEdbCode(num int, pre string) (edbCode string, err error) {
  1151. if num >= 10 {
  1152. err = errors.New("指标编码生成失败,请重新生成")
  1153. return
  1154. }
  1155. // 4位随机数
  1156. randStr := GetRandDigit(4)
  1157. // 年月日时分秒+4位随机数
  1158. edbCode = `C` + pre + time.Now().Format(FormatShortDateTimeUnSpace) + randStr
  1159. if _, ok := GenerateEdbCodeMap[edbCode]; ok {
  1160. num++
  1161. edbCode, err = GenerateEdbCode(num, pre)
  1162. }
  1163. GenerateEdbCodeMap[edbCode] = true
  1164. return
  1165. }
  1166. // InsertStr2StrIdx 可分隔的字符串中插入指定字符串, 例如: CO1 Comdty插入V => CO1 V Comdty
  1167. func InsertStr2StrIdx(str, sep string, idx int, value string) string {
  1168. str = strings.TrimSpace(str)
  1169. // 默认以空格作为分隔符
  1170. if sep == "" {
  1171. sep = " "
  1172. }
  1173. slice := strings.Split(str, sep)
  1174. if len(slice) < 2 {
  1175. return str
  1176. }
  1177. // 如果idx不在切片的有效范围内,直接返回原字符串
  1178. if idx < 0 || idx > len(slice) {
  1179. return str
  1180. }
  1181. slice = append(slice[:idx], append([]string{value}, slice[idx:]...)...)
  1182. return strings.Join(slice, sep)
  1183. }
  1184. // FormatFloatPlaces 格式化浮点数位数
  1185. func FormatFloatPlaces(val float64, places int32) (newVal float64, err error) {
  1186. if places <= 0 {
  1187. places = 4
  1188. }
  1189. strNewVal := decimal.NewFromFloat(val).Round(places).String()
  1190. di, e := decimal.NewFromString(strNewVal)
  1191. if e != nil {
  1192. err = fmt.Errorf("NewFromString err: %v", e)
  1193. return
  1194. }
  1195. newVal, _ = di.Float64()
  1196. return
  1197. }
  1198. // handleSystemAppointDateT
  1199. // @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  1200. // @author: Roc
  1201. // @datetime2023-10-27 09:31:35
  1202. // @param Frequency string
  1203. // @param Day string
  1204. // @return date string
  1205. // @return err error
  1206. // @return errMsg string
  1207. func HandleSystemAppointDateT(currDate time.Time, appointDay, frequency string) (date string, err error, errMsg string) {
  1208. //currDate := time.Now()
  1209. switch frequency {
  1210. case "本周":
  1211. day := int(currDate.Weekday())
  1212. if day == 0 { // 周日
  1213. day = 7
  1214. }
  1215. num := 0
  1216. switch appointDay {
  1217. case "周一":
  1218. num = 1
  1219. case "周二":
  1220. num = 2
  1221. case "周三":
  1222. num = 3
  1223. case "周四":
  1224. num = 4
  1225. case "周五":
  1226. num = 5
  1227. case "周六":
  1228. num = 6
  1229. case "周日":
  1230. num = 7
  1231. }
  1232. day = num - day
  1233. date = currDate.AddDate(0, 0, day).Format(FormatDate)
  1234. case "本旬":
  1235. day := currDate.Day()
  1236. var tmpDate time.Time
  1237. switch appointDay {
  1238. case "第一天":
  1239. if day <= 10 {
  1240. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  1241. } else if day <= 20 {
  1242. tmpDate = time.Date(currDate.Year(), currDate.Month(), 11, 0, 0, 0, 0, currDate.Location())
  1243. } else {
  1244. tmpDate = time.Date(currDate.Year(), currDate.Month(), 21, 0, 0, 0, 0, currDate.Location())
  1245. }
  1246. case "最后一天":
  1247. if day <= 10 {
  1248. tmpDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, currDate.Location())
  1249. } else if day <= 20 {
  1250. tmpDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, currDate.Location())
  1251. } else {
  1252. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  1253. }
  1254. }
  1255. date = tmpDate.Format(FormatDate)
  1256. case "本月":
  1257. var tmpDate time.Time
  1258. switch appointDay {
  1259. case "第一天":
  1260. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  1261. case "最后一天":
  1262. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  1263. }
  1264. date = tmpDate.Format(FormatDate)
  1265. case "本季":
  1266. month := currDate.Month()
  1267. var tmpDate time.Time
  1268. switch appointDay {
  1269. case "第一天":
  1270. if month <= 3 {
  1271. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  1272. } else if month <= 6 {
  1273. tmpDate = time.Date(currDate.Year(), 4, 1, 0, 0, 0, 0, currDate.Location())
  1274. } else if month <= 9 {
  1275. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  1276. } else {
  1277. tmpDate = time.Date(currDate.Year(), 10, 1, 0, 0, 0, 0, currDate.Location())
  1278. }
  1279. case "最后一天":
  1280. if month <= 3 {
  1281. tmpDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, currDate.Location())
  1282. } else if month <= 6 {
  1283. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  1284. } else if month <= 9 {
  1285. tmpDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, currDate.Location())
  1286. } else {
  1287. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  1288. }
  1289. }
  1290. date = tmpDate.Format(FormatDate)
  1291. case "本半年":
  1292. month := currDate.Month()
  1293. var tmpDate time.Time
  1294. switch appointDay {
  1295. case "第一天":
  1296. if month <= 6 {
  1297. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  1298. } else {
  1299. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  1300. }
  1301. case "最后一天":
  1302. if month <= 6 {
  1303. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  1304. } else {
  1305. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  1306. }
  1307. }
  1308. date = tmpDate.Format(FormatDate)
  1309. case "本年":
  1310. var tmpDate time.Time
  1311. switch appointDay {
  1312. case "第一天":
  1313. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  1314. case "最后一天":
  1315. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  1316. }
  1317. date = tmpDate.Format(FormatDate)
  1318. default:
  1319. errMsg = "错误的日期频度:" + frequency
  1320. err = errors.New(errMsg)
  1321. return
  1322. }
  1323. return
  1324. }
  1325. func CompareFloatByOpStrings(op string, a, b float64) bool {
  1326. switch op {
  1327. case "=":
  1328. return a == b
  1329. case ">":
  1330. return a > b
  1331. case ">=":
  1332. return a >= b
  1333. case "<=":
  1334. return a <= b
  1335. case "<":
  1336. return a < b
  1337. }
  1338. return false
  1339. }
  1340. // IsDivideZero
  1341. // @Description: 判断是否分母为0的bug
  1342. // @author: Roc
  1343. // @datetime 2024-08-23 11:21:25
  1344. // @param err error
  1345. // @return bool
  1346. func IsDivideZero(err error) bool {
  1347. if err == nil {
  1348. return false
  1349. }
  1350. //if strings.Contains(err.Error(), "divide by zero") {
  1351. // return true
  1352. //}
  1353. if strings.Contains(err.Error(), "division by zero") {
  1354. return true
  1355. }
  1356. return false
  1357. }
  1358. // GetTradingDays 获取开始时间至结束时间之间的交易日期(日度)
  1359. func GetTradingDays(startDate, endDate time.Time) []time.Time {
  1360. var tradingDays []time.Time
  1361. for curr := startDate; !curr.After(endDate); curr = curr.AddDate(0, 0, 1) {
  1362. if curr.Weekday() >= time.Monday && curr.Weekday() <= time.Friday {
  1363. tradingDays = append(tradingDays, curr)
  1364. }
  1365. }
  1366. return tradingDays
  1367. }