common.go 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429
  1. package utils
  2. import (
  3. "bufio"
  4. "bytes"
  5. "crypto/hmac"
  6. "crypto/md5"
  7. "crypto/sha1"
  8. "encoding/base64"
  9. "encoding/hex"
  10. "encoding/json"
  11. "errors"
  12. "fmt"
  13. "github.com/PuerkitoBio/goquery"
  14. "github.com/shopspring/decimal"
  15. "html"
  16. "image"
  17. "image/png"
  18. "io"
  19. "math"
  20. "math/rand"
  21. "net"
  22. "net/http"
  23. "os"
  24. "os/exec"
  25. "path"
  26. "regexp"
  27. "strconv"
  28. "strings"
  29. "time"
  30. )
  31. // 随机数种子
  32. var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
  33. func GetRandString(size int) string {
  34. 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", "!", "@", "#", "$", "%", "^", "&", "*"}
  35. randomSb := ""
  36. digitSize := len(allLetterDigit)
  37. for i := 0; i < size; i++ {
  38. randomSb += allLetterDigit[rnd.Intn(digitSize)]
  39. }
  40. return randomSb
  41. }
  42. func GetRandStringNoSpecialChar(size int) string {
  43. 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"}
  44. randomSb := ""
  45. digitSize := len(allLetterDigit)
  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. // HmacMd5 HmacMd5加密
  75. func HmacMd5(key, data string) string {
  76. h := hmac.New(md5.New, []byte(key))
  77. h.Write([]byte(data))
  78. return hex.EncodeToString(h.Sum([]byte("")))
  79. }
  80. // 获取数字随机字符
  81. func GetRandDigit(n int) string {
  82. return fmt.Sprintf("%0"+strconv.Itoa(n)+"d", rnd.Intn(int(math.Pow10(n))))
  83. }
  84. // 获取随机数
  85. func GetRandNumber(n int) int {
  86. return rnd.Intn(n)
  87. }
  88. func GetRandInt(min, max int) int {
  89. if min >= max || min == 0 || max == 0 {
  90. return max
  91. }
  92. return rand.Intn(max-min) + min
  93. }
  94. func GetToday(format string) string {
  95. today := time.Now().Format(format)
  96. return today
  97. }
  98. // 获取今天剩余秒数
  99. func GetTodayLastSecond() time.Duration {
  100. today := GetToday(FormatDate) + " 23:59:59"
  101. end, _ := time.ParseInLocation(FormatDateTime, today, time.Local)
  102. return time.Duration(end.Unix()-time.Now().Local().Unix()) * time.Second
  103. }
  104. // 处理出生日期函数
  105. func GetBrithDate(idcard string) string {
  106. l := len(idcard)
  107. var s string
  108. if l == 15 {
  109. s = "19" + idcard[6:8] + "-" + idcard[8:10] + "-" + idcard[10:12]
  110. return s
  111. }
  112. if l == 18 {
  113. s = idcard[6:10] + "-" + idcard[10:12] + "-" + idcard[12:14]
  114. return s
  115. }
  116. return GetToday(FormatDate)
  117. }
  118. // 处理性别
  119. func WhichSexByIdcard(idcard string) string {
  120. var sexs = [2]string{"女", "男"}
  121. length := len(idcard)
  122. if length == 18 {
  123. sex, _ := strconv.Atoi(string(idcard[16]))
  124. return sexs[sex%2]
  125. } else if length == 15 {
  126. sex, _ := strconv.Atoi(string(idcard[14]))
  127. return sexs[sex%2]
  128. }
  129. return "男"
  130. }
  131. // 截取小数点后几位
  132. func SubFloatToString(f float64, m int) string {
  133. n := strconv.FormatFloat(f, 'f', -1, 64)
  134. if n == "" {
  135. return ""
  136. }
  137. if m >= len(n) {
  138. return n
  139. }
  140. newn := strings.Split(n, ".")
  141. if m == 0 {
  142. return newn[0]
  143. }
  144. if len(newn) < 2 || m >= len(newn[1]) {
  145. return n
  146. }
  147. return newn[0] + "." + newn[1][:m]
  148. }
  149. // 截取小数点后几位
  150. func SubFloatToFloat(f float64, m int) float64 {
  151. newn := SubFloatToString(f, m)
  152. newf, _ := strconv.ParseFloat(newn, 64)
  153. return newf
  154. }
  155. // 截取小数点后几位
  156. func SubFloatToFloatStr(f float64, m int) string {
  157. newn := SubFloatToString(f, m)
  158. return newn
  159. }
  160. // 获取相差时间-年
  161. func GetYearDiffer(start_time, end_time string) int {
  162. t1, _ := time.ParseInLocation("2006-01-02", start_time, time.Local)
  163. t2, _ := time.ParseInLocation("2006-01-02", end_time, time.Local)
  164. age := t2.Year() - t1.Year()
  165. if t2.Month() < t1.Month() || (t2.Month() == t1.Month() && t2.Day() < t1.Day()) {
  166. age--
  167. }
  168. return age
  169. }
  170. // 获取相差时间-秒
  171. func GetSecondDifferByTime(start_time, end_time time.Time) int64 {
  172. diff := end_time.Unix() - start_time.Unix()
  173. return diff
  174. }
  175. func FixFloat(f float64, m int) float64 {
  176. newn := SubFloatToString(f+0.00000001, m)
  177. newf, _ := strconv.ParseFloat(newn, 64)
  178. return newf
  179. }
  180. // 将字符串数组转化为逗号分割的字符串形式 ["str1","str2","str3"] >>> "str1,str2,str3"
  181. func StrListToString(strList []string) (str string) {
  182. if len(strList) > 0 {
  183. for k, v := range strList {
  184. if k == 0 {
  185. str = v
  186. } else {
  187. str = str + "," + v
  188. }
  189. }
  190. return
  191. }
  192. return ""
  193. }
  194. // Token
  195. func GetToken() string {
  196. randStr := GetRandString(64)
  197. token := MD5(randStr + Md5Key)
  198. tokenLen := 64 - len(token)
  199. return strings.ToUpper(token + GetRandString(tokenLen))
  200. }
  201. // 数据没有记录
  202. func ErrNoRow() string {
  203. return "<QuerySeter> no row found"
  204. }
  205. // 校验邮箱格式
  206. func ValidateEmailFormatat(email string) bool {
  207. reg := regexp.MustCompile(RegularEmail)
  208. return reg.MatchString(email)
  209. }
  210. // 验证是否是手机号
  211. func ValidateMobileFormatat(mobileNum string) bool {
  212. reg := regexp.MustCompile(RegularMobile)
  213. return reg.MatchString(mobileNum)
  214. }
  215. // 判断文件是否存在
  216. func FileIsExist(filePath string) bool {
  217. _, err := os.Stat(filePath)
  218. return err == nil || os.IsExist(err)
  219. }
  220. // 获取图片扩展名
  221. func GetImgExt(file string) (ext string, err error) {
  222. var headerByte []byte
  223. headerByte = make([]byte, 8)
  224. fd, err := os.Open(file)
  225. if err != nil {
  226. return "", err
  227. }
  228. defer fd.Close()
  229. _, err = fd.Read(headerByte)
  230. if err != nil {
  231. return "", err
  232. }
  233. xStr := fmt.Sprintf("%x", headerByte)
  234. switch {
  235. case xStr == "89504e470d0a1a0a":
  236. ext = ".png"
  237. case xStr == "0000010001002020":
  238. ext = ".ico"
  239. case xStr == "0000020001002020":
  240. ext = ".cur"
  241. case xStr[:12] == "474946383961" || xStr[:12] == "474946383761":
  242. ext = ".gif"
  243. case xStr[:10] == "0000020000" || xStr[:10] == "0000100000":
  244. ext = ".tga"
  245. case xStr[:8] == "464f524d":
  246. ext = ".iff"
  247. case xStr[:8] == "52494646":
  248. ext = ".ani"
  249. case xStr[:4] == "4d4d" || xStr[:4] == "4949":
  250. ext = ".tiff"
  251. case xStr[:4] == "424d":
  252. ext = ".bmp"
  253. case xStr[:4] == "ffd8":
  254. ext = ".jpg"
  255. case xStr[:2] == "0a":
  256. ext = ".pcx"
  257. default:
  258. ext = ""
  259. }
  260. return ext, nil
  261. }
  262. // 保存图片
  263. func SaveImage(path string, img image.Image) (err error) {
  264. //需要保持的文件
  265. imgfile, err := os.Create(path)
  266. defer imgfile.Close()
  267. // 以PNG格式保存文件
  268. err = png.Encode(imgfile, img)
  269. return err
  270. }
  271. // 下载图片
  272. func DownloadImage(imgUrl string) (filePath string, err error) {
  273. imgPath := "./static/imgs/"
  274. fileName := path.Base(imgUrl)
  275. res, err := http.Get(imgUrl)
  276. if err != nil {
  277. fmt.Println("A error occurred!")
  278. return
  279. }
  280. defer res.Body.Close()
  281. // 获得get请求响应的reader对象
  282. reader := bufio.NewReaderSize(res.Body, 32*1024)
  283. filePath = imgPath + fileName
  284. file, err := os.Create(filePath)
  285. if err != nil {
  286. return
  287. }
  288. // 获得文件的writer对象
  289. writer := bufio.NewWriter(file)
  290. written, _ := io.Copy(writer, reader)
  291. fmt.Printf("Total length: %d \n", written)
  292. return
  293. }
  294. // 保存base64数据为文件
  295. func SaveBase64ToFile(content, path string) error {
  296. data, err := base64.StdEncoding.DecodeString(content)
  297. if err != nil {
  298. return err
  299. }
  300. f, err := os.Create(path)
  301. defer f.Close()
  302. if err != nil {
  303. return err
  304. }
  305. f.Write(data)
  306. return nil
  307. }
  308. func SaveBase64ToFileBySeek(content, path string) (err error) {
  309. data, err := base64.StdEncoding.DecodeString(content)
  310. exist, err := PathExists(path)
  311. if err != nil {
  312. return
  313. }
  314. if !exist {
  315. f, err := os.Create(path)
  316. if err != nil {
  317. return err
  318. }
  319. n, _ := f.Seek(0, 2)
  320. // 从末尾的偏移量开始写入内容
  321. _, err = f.WriteAt([]byte(data), n)
  322. defer f.Close()
  323. } else {
  324. f, err := os.OpenFile(path, os.O_WRONLY, 0644)
  325. if err != nil {
  326. return err
  327. }
  328. n, _ := f.Seek(0, 2)
  329. // 从末尾的偏移量开始写入内容
  330. _, err = f.WriteAt([]byte(data), n)
  331. defer f.Close()
  332. }
  333. return nil
  334. }
  335. func PathExists(path string) (bool, error) {
  336. _, err := os.Stat(path)
  337. if err == nil {
  338. return true, nil
  339. }
  340. if os.IsNotExist(err) {
  341. return false, nil
  342. }
  343. return false, err
  344. }
  345. func StartIndex(page, pagesize int) int {
  346. if page > 1 {
  347. return (page - 1) * pagesize
  348. }
  349. return 0
  350. }
  351. func PageCount(count, pagesize int) int {
  352. if count%pagesize > 0 {
  353. return count/pagesize + 1
  354. } else {
  355. return count / pagesize
  356. }
  357. }
  358. func TrimHtml(src string) string {
  359. //将HTML标签全转换成小写
  360. re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
  361. src = re.ReplaceAllStringFunc(src, strings.ToLower)
  362. re, _ = regexp.Compile("\\<img[\\S\\s]+?\\>")
  363. src = re.ReplaceAllString(src, "")
  364. re, _ = regexp.Compile("class[\\S\\s]+?>")
  365. src = re.ReplaceAllString(src, "")
  366. re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
  367. src = re.ReplaceAllString(src, "")
  368. src = strings.ReplaceAll(src, "&nbsp;", " ")
  369. return strings.TrimSpace(src)
  370. }
  371. //1556164246 -> 2019-04-25 03:50:46 +0000
  372. //timestamp
  373. func TimeToTimestamp() {
  374. fmt.Println(time.Unix(1556164246, 0).Format("2006-01-02 15:04:05"))
  375. }
  376. func ToUnicode(text string) string {
  377. textQuoted := strconv.QuoteToASCII(text)
  378. textUnquoted := textQuoted[1 : len(textQuoted)-1]
  379. return textUnquoted
  380. }
  381. func VersionToInt(version string) int {
  382. version = strings.Replace(version, ".", "", -1)
  383. n, _ := strconv.Atoi(version)
  384. return n
  385. }
  386. func IsCheckInList(list []int, s int) bool {
  387. for _, v := range list {
  388. if v == s {
  389. return true
  390. }
  391. }
  392. return false
  393. }
  394. func round(num float64) int {
  395. return int(num + math.Copysign(0.5, num))
  396. }
  397. func toFixed(num float64, precision int) float64 {
  398. output := math.Pow(10, float64(precision))
  399. return float64(round(num*output)) / output
  400. }
  401. // GetWilsonScore returns Wilson Score
  402. func GetWilsonScore(p, n float64) float64 {
  403. if p == 0 && n == 0 {
  404. return 0
  405. }
  406. 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)
  407. }
  408. // 将中文数字转化成数字,比如 第三百四十五章,返回第345章 不支持一亿及以上
  409. func ChangeWordsToNum(str string) (numStr string) {
  410. words := ([]rune)(str)
  411. num := 0
  412. n := 0
  413. for i := 0; i < len(words); i++ {
  414. word := string(words[i : i+1])
  415. switch word {
  416. case "万":
  417. if n == 0 {
  418. n = 1
  419. }
  420. n = n * 10000
  421. num = num*10000 + n
  422. n = 0
  423. case "千":
  424. if n == 0 {
  425. n = 1
  426. }
  427. n = n * 1000
  428. num += n
  429. n = 0
  430. case "百":
  431. if n == 0 {
  432. n = 1
  433. }
  434. n = n * 100
  435. num += n
  436. n = 0
  437. case "十":
  438. if n == 0 {
  439. n = 1
  440. }
  441. n = n * 10
  442. num += n
  443. n = 0
  444. case "一":
  445. n += 1
  446. case "二":
  447. n += 2
  448. case "三":
  449. n += 3
  450. case "四":
  451. n += 4
  452. case "五":
  453. n += 5
  454. case "六":
  455. n += 6
  456. case "七":
  457. n += 7
  458. case "八":
  459. n += 8
  460. case "九":
  461. n += 9
  462. case "零":
  463. default:
  464. if n > 0 {
  465. num += n
  466. n = 0
  467. }
  468. if num == 0 {
  469. numStr += word
  470. } else {
  471. numStr += strconv.Itoa(num) + word
  472. num = 0
  473. }
  474. }
  475. }
  476. if n > 0 {
  477. num += n
  478. n = 0
  479. }
  480. if num != 0 {
  481. numStr += strconv.Itoa(num)
  482. }
  483. return
  484. }
  485. func Sha1(data string) string {
  486. sha1 := sha1.New()
  487. sha1.Write([]byte(data))
  488. return hex.EncodeToString(sha1.Sum([]byte("")))
  489. }
  490. func GetVideoPlaySeconds(videoPath string) (playSeconds float64, err error) {
  491. cmd := `ffmpeg -i ` + videoPath + ` 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//`
  492. out, err := exec.Command("bash", "-c", cmd).Output()
  493. if err != nil {
  494. return
  495. }
  496. outTimes := string(out)
  497. fmt.Println("outTimes:", outTimes)
  498. if outTimes != "" {
  499. timeArr := strings.Split(outTimes, ":")
  500. h := timeArr[0]
  501. m := timeArr[1]
  502. s := timeArr[2]
  503. hInt, err := strconv.Atoi(h)
  504. if err != nil {
  505. return playSeconds, err
  506. }
  507. mInt, err := strconv.Atoi(m)
  508. if err != nil {
  509. return playSeconds, err
  510. }
  511. s = strings.Trim(s, " ")
  512. s = strings.Trim(s, "\n")
  513. sInt, err := strconv.ParseFloat(s, 64)
  514. if err != nil {
  515. return playSeconds, err
  516. }
  517. playSeconds = float64(hInt)*3600 + float64(mInt)*60 + float64(sInt)
  518. }
  519. return
  520. }
  521. func GetMaxTradeCode(tradeCode string) (maxTradeCode string, err error) {
  522. tradeCode = strings.Replace(tradeCode, "W", "", -1)
  523. tradeCode = strings.Trim(tradeCode, " ")
  524. tradeCodeInt, err := strconv.Atoi(tradeCode)
  525. if err != nil {
  526. return
  527. }
  528. tradeCodeInt = tradeCodeInt + 1
  529. maxTradeCode = fmt.Sprintf("W%06d", tradeCodeInt)
  530. return
  531. }
  532. // excel日期字段格式化 yyyy-mm-dd
  533. func ConvertToFormatDay(excelDaysString string) string {
  534. // 2006-01-02 距离 1900-01-01的天数
  535. baseDiffDay := 38719 //在网上工具计算的天数需要加2天,什么原因没弄清楚
  536. curDiffDay := excelDaysString
  537. b, _ := strconv.Atoi(curDiffDay)
  538. // 获取excel的日期距离2006-01-02的天数
  539. realDiffDay := b - baseDiffDay
  540. //fmt.Println("realDiffDay:",realDiffDay)
  541. // 距离2006-01-02 秒数
  542. realDiffSecond := realDiffDay * 24 * 3600
  543. //fmt.Println("realDiffSecond:",realDiffSecond)
  544. // 2006-01-02 15:04:05距离1970-01-01 08:00:00的秒数 网上工具可查出
  545. baseOriginSecond := 1136185445
  546. resultTime := time.Unix(int64(baseOriginSecond+realDiffSecond), 0).Format("2006-01-02")
  547. return resultTime
  548. }
  549. // GetDaysDiff1900 计算日期距离1900-01-01的天数
  550. func GetDaysDiff1900(date string) int {
  551. // 将字符串转换为时间类型
  552. //从1899年12月30日开始是因为Excel的日期计算是基于1900年1月1日的,而1900年并不是闰年,因此Excel日期计算存在一个错误。为了修正这个错误,
  553. //我们将时间回溯到1899年12月30日,这样在进行日期计算时可以正确地处理Excel日期。
  554. tStart, err := time.ParseInLocation(FormatDate, "1899-12-30", time.Local)
  555. if err != nil {
  556. return 0
  557. }
  558. tEnd, err := time.ParseInLocation(FormatDate, date, time.Local)
  559. if err != nil {
  560. return 0
  561. }
  562. // 计算两个日期之间的天数差值
  563. duration := tEnd.Sub(tStart).Hours() / 24
  564. days := int(duration)
  565. return days
  566. }
  567. func CheckPwd(pwd string) bool {
  568. compile := `([0-9a-z]+){6,12}|(a-z0-9]+){6,12}`
  569. reg := regexp.MustCompile(compile)
  570. flag := reg.MatchString(pwd)
  571. return flag
  572. }
  573. func GetMonthStartAndEnd(myYear string, myMonth string) (startDate, endDate string) {
  574. // 数字月份必须前置补零
  575. if len(myMonth) == 1 {
  576. myMonth = "0" + myMonth
  577. }
  578. yInt, _ := strconv.Atoi(myYear)
  579. timeLayout := "2006-01-02 15:04:05"
  580. loc, _ := time.LoadLocation("Local")
  581. theTime, _ := time.ParseInLocation(timeLayout, myYear+"-"+myMonth+"-01 00:00:00", loc)
  582. newMonth := theTime.Month()
  583. t1 := time.Date(yInt, newMonth, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  584. t2 := time.Date(yInt, newMonth+1, 0, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  585. return t1, t2
  586. }
  587. // TrimStr 移除字符串中的空格
  588. func TrimStr(str string) (str2 string) {
  589. if str == "" {
  590. return str
  591. }
  592. return strings.Replace(str, " ", "", -1)
  593. }
  594. // TrimLRStr 移除字符串前后的空格
  595. func TrimLRStr(str string) (str2 string) {
  596. if str == "" {
  597. return str
  598. }
  599. str2 = strings.TrimLeft(str, " ")
  600. str2 = strings.TrimRight(str2, " ")
  601. return
  602. }
  603. // 字符串转换为time
  604. func StrTimeToTime(strTime string) time.Time {
  605. timeLayout := "2006-01-02 15:04:05" //转化所需模板
  606. loc, _ := time.LoadLocation("Local") //重要:获取时区
  607. resultTime, _ := time.ParseInLocation(timeLayout, strTime, loc)
  608. return resultTime
  609. }
  610. // 字符串类型时间转周几
  611. func StrDateTimeToWeek(strTime string) string {
  612. var WeekDayMap = map[string]string{
  613. "Monday": "周一",
  614. "Tuesday": "周二",
  615. "Wednesday": "周三",
  616. "Thursday": "周四",
  617. "Friday": "周五",
  618. "Saturday": "周六",
  619. "Sunday": "周日",
  620. }
  621. var ctime = StrTimeToTime(strTime).Format("2006-01-02")
  622. startday, _ := time.Parse("2006-01-02", ctime)
  623. staweek_int := startday.Weekday().String()
  624. return WeekDayMap[staweek_int]
  625. }
  626. // EnWeekToCnWeek 英文周几转中文周几
  627. func EnWeekToCnWeek(enWeekStr string) string {
  628. var WeekDayMap = map[string]string{
  629. "Monday": "周一",
  630. "Tuesday": "周二",
  631. "Wednesday": "周三",
  632. "Thursday": "周四",
  633. "Friday": "周五",
  634. "Saturday": "周六",
  635. "Sunday": "周日",
  636. }
  637. cnWeekStr, ok := WeekDayMap[enWeekStr]
  638. if !ok {
  639. cnWeekStr = `周一`
  640. }
  641. return cnWeekStr
  642. }
  643. // 时间格式转年月日字符串
  644. func TimeToStrYmd(time2 time.Time) string {
  645. var Ymd string
  646. year := time2.Year()
  647. month := time2.Format("1")
  648. day1 := time.Now().Day()
  649. Ymd = strconv.Itoa(year) + "年" + month + "月" + strconv.Itoa(day1) + "日"
  650. return Ymd
  651. }
  652. // 时间格式去掉时分秒
  653. func TimeRemoveHms(strTime string) string {
  654. var Ymd string
  655. var resultTime = StrTimeToTime(strTime)
  656. year := resultTime.Year()
  657. month := resultTime.Format("01")
  658. day1 := resultTime.Day()
  659. if day1 < 10 {
  660. Ymd = strconv.Itoa(year) + "." + month + ".0" + strconv.Itoa(day1)
  661. } else {
  662. Ymd = strconv.Itoa(year) + "." + month + "." + strconv.Itoa(day1)
  663. }
  664. return Ymd
  665. }
  666. // 时间格式去掉时分秒
  667. func TimeRemoveHms2(strTime string) string {
  668. var Ymd string
  669. var resultTime = StrTimeToTime(strTime)
  670. year := resultTime.Year()
  671. month := resultTime.Format("01")
  672. day1 := resultTime.Day()
  673. if day1 < 10 {
  674. Ymd = strconv.Itoa(year) + "-" + month + "-0" + strconv.Itoa(day1)
  675. } else {
  676. Ymd = strconv.Itoa(year) + "-" + month + "-" + strconv.Itoa(day1)
  677. }
  678. return Ymd
  679. }
  680. // 文章上一次编辑时间
  681. func ArticleLastTime(strTime string) string {
  682. var newTime string
  683. stamp, _ := time.ParseInLocation("2006-01-02 15:04:05", strTime, time.Local)
  684. diffTime := time.Now().Unix() - stamp.Unix()
  685. if diffTime <= 60 {
  686. newTime = "当前"
  687. } else if diffTime < 60*60 {
  688. newTime = strconv.FormatInt(diffTime/60, 10) + "分钟前"
  689. } else if diffTime < 24*60*60 {
  690. newTime = strconv.FormatInt(diffTime/(60*60), 10) + "小时前"
  691. } else if diffTime < 30*24*60*60 {
  692. newTime = strconv.FormatInt(diffTime/(24*60*60), 10) + "天前"
  693. } else if diffTime < 12*30*24*60*60 {
  694. newTime = strconv.FormatInt(diffTime/(30*24*60*60), 10) + "月前"
  695. } else {
  696. newTime = "1年前"
  697. }
  698. return newTime
  699. }
  700. // 人民币小写转大写
  701. func ConvertNumToCny(num float64) (str string, err error) {
  702. strNum := strconv.FormatFloat(num*100, 'f', 0, 64)
  703. sliceUnit := []string{"仟", "佰", "拾", "亿", "仟", "佰", "拾", "万", "仟", "佰", "拾", "元", "角", "分"}
  704. // log.Println(sliceUnit[:len(sliceUnit)-2])
  705. s := sliceUnit[len(sliceUnit)-len(strNum):]
  706. upperDigitUnit := map[string]string{"0": "零", "1": "壹", "2": "贰", "3": "叁", "4": "肆", "5": "伍", "6": "陆", "7": "柒", "8": "捌", "9": "玖"}
  707. for k, v := range strNum[:] {
  708. str = str + upperDigitUnit[string(v)] + s[k]
  709. }
  710. reg, err := regexp.Compile(`零角零分$`)
  711. str = reg.ReplaceAllString(str, "整")
  712. reg, err = regexp.Compile(`零角`)
  713. str = reg.ReplaceAllString(str, "零")
  714. reg, err = regexp.Compile(`零分$`)
  715. str = reg.ReplaceAllString(str, "整")
  716. reg, err = regexp.Compile(`零[仟佰拾]`)
  717. str = reg.ReplaceAllString(str, "零")
  718. reg, err = regexp.Compile(`零{2,}`)
  719. str = reg.ReplaceAllString(str, "零")
  720. reg, err = regexp.Compile(`零亿`)
  721. str = reg.ReplaceAllString(str, "亿")
  722. reg, err = regexp.Compile(`零万`)
  723. str = reg.ReplaceAllString(str, "万")
  724. reg, err = regexp.Compile(`零*元`)
  725. str = reg.ReplaceAllString(str, "元")
  726. reg, err = regexp.Compile(`亿零{0, 3}万`)
  727. str = reg.ReplaceAllString(str, "^元")
  728. reg, err = regexp.Compile(`零元`)
  729. str = reg.ReplaceAllString(str, "零")
  730. return
  731. }
  732. // GetNowWeekMonday 获取本周周一的时间
  733. func GetNowWeekMonday() time.Time {
  734. offset := int(time.Monday - time.Now().Weekday())
  735. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  736. offset = -6
  737. }
  738. mondayTime := time.Now().AddDate(0, 0, offset)
  739. mondayTime = time.Date(mondayTime.Year(), mondayTime.Month(), mondayTime.Day(), 0, 0, 0, 0, mondayTime.Location())
  740. return mondayTime
  741. }
  742. // GetNowWeekLastDay 获取本周最后一天的时间
  743. func GetNowWeekLastDay() time.Time {
  744. offset := int(time.Monday - time.Now().Weekday())
  745. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  746. offset = -6
  747. }
  748. firstDayTime := time.Now().AddDate(0, 0, offset)
  749. firstDayTime = time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 0, 0, 0, 0, firstDayTime.Location()).AddDate(0, 0, 6)
  750. lastDayTime := time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 23, 59, 59, 0, firstDayTime.Location())
  751. return lastDayTime
  752. }
  753. // GetNowMonthFirstDay 获取本月第一天的时间
  754. func GetNowMonthFirstDay() time.Time {
  755. nowMonthFirstDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location())
  756. return nowMonthFirstDay
  757. }
  758. // GetNowMonthLastDay 获取本月最后一天的时间
  759. func GetNowMonthLastDay() time.Time {
  760. nowMonthLastDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  761. nowMonthLastDay = time.Date(nowMonthLastDay.Year(), nowMonthLastDay.Month(), nowMonthLastDay.Day(), 23, 59, 59, 0, nowMonthLastDay.Location())
  762. return nowMonthLastDay
  763. }
  764. // GetNowQuarterFirstDay 获取本季度第一天的时间
  765. func GetNowQuarterFirstDay() time.Time {
  766. month := int(time.Now().Month())
  767. var nowQuarterFirstDay time.Time
  768. if month >= 1 && month <= 3 {
  769. //1月1号
  770. nowQuarterFirstDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  771. } else if month >= 4 && month <= 6 {
  772. //4月1号
  773. nowQuarterFirstDay = time.Date(time.Now().Year(), 4, 1, 0, 0, 0, 0, time.Now().Location())
  774. } else if month >= 7 && month <= 9 {
  775. nowQuarterFirstDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
  776. } else {
  777. nowQuarterFirstDay = time.Date(time.Now().Year(), 10, 1, 0, 0, 0, 0, time.Now().Location())
  778. }
  779. return nowQuarterFirstDay
  780. }
  781. // GetNowQuarterLastDay 获取本季度最后一天的时间
  782. func GetNowQuarterLastDay() time.Time {
  783. month := int(time.Now().Month())
  784. var nowQuarterLastDay time.Time
  785. if month >= 1 && month <= 3 {
  786. //03-31 23:59:59
  787. nowQuarterLastDay = time.Date(time.Now().Year(), 3, 31, 23, 59, 59, 0, time.Now().Location())
  788. } else if month >= 4 && month <= 6 {
  789. //06-30 23:59:59
  790. nowQuarterLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
  791. } else if month >= 7 && month <= 9 {
  792. //09-30 23:59:59
  793. nowQuarterLastDay = time.Date(time.Now().Year(), 9, 30, 23, 59, 59, 0, time.Now().Location())
  794. } else {
  795. //12-31 23:59:59
  796. nowQuarterLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  797. }
  798. return nowQuarterLastDay
  799. }
  800. // GetNowHalfYearFirstDay 获取当前半年的第一天的时间
  801. func GetNowHalfYearFirstDay() time.Time {
  802. month := int(time.Now().Month())
  803. var nowHalfYearLastDay time.Time
  804. if month >= 1 && month <= 6 {
  805. //03-31 23:59:59
  806. nowHalfYearLastDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  807. } else {
  808. //12-31 23:59:59
  809. nowHalfYearLastDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
  810. }
  811. return nowHalfYearLastDay
  812. }
  813. // GetNowHalfYearLastDay 获取当前半年的最后一天的时间
  814. func GetNowHalfYearLastDay() time.Time {
  815. month := int(time.Now().Month())
  816. var nowHalfYearLastDay time.Time
  817. if month >= 1 && month <= 6 {
  818. //03-31 23:59:59
  819. nowHalfYearLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
  820. } else {
  821. //12-31 23:59:59
  822. nowHalfYearLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  823. }
  824. return nowHalfYearLastDay
  825. }
  826. // GetNowYearFirstDay 获取当前年的最后一天的时间
  827. func GetNowYearFirstDay() time.Time {
  828. //12-31 23:59:59
  829. nowYearFirstDay := time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  830. return nowYearFirstDay
  831. }
  832. // GetNowYearLastDay 获取当前年的最后一天的时间
  833. func GetNowYearLastDay() time.Time {
  834. //12-31 23:59:59
  835. nowYearLastDay := time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  836. return nowYearLastDay
  837. }
  838. // CalculationDate 计算两个日期之间相差n年m月y天
  839. func CalculationDate(startDate, endDate time.Time) (beetweenDay string, err error) {
  840. //startDate := time.Date(2021, 3, 28, 0, 0, 0, 0, time.Now().Location())
  841. //endDate := time.Date(2022, 3, 31, 0, 0, 0, 0, time.Now().Location())
  842. numYear := endDate.Year() - startDate.Year()
  843. numMonth := int(endDate.Month()) - int(startDate.Month())
  844. numDay := 0
  845. //获取截止月的总天数
  846. endDateDays := getMonthDay(endDate.Year(), int(endDate.Month()))
  847. //获取截止月的前一个月
  848. endDatePrevMonthDate := endDate.AddDate(0, -1, 0)
  849. //获取截止日期的上一个月的总天数
  850. endDatePrevMonthDays := getMonthDay(endDatePrevMonthDate.Year(), int(endDatePrevMonthDate.Month()))
  851. //获取开始日期的的月份总天数
  852. startDateMonthDays := getMonthDay(startDate.Year(), int(startDate.Month()))
  853. //判断,截止月是否完全被选中,如果相等,那么代表截止月份全部天数被选择
  854. if endDate.Day() == endDateDays {
  855. numDay = startDateMonthDays - startDate.Day() + 1
  856. //如果剩余天数正好与开始日期的天数是一致的,那么月份加1
  857. if numDay == startDateMonthDays {
  858. numMonth++
  859. numDay = 0
  860. //超过月份了,那么年份加1
  861. if numMonth == 12 {
  862. numYear++
  863. numMonth = 0
  864. }
  865. }
  866. } else {
  867. numDay = endDate.Day() - startDate.Day() + 1
  868. }
  869. //天数小于0,那么向月份借一位
  870. if numDay < 0 {
  871. //向上一个月借一个月的天数
  872. numDay += endDatePrevMonthDays
  873. //总月份减去一个月
  874. numMonth = numMonth - 1
  875. }
  876. //月份小于0,那么向年份借一位
  877. if numMonth < 0 {
  878. //向上一个年借12个月
  879. numMonth += 12
  880. //总年份减去一年
  881. numYear = numYear - 1
  882. }
  883. if numYear < 0 {
  884. err = errors.New("日期异常")
  885. return
  886. }
  887. if numYear > 0 {
  888. beetweenDay += fmt.Sprint(numYear, "年")
  889. }
  890. if numMonth > 0 {
  891. beetweenDay += fmt.Sprint(numMonth, "个月")
  892. }
  893. if numDay > 0 {
  894. beetweenDay += fmt.Sprint(numDay, "天")
  895. }
  896. return
  897. }
  898. // FormatPrice 格式化展示金额数字(财务金额展示,小数点前,每三位用,隔开) 1,234,567,898.55
  899. func FormatPrice(price float64) (str string) {
  900. str = decimal.NewFromFloat(price).String()
  901. length := len(str)
  902. if length < 4 {
  903. return str
  904. }
  905. arr := strings.Split(str, ".") //用小数点符号分割字符串,为数组接收
  906. length1 := len(arr[0])
  907. if length1 < 4 {
  908. return str
  909. }
  910. count := (length1 - 1) / 3
  911. for i := 0; i < count; i++ {
  912. arr[0] = arr[0][:length1-(i+1)*3] + "," + arr[0][length1-(i+1)*3:]
  913. }
  914. return strings.Join(arr, ".") //将一系列字符串连接为一个字符串,之间用sep来分隔。
  915. }
  916. // getMonthDay 获取某年某月有多少天
  917. func getMonthDay(year, month int) (days int) {
  918. if month != 2 {
  919. if month == 4 || month == 6 || month == 9 || month == 11 {
  920. days = 30
  921. } else {
  922. days = 31
  923. }
  924. } else {
  925. if ((year%4) == 0 && (year%100) != 0) || (year%400) == 0 {
  926. days = 29
  927. } else {
  928. days = 28
  929. }
  930. }
  931. return
  932. }
  933. func SaveToFile(content, path string) error {
  934. f, err := os.Create(path)
  935. defer f.Close()
  936. if err != nil {
  937. return err
  938. }
  939. f.Write([]byte(content))
  940. return nil
  941. }
  942. // 用户参会时间转换
  943. func GetAttendanceDetailSeconds(secondNum int) string {
  944. var timeStr string
  945. if secondNum <= 60 {
  946. if secondNum < 10 {
  947. timeStr = "0" + strconv.Itoa(secondNum) + "''"
  948. } else {
  949. timeStr = strconv.Itoa(secondNum) + "''"
  950. }
  951. } else {
  952. var remainderStr string
  953. remainderNum := secondNum % 60
  954. minuteNum := secondNum / 60
  955. if remainderNum < 10 {
  956. remainderStr = "0" + strconv.Itoa(remainderNum) + "''"
  957. } else {
  958. remainderStr = strconv.Itoa(remainderNum) + "''"
  959. }
  960. if minuteNum < 10 {
  961. timeStr = "0" + strconv.Itoa(minuteNum) + "'" + remainderStr
  962. } else {
  963. timeStr = strconv.Itoa(minuteNum) + "'" + remainderStr
  964. }
  965. }
  966. return timeStr
  967. }
  968. // SubStr 截取字符串(中文)
  969. func SubStr(str string, subLen int) string {
  970. strRune := []rune(str)
  971. bodyRuneLen := len(strRune)
  972. if bodyRuneLen > subLen {
  973. bodyRuneLen = subLen
  974. }
  975. str = string(strRune[:bodyRuneLen])
  976. return str
  977. }
  978. // HideString 给字段加***(从字符串中间替换,少于需要替换的长度,那么就补全*的长度)
  979. // src 待*字符串
  980. // hideLen 需要加*的长度
  981. func HideString(src string, hideLen int) string {
  982. if src == "" {
  983. return src
  984. }
  985. str := []rune(src)
  986. if hideLen == 0 {
  987. hideLen = 4
  988. }
  989. hideStr := ""
  990. for i := 0; i < hideLen; i++ {
  991. hideStr += "*"
  992. }
  993. strLen := len(str)
  994. // 字符长度是1
  995. if strLen == 1 {
  996. return string(str[:1]) + hideStr
  997. }
  998. //字符长度大于1,但是小于等于需要隐藏的字符长度,那么就隐藏中间,保留前后各一位字符
  999. if strLen <= hideLen+2 {
  1000. return string(str[:1]) + hideStr + string(str[strLen-1:])
  1001. }
  1002. subLen := strLen - hideLen //剩余需要展示的字符长度
  1003. decimal.NewFromFloat(2)
  1004. frontLenDecimal := decimal.NewFromInt(int64(subLen)).Div(decimal.NewFromInt(2)) //前面需要展示的字符的长度
  1005. frontLen := frontLenDecimal.Floor().IntPart()
  1006. return string(str[:frontLen]) + hideStr + string(str[frontLen+int64(hideLen):])
  1007. }
  1008. func GetLocalIP() (ip string, err error) {
  1009. addrs, err := net.InterfaceAddrs()
  1010. if err != nil {
  1011. return
  1012. }
  1013. for _, addr := range addrs {
  1014. ipAddr, ok := addr.(*net.IPNet)
  1015. if !ok {
  1016. continue
  1017. }
  1018. if ipAddr.IP.IsLoopback() {
  1019. continue
  1020. }
  1021. if !ipAddr.IP.IsGlobalUnicast() {
  1022. continue
  1023. }
  1024. return ipAddr.IP.String(), nil
  1025. }
  1026. return
  1027. }
  1028. // GetNowWeekMonday 获取上一周周一的时间
  1029. func GetPreWeekMonday() time.Time {
  1030. nowMonday := GetNowWeekMonday()
  1031. return nowMonday.AddDate(0, 0, -7)
  1032. }
  1033. // GetNowWeekLastDay 获取上一周最后一天的时间
  1034. func GetPreWeekLastDay() time.Time {
  1035. nowSunday := GetNowWeekLastDay()
  1036. return nowSunday.AddDate(0, 0, -7)
  1037. }
  1038. // GetNextWeekMonday 获取下一周周一的时间
  1039. func GetNextWeekMonday() time.Time {
  1040. nowMonday := GetNowWeekMonday()
  1041. return nowMonday.AddDate(0, 0, 7)
  1042. }
  1043. // GetNextWeekLastDay 获取下一周最后一天的时间
  1044. func GetNextWeekLastDay() time.Time {
  1045. nowSunday := GetNowWeekLastDay()
  1046. return nowSunday.AddDate(0, 0, 7)
  1047. }
  1048. // 处理真实姓名,隐藏中间的部分,如:张*,张*将
  1049. func DealRealName(realName string) string {
  1050. if realName == "" {
  1051. return ""
  1052. }
  1053. names := bytes.Runes([]byte(realName))
  1054. fistName := names[0]
  1055. length := len(names)
  1056. if length == 2 {
  1057. return string(fistName) + "*"
  1058. } else if length > 2 {
  1059. lastName := names[length-1]
  1060. return string(fistName) + "*" + string(lastName)
  1061. }
  1062. return realName
  1063. }
  1064. // Implode php中的implode(用来将int型的切片转为string)
  1065. func Implode(idIntList []int) (str string) {
  1066. for _, id := range idIntList {
  1067. str += fmt.Sprint(id, ",")
  1068. }
  1069. str = str[:len(str)-1]
  1070. return
  1071. }
  1072. // InArrayByInt php中的in_array(判断Int类型的切片中是否存在该int值)
  1073. func InArrayByInt(idIntList []int, searchId int) (has bool) {
  1074. for _, id := range idIntList {
  1075. if id == searchId {
  1076. has = true
  1077. return
  1078. }
  1079. }
  1080. return
  1081. }
  1082. // InArrayByStr php中的in_array(判断String类型的切片中是否存在该string值)
  1083. func InArrayByStr(idStrList []string, searchId string) (has bool) {
  1084. for _, id := range idStrList {
  1085. if id == searchId {
  1086. has = true
  1087. return
  1088. }
  1089. }
  1090. return
  1091. }
  1092. // Intersection 求两个切片的交集
  1093. func Intersection(a []string, b []string) (inter []string) {
  1094. // interacting on the smallest list first can potentailly be faster...but not by much, worse case is the same
  1095. low, high := a, b
  1096. if len(a) > len(b) {
  1097. low = b
  1098. high = a
  1099. }
  1100. done := false
  1101. for i, l := range low {
  1102. for j, h := range high {
  1103. // get future index values
  1104. f1 := i + 1
  1105. f2 := j + 1
  1106. if l == h {
  1107. inter = append(inter, h)
  1108. if f1 < len(low) && f2 < len(high) {
  1109. // if the future values aren't the same then that's the end of the intersection
  1110. if low[f1] != high[f2] {
  1111. done = true
  1112. }
  1113. }
  1114. // we don't want to interate on the entire list everytime, so remove the parts we already looped on will make it faster each pass
  1115. high = high[:j+copy(high[j:], high[j+1:])]
  1116. break
  1117. }
  1118. }
  1119. // nothing in the future so we are done
  1120. if done {
  1121. break
  1122. }
  1123. }
  1124. return
  1125. }
  1126. // Minus 获取差集
  1127. func Minus(a []string, b []string) []string {
  1128. //var inter []string
  1129. inter := make([]string, 0)
  1130. mp := make(map[string]bool)
  1131. for _, s := range a {
  1132. if _, ok := mp[s]; !ok {
  1133. mp[s] = true
  1134. }
  1135. }
  1136. for _, s := range b {
  1137. if _, ok := mp[s]; ok {
  1138. delete(mp, s)
  1139. }
  1140. }
  1141. for key := range mp {
  1142. inter = append(inter, key)
  1143. }
  1144. return inter
  1145. }
  1146. // IntArr2joinString []int转字符串
  1147. func IntArr2joinString(arr []int, sep string) string {
  1148. if len(arr) == 0 {
  1149. return ""
  1150. }
  1151. strArr := make([]string, 0)
  1152. for _, v := range arr {
  1153. strArr = append(strArr, strconv.Itoa(v))
  1154. }
  1155. return strings.Join(strArr, sep)
  1156. }
  1157. // GetOneMonthFirstDay 获取某月的第一天的时间
  1158. func GetOneMonthFirstDay(time2 time.Time) time.Time {
  1159. nowMonthFirstDay := time.Date(time2.Year(), time2.Month(), 1, 0, 0, 0, 0, time.Now().Location())
  1160. return nowMonthFirstDay
  1161. }
  1162. // GetOneMonthLastDay 获取某月最后一天的时间
  1163. func GetOneMonthLastDay(time2 time.Time) time.Time {
  1164. nowMonthLastDay := time.Date(time2.Year(), time2.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  1165. nowMonthLastDay = time.Date(nowMonthLastDay.Year(), nowMonthLastDay.Month(), nowMonthLastDay.Day(), 23, 59, 59, 0, nowMonthLastDay.Location())
  1166. return nowMonthLastDay
  1167. }
  1168. func GetReportContentTextSub(content string) (contentSub string, err error) {
  1169. content = html.UnescapeString(content)
  1170. doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
  1171. docText := doc.Text()
  1172. bodyRune := []rune(docText)
  1173. bodyRuneLen := len(bodyRune)
  1174. if bodyRuneLen > 200 {
  1175. bodyRuneLen = 200
  1176. }
  1177. body := string(bodyRune[:bodyRuneLen])
  1178. contentSub = body
  1179. contentSub = strings.Replace(body, "Powered by Froala Editor", "", -1)
  1180. return
  1181. }
  1182. // GetOrmInReplace 获取orm的in查询替换?的方法
  1183. func GetOrmInReplace(num int) string {
  1184. template := make([]string, num)
  1185. for i := 0; i < num; i++ {
  1186. template[i] = "?"
  1187. }
  1188. return strings.Join(template, ",")
  1189. }
  1190. // RevSlice 反转切片
  1191. func RevSlice(slice []int) []int {
  1192. for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 {
  1193. slice[i], slice[j] = slice[j], slice[i]
  1194. }
  1195. return slice
  1196. }
  1197. // ReplaceSpaceAndWrap 去除空格跟换行
  1198. func ReplaceSpaceAndWrap(str string) string {
  1199. // 去除空格
  1200. str = strings.Replace(str, " ", "", -1)
  1201. // 去除换行符
  1202. str = strings.Replace(str, "\n", "", -1)
  1203. return str
  1204. }
  1205. // JoinStr2IntArr 拼接字符串转[]int
  1206. func JoinStr2IntArr(str, sep string) (arr []int) {
  1207. arr = make([]int, 0)
  1208. if str == "" {
  1209. return
  1210. }
  1211. if sep == "" {
  1212. sep = ","
  1213. }
  1214. strArr := strings.Split(str, sep)
  1215. if len(strArr) == 0 {
  1216. return
  1217. }
  1218. for i := range strArr {
  1219. v, e := strconv.Atoi(strArr[i])
  1220. // int2str此处过滤掉无效int
  1221. if e != nil {
  1222. continue
  1223. }
  1224. arr = append(arr, v)
  1225. }
  1226. return
  1227. }
  1228. // 字符串转换为time类型
  1229. func StrDateToDate(strTime string) time.Time {
  1230. timeLayout := "2006-01-02" //转化所需模板
  1231. loc, _ := time.LoadLocation("Local") //重要:获取时区
  1232. resultTime, _ := time.ParseInLocation(timeLayout, strTime, loc)
  1233. return resultTime
  1234. }
  1235. // GetTimeSubDay 计算两个时间的自然日期差(后面减去前面)
  1236. func GetTimeSubDay(t1, t2 time.Time) int {
  1237. var day int
  1238. swap := false
  1239. if t1.Unix() > t2.Unix() {
  1240. t1, t2 = t2, t1
  1241. swap = true
  1242. }
  1243. t1_ := t1.Add(time.Duration(t2.Sub(t1).Milliseconds()%86400000) * time.Millisecond)
  1244. day = int(t2.Sub(t1).Hours() / 24)
  1245. // 计算在t1+两个时间的余数之后天数是否有变化
  1246. if t1_.Day() != t1.Day() {
  1247. day += 1
  1248. }
  1249. if swap {
  1250. day = -day
  1251. }
  1252. return day
  1253. }
  1254. // 去除非中文字符串
  1255. func StrFilterNonChinese(src *string) {
  1256. var hzRegexp = regexp.MustCompile("^[\u4e00-\u9fa5]$")
  1257. strn := ""
  1258. for _, c := range *src {
  1259. if hzRegexp.MatchString(string(c)) {
  1260. strn += string(c)
  1261. }
  1262. }
  1263. *src = strn
  1264. }
  1265. // MinusInt 获取两个[]int差集
  1266. func MinusInt(a []int, b []int) []int {
  1267. var diff []int
  1268. mpA, mpB := make(map[int]bool), make(map[int]bool)
  1269. for _, v := range a {
  1270. mpA[v] = true
  1271. }
  1272. for _, v := range b {
  1273. mpB[v] = true
  1274. }
  1275. for _, s := range a {
  1276. if !mpB[s] {
  1277. diff = append(diff, s)
  1278. }
  1279. }
  1280. for _, s := range b {
  1281. if !mpA[s] {
  1282. diff = append(diff, s)
  1283. }
  1284. }
  1285. return diff
  1286. }
  1287. // IntersectInt 获取两个[]int交集
  1288. func IntersectInt(nums1 []int, nums2 []int) []int {
  1289. m := make(map[int]int)
  1290. var res []int
  1291. for _, num := range nums1 {
  1292. m[num]++
  1293. }
  1294. for _, num := range nums2 {
  1295. if m[num] > 0 {
  1296. res = append(res, num)
  1297. m[num]--
  1298. }
  1299. }
  1300. return res
  1301. }
  1302. // MapSorter 对于map 排序
  1303. type MapSorter []Item
  1304. type Item struct {
  1305. Key int
  1306. Val float64
  1307. }
  1308. func NewMapSorter(m map[int]float64) MapSorter {
  1309. ms := make(MapSorter, 0, len(m))
  1310. for k, v := range m {
  1311. ms = append(ms, Item{k, v})
  1312. }
  1313. return ms
  1314. }
  1315. func (ms MapSorter) Len() int {
  1316. return len(ms)
  1317. }
  1318. func (ms MapSorter) Less(i, j int) bool {
  1319. return ms[i].Val > ms[j].Val // 按值排序
  1320. //return ms[i].Key < ms[j].Key // 按键排序
  1321. }
  1322. func (ms MapSorter) Swap(i, j int) {
  1323. ms[i], ms[j] = ms[j], ms[i]
  1324. }
  1325. // FormatFileSize 字节的单位转换 保留两位小数
  1326. func FormatFileSize(fileSize int64) (size string) {
  1327. if fileSize < 1024 {
  1328. return fmt.Sprintf("%.2fB", float64(fileSize)/float64(1))
  1329. } else if fileSize < (1024 * 1024) {
  1330. return fmt.Sprintf("%.2fKB", float64(fileSize)/float64(1024))
  1331. } else if fileSize < (1024 * 1024 * 1024) {
  1332. return fmt.Sprintf("%.2fMB", float64(fileSize)/float64(1024*1024))
  1333. } else if fileSize < (1024 * 1024 * 1024 * 1024) {
  1334. return fmt.Sprintf("%.2fGB", float64(fileSize)/float64(1024*1024*1024))
  1335. } else if fileSize < (1024 * 1024 * 1024 * 1024 * 1024) {
  1336. return fmt.Sprintf("%.2fTB", float64(fileSize)/float64(1024*1024*1024*1024))
  1337. } else { //if fileSize < (1024 * 1024 * 1024 * 1024 * 1024 * 1024)
  1338. return fmt.Sprintf("%.2fPB", float64(fileSize)/float64(1024*1024*1024*1024*1024))
  1339. }
  1340. }
  1341. func GetDurationFormatBySecond(sec int) (formatString string) {
  1342. if sec == 0 {
  1343. formatString = "0分钟"
  1344. return
  1345. } else if sec <= 60 && sec > 0 {
  1346. formatString = "1分钟"
  1347. return
  1348. }
  1349. duration := time.Duration(int64(sec)) * time.Second
  1350. h := int(duration.Hours())
  1351. m := int(duration.Minutes()) % 60
  1352. //s := int(duration.Seconds()) % 60
  1353. if h > 0 {
  1354. formatString = fmt.Sprintf("%d小时", h)
  1355. }
  1356. if m > 0 {
  1357. formatString += fmt.Sprintf("%d分钟", m)
  1358. }
  1359. //if s > 0 {
  1360. // formatString += fmt.Sprintf("%d秒", s)
  1361. //}
  1362. return
  1363. }
  1364. // GetDaysBetween2Date 计算两个日期之间相差几天
  1365. func GetDaysBetween2Date(format, date1Str, date2Str string) (int, error) {
  1366. // 将字符串转化为Time格式
  1367. date1, err := time.ParseInLocation(format, date1Str, time.Local)
  1368. if err != nil {
  1369. return 0, err
  1370. }
  1371. // 将字符串转化为Time格式
  1372. date2, err := time.ParseInLocation(format, date2Str, time.Local)
  1373. if err != nil {
  1374. return 0, err
  1375. }
  1376. //计算相差天数
  1377. return int(date1.Sub(date2).Hours() / 24), nil
  1378. }
  1379. // GetFrequencyEn 获取频度的英文版
  1380. func GetFrequencyEn(frequency string) (frequencyEn string) {
  1381. switch frequency {
  1382. case "日度":
  1383. frequencyEn = "day"
  1384. return
  1385. case "周度":
  1386. frequencyEn = "week"
  1387. return
  1388. case "旬度":
  1389. frequencyEn = "ten days"
  1390. return
  1391. case "月度":
  1392. frequencyEn = "month"
  1393. return
  1394. case "季度":
  1395. frequencyEn = "quarter"
  1396. return
  1397. case "年度":
  1398. frequencyEn = "year"
  1399. return
  1400. }
  1401. return
  1402. }
  1403. // GetLeadUnitEn 获取移动单位的英文版
  1404. func GetLeadUnitEn(unit string) (unitEn string) {
  1405. switch unit {
  1406. case "天":
  1407. unitEn = "day"
  1408. return
  1409. case "周":
  1410. unitEn = "week"
  1411. return
  1412. case "月":
  1413. unitEn = "month"
  1414. return
  1415. case "季":
  1416. unitEn = "quarter"
  1417. return
  1418. case "年":
  1419. unitEn = "year"
  1420. return
  1421. }
  1422. return
  1423. }
  1424. // GetDateByDateType 通过dateType获取需要的开始/结束日期
  1425. func GetDateByDateType(dateType int, tmpStartDate, tmpEndDate string) (startDate, endDate string) {
  1426. startDate = tmpStartDate
  1427. endDate = tmpEndDate
  1428. switch dateType {
  1429. case 1:
  1430. startDate = "2000-01-01"
  1431. endDate = ""
  1432. case 2:
  1433. startDate = "2010-01-01"
  1434. endDate = ""
  1435. case 3:
  1436. startDate = "2015-01-01"
  1437. endDate = ""
  1438. case 4:
  1439. //startDate = strconv.Itoa(time.Now().Year()) + "-01-01"
  1440. startDate = "2021-01-01"
  1441. endDate = ""
  1442. case 5:
  1443. //startDate = startDate + "-01"
  1444. //endDate = endDate + "-01"
  1445. case 6:
  1446. //startDate = startDate + "-01"
  1447. endDate = ""
  1448. case 7:
  1449. startDate = "2018-01-01"
  1450. endDate = ""
  1451. case 8:
  1452. startDate = "2019-01-01"
  1453. endDate = ""
  1454. case 9:
  1455. startDate = "2020-01-01"
  1456. endDate = ""
  1457. case 11:
  1458. startDate = "2022-01-01"
  1459. endDate = ""
  1460. }
  1461. // 兼容日期错误
  1462. {
  1463. if strings.Count(startDate, "-") == 1 {
  1464. startDate = startDate + "-01"
  1465. }
  1466. if strings.Count(endDate, "-") == 1 {
  1467. endDate = endDate + "-01"
  1468. }
  1469. }
  1470. return
  1471. }
  1472. func TimeTransferString(format string, t time.Time) string {
  1473. str := t.Format(format)
  1474. if t.IsZero() {
  1475. return ""
  1476. }
  1477. return str
  1478. }
  1479. // GetDateByDateType2 通过dateType获取需要的开始/结束日期(日期类型:1:最近3月;2:最近6月;3:最近1年;4:最近2年;5:最近3年;6:最近5年;7:最近10年,8:自定义时间)
  1480. func GetDateByDateType2(dateType int, currDate time.Time) (startDate time.Time) {
  1481. switch dateType {
  1482. case 1:
  1483. startDate = currDate.AddDate(0, -3, 0)
  1484. case 2:
  1485. startDate = currDate.AddDate(0, -6, 0)
  1486. case 3:
  1487. startDate = currDate.AddDate(-1, 0, 0)
  1488. case 4:
  1489. startDate = currDate.AddDate(-2, 0, 0)
  1490. case 5:
  1491. startDate = currDate.AddDate(-3, 0, 0)
  1492. case 6:
  1493. startDate = currDate.AddDate(-5, 0, 0)
  1494. case 7:
  1495. startDate = currDate.AddDate(-10, 0, 0)
  1496. }
  1497. return
  1498. }
  1499. // GetCeilNewNum 保留n位有效数字的向上取整
  1500. // @params num 实际数据
  1501. // @params baseLen 需要保留的有效位数
  1502. func GetCeilNewNum(num float64, baseLen int) (newNum float64) {
  1503. if num >= 1 {
  1504. tmpNum := int(math.Ceil(num)) // 向上取整
  1505. str := strconv.Itoa(tmpNum)
  1506. lenStr := len(str)
  1507. if lenStr > baseLen {
  1508. newNumStr := str[0:baseLen]
  1509. newNumInt, _ := strconv.Atoi(newNumStr)
  1510. newNum = float64(newNumInt) * math.Pow(10, float64(lenStr-baseLen))
  1511. if newNum < num {
  1512. newNumInt += 1
  1513. newNum = float64(newNumInt) * math.Pow(10, float64(lenStr-baseLen))
  1514. }
  1515. } else {
  1516. newNum = float64(tmpNum)
  1517. }
  1518. return
  1519. } else if num > 0 {
  1520. // 这是小数
  1521. str := strconv.FormatFloat(num, 'f', -1, 64)
  1522. // 去除小数点和负号
  1523. str = removeDecimalPoint(str)
  1524. // 计算字符串长度
  1525. lenStr := len(str)
  1526. if lenStr > baseLen {
  1527. newNumStr := str[0:baseLen]
  1528. newNumInt, _ := strconv.Atoi(newNumStr)
  1529. newNum, _ = decimal.NewFromInt(int64(newNumInt)).Div(decimal.NewFromFloat(math.Pow(10, float64(baseLen)))).Float64()
  1530. if newNum < num {
  1531. newNumInt += 1
  1532. newNum, _ = decimal.NewFromInt(int64(newNumInt)).Div(decimal.NewFromFloat(math.Pow(10, float64(baseLen)))).Float64()
  1533. }
  1534. } else {
  1535. newNum = num
  1536. }
  1537. } else if num > -1 {
  1538. // 这是小数
  1539. str := strconv.FormatFloat(num, 'f', -1, 64)
  1540. // 去除小数点和负号
  1541. str = removeDecimalPoint(str)
  1542. // 计算字符串长度
  1543. lenStr := len(str)
  1544. if lenStr > baseLen {
  1545. newNumStr := str[0:baseLen]
  1546. newNumInt, _ := strconv.Atoi(newNumStr)
  1547. newNum, _ = decimal.NewFromInt(int64(newNumInt)).Div(decimal.NewFromFloat(math.Pow(10, float64(baseLen)))).Float64()
  1548. newNum = -newNum
  1549. if newNum < num {
  1550. newNumInt -= 1
  1551. newNum, _ = decimal.NewFromInt(int64(newNumInt)).Div(decimal.NewFromFloat(math.Pow(10, float64(baseLen)))).Float64()
  1552. newNum = -newNum
  1553. }
  1554. } else {
  1555. newNum = num
  1556. }
  1557. if newNum == -0 {
  1558. newNum = 0
  1559. }
  1560. } else { // 小于等于-1
  1561. tmpNumFloat := math.Abs(num)
  1562. tmpNum := int(math.Floor(tmpNumFloat)) // 向上取整
  1563. str := strconv.Itoa(tmpNum)
  1564. lenStr := len(str)
  1565. if lenStr > baseLen {
  1566. newNumStr := str[0:baseLen]
  1567. //fmt.Println("newNumStr:", newNumStr)
  1568. newNumInt, _ := strconv.Atoi(newNumStr)
  1569. newNum = float64(newNumInt) * math.Pow(10, float64(lenStr-baseLen))
  1570. newNum = -newNum
  1571. if newNum < num {
  1572. newNumInt -= 1
  1573. newNum = float64(newNumInt) * math.Pow(10, float64(lenStr-baseLen))
  1574. newNum = -newNum
  1575. }
  1576. } else {
  1577. newNum = float64(-tmpNum)
  1578. }
  1579. }
  1580. return
  1581. }
  1582. // GetFloorNewNum 保留n位有效数字的向下取整
  1583. // @params num 实际数据
  1584. // @params baseLen 需要保留的有效位数
  1585. func GetFloorNewNum(num float64, baseLen int) (newNum float64) {
  1586. if num >= 1 {
  1587. tmpNum := int(math.Floor(num)) // 向上取整
  1588. str := strconv.Itoa(tmpNum)
  1589. lenStr := len(str)
  1590. if lenStr > baseLen {
  1591. newNumStr := str[0:baseLen]
  1592. newNumInt, _ := strconv.Atoi(newNumStr)
  1593. newNum = float64(newNumInt) * math.Pow(10, float64(lenStr-baseLen))
  1594. if newNum < num {
  1595. newNumInt -= 1
  1596. newNum = float64(newNumInt) * math.Pow(10, float64(lenStr-baseLen))
  1597. }
  1598. } else {
  1599. newNum = float64(tmpNum)
  1600. }
  1601. return
  1602. } else if num > 0 {
  1603. // 这是小数
  1604. str := strconv.FormatFloat(num, 'f', -1, 64)
  1605. // 去除小数点和负号
  1606. str = removeDecimalPoint(str)
  1607. // 计算字符串长度
  1608. lenStr := len(str)
  1609. if lenStr > baseLen {
  1610. newNumStr := str[0:baseLen]
  1611. newNumInt, _ := strconv.Atoi(newNumStr)
  1612. newNum, _ = decimal.NewFromInt(int64(newNumInt)).Div(decimal.NewFromFloat(math.Pow(10, float64(baseLen)))).Float64()
  1613. if newNum > num {
  1614. newNumInt -= 1
  1615. newNum, _ = decimal.NewFromInt(int64(newNumInt)).Div(decimal.NewFromFloat(math.Pow(10, float64(baseLen)))).Float64()
  1616. }
  1617. } else {
  1618. newNum = num
  1619. }
  1620. } else if num > -1 {
  1621. // 这是小数
  1622. str := strconv.FormatFloat(num, 'f', -1, 64)
  1623. // 去除小数点和负号
  1624. str = removeDecimalPoint(str)
  1625. // 计算字符串长度
  1626. lenStr := len(str)
  1627. if lenStr > baseLen {
  1628. newNumStr := str[0:baseLen]
  1629. newNumInt, _ := strconv.Atoi(newNumStr)
  1630. newNum, _ = decimal.NewFromInt(int64(newNumInt)).Div(decimal.NewFromFloat(math.Pow(10, float64(baseLen)))).Float64()
  1631. newNum = -newNum
  1632. if newNum > num {
  1633. newNumInt += 1
  1634. newNum, _ = decimal.NewFromInt(int64(newNumInt)).Div(decimal.NewFromFloat(math.Pow(10, float64(baseLen)))).Float64()
  1635. newNum = -newNum
  1636. }
  1637. } else {
  1638. newNum = num
  1639. }
  1640. if newNum == -0 {
  1641. newNum = 0
  1642. }
  1643. } else { // 小于等于-1
  1644. tmpNumFloat := math.Abs(num)
  1645. tmpNum := int(math.Ceil(tmpNumFloat)) // 向上取整
  1646. str := strconv.Itoa(tmpNum)
  1647. lenStr := len(str)
  1648. if lenStr > baseLen {
  1649. newNumStr := str[0:baseLen]
  1650. //fmt.Println("newNumStr:", newNumStr)
  1651. newNumInt, _ := strconv.Atoi(newNumStr)
  1652. newNum = float64(newNumInt) * math.Pow(10, float64(lenStr-baseLen))
  1653. newNum = -newNum
  1654. if newNum > num {
  1655. newNumInt += 1
  1656. newNum = float64(newNumInt) * math.Pow(10, float64(lenStr-baseLen))
  1657. newNum = -newNum
  1658. }
  1659. } else {
  1660. newNum = float64(-tmpNum)
  1661. }
  1662. }
  1663. return
  1664. }
  1665. // 去除小数点和负号
  1666. func removeDecimalPoint(str string) string {
  1667. // 去除小数点
  1668. str = str[strings.Index(str, ".")+1:]
  1669. return str
  1670. }
  1671. // GetPredictEdbDayListByEndDate 根据截止日期获取预测指标日期列表
  1672. func GetPredictEdbDayListByEndDate(startDate, endDate time.Time, frequency string) (dayList []time.Time) {
  1673. //if !utils.InArrayByStr([]string{"日度", "周度", "月度"}, frequency)
  1674. switch frequency {
  1675. case "日度":
  1676. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 1) {
  1677. //周六、日排除
  1678. if currDate.Weekday() == time.Sunday || currDate.Weekday() == time.Saturday {
  1679. continue
  1680. }
  1681. dayList = append(dayList, currDate)
  1682. }
  1683. case "周度":
  1684. //nextDate := startDate.AddDate(0, 0, 7)
  1685. for currDate := startDate.AddDate(0, 0, 7); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 7) {
  1686. dayList = append(dayList, currDate)
  1687. }
  1688. case "旬度":
  1689. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); {
  1690. nextDate := currDate.AddDate(0, 0, 1)
  1691. //每个月的10号、20号、最后一天,那么就写入
  1692. if nextDate.Day() == 11 || nextDate.Day() == 21 || nextDate.Day() == 1 {
  1693. dayList = append(dayList, currDate)
  1694. }
  1695. currDate = nextDate
  1696. }
  1697. case "月度":
  1698. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  1699. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  1700. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  1701. dayList = append(dayList, currDate)
  1702. }
  1703. currDate = currDate.AddDate(0, 0, 1)
  1704. }
  1705. case "季度":
  1706. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  1707. // 每月的最后一天
  1708. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  1709. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  1710. // 季度日期就写入,否则不写入
  1711. if currDate.Month() == 3 || currDate.Month() == 6 || currDate.Month() == 9 || currDate.Month() == 12 {
  1712. dayList = append(dayList, currDate)
  1713. }
  1714. }
  1715. currDate = currDate.AddDate(0, 0, 1)
  1716. }
  1717. case "半年度":
  1718. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  1719. // 每月的最后一天
  1720. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  1721. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  1722. // 半年度日期就写入,否则不写入
  1723. if currDate.Month() == 6 || currDate.Month() == 12 {
  1724. dayList = append(dayList, currDate)
  1725. }
  1726. }
  1727. currDate = currDate.AddDate(0, 0, 1)
  1728. }
  1729. case "年度":
  1730. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  1731. currDate = time.Date(currDate.Year()+1, 12, 31, 0, 0, 0, 0, time.Now().Location())
  1732. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  1733. dayList = append(dayList, currDate)
  1734. }
  1735. }
  1736. }
  1737. return
  1738. }
  1739. // GetPredictEdbDayListByNum 根据期数获取预测指标日期列表
  1740. func GetPredictEdbDayListByNum(startDate time.Time, num int, frequency string) (dayList []time.Time) {
  1741. switch frequency {
  1742. case "日度":
  1743. for i := 1; i <= num; {
  1744. currDate := startDate.AddDate(0, 0, i)
  1745. //周六、日排除
  1746. if currDate.Weekday() == time.Sunday || currDate.Weekday() == time.Saturday {
  1747. continue
  1748. }
  1749. dayList = append(dayList, currDate)
  1750. i++
  1751. }
  1752. case "周度":
  1753. for i := 1; i <= num; {
  1754. dayList = append(dayList, startDate.AddDate(0, 0, i*7))
  1755. i++
  1756. }
  1757. case "旬度":
  1758. currDate := startDate
  1759. for i := 1; i <= num; {
  1760. nextDate := currDate.AddDate(0, 0, 1)
  1761. //每个月的10号、20号、最后一天,那么就写入
  1762. if nextDate.Day() == 11 || nextDate.Day() == 21 || nextDate.Day() == 1 {
  1763. dayList = append(dayList, currDate)
  1764. i++
  1765. }
  1766. currDate = nextDate
  1767. }
  1768. case "月度":
  1769. currDate := startDate
  1770. for i := 1; i <= num; {
  1771. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  1772. if !currDate.Equal(startDate) {
  1773. dayList = append(dayList, currDate)
  1774. i++
  1775. }
  1776. currDate = currDate.AddDate(0, 0, 1)
  1777. }
  1778. case "季度":
  1779. currDate := startDate
  1780. for i := 1; i <= num; {
  1781. // 每月的最后一天
  1782. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  1783. if !currDate.Equal(startDate) {
  1784. // 季度日期就写入,否则不写入
  1785. if currDate.Month() == 3 || currDate.Month() == 6 || currDate.Month() == 9 || currDate.Month() == 12 {
  1786. dayList = append(dayList, currDate)
  1787. i++
  1788. }
  1789. }
  1790. currDate = currDate.AddDate(0, 0, 1)
  1791. }
  1792. case "半年度":
  1793. currDate := startDate
  1794. for i := 1; i <= num; {
  1795. // 每月的最后一天
  1796. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  1797. if !currDate.Equal(startDate) {
  1798. // 半年度日期就写入,否则不写入
  1799. if currDate.Month() == 6 || currDate.Month() == 12 {
  1800. dayList = append(dayList, currDate)
  1801. i++
  1802. }
  1803. }
  1804. currDate = currDate.AddDate(0, 0, 1)
  1805. }
  1806. case "年度":
  1807. currDate := startDate
  1808. for i := 1; i <= num; {
  1809. currDate = time.Date(currDate.Year()+1, 12, 31, 0, 0, 0, 0, time.Now().Location())
  1810. if !currDate.Equal(startDate) {
  1811. dayList = append(dayList, currDate)
  1812. i++
  1813. }
  1814. }
  1815. }
  1816. return
  1817. }
  1818. func GetDateByDateTypeV2(dateType int, tmpStartDate, tmpEndDate string, startYear, yearMax int) (startDate, endDate string) {
  1819. startDate = tmpStartDate
  1820. endDate = tmpEndDate
  1821. switch dateType {
  1822. case 1:
  1823. startDate = "2000-01-01"
  1824. endDate = ""
  1825. case 2:
  1826. startDate = "2010-01-01"
  1827. endDate = ""
  1828. case 3:
  1829. startDate = "2015-01-01"
  1830. endDate = ""
  1831. case 4:
  1832. //startDate = strconv.Itoa(time.Now().Year()) + "-01-01"
  1833. startDate = "2021-01-01"
  1834. endDate = ""
  1835. case 5:
  1836. //startDate = startDate + "-01"
  1837. //endDate = endDate + "-01"
  1838. case 6:
  1839. //startDate = startDate + "-01"
  1840. endDate = ""
  1841. case 7:
  1842. startDate = "2018-01-01"
  1843. endDate = ""
  1844. case 8:
  1845. startDate = "2019-01-01"
  1846. endDate = ""
  1847. case 9:
  1848. startDate = "2020-01-01"
  1849. endDate = ""
  1850. case 11:
  1851. startDate = "2022-01-01"
  1852. endDate = ""
  1853. case DateTypeNYears:
  1854. if startYear == 0 { //默认取最近5年
  1855. startYear = 5
  1856. }
  1857. if yearMax == 0 {
  1858. return
  1859. }
  1860. startYear = startYear - 1
  1861. baseDate, _ := time.Parse(FormatDate, fmt.Sprintf("%d-01-01", yearMax))
  1862. startDate = baseDate.AddDate(-startYear, 0, 0).Format(FormatDate)
  1863. endDate = ""
  1864. }
  1865. // 兼容日期错误
  1866. {
  1867. if strings.Count(startDate, "-") == 1 {
  1868. startDate = startDate + "-01"
  1869. }
  1870. if strings.Count(endDate, "-") == 1 {
  1871. endDate = endDate + "-01"
  1872. }
  1873. }
  1874. return
  1875. }
  1876. // ArrContainsStr 函数检查一个字符串是否在字符串数组中
  1877. func ArrContainsStr(arr []string, str string) bool {
  1878. for _, v := range arr {
  1879. if v == str {
  1880. return true
  1881. }
  1882. }
  1883. return false
  1884. }
  1885. func GetColorMap() map[int]string {
  1886. colorMap := make(map[int]string)
  1887. colors := []string{"#0000FF", "#FF0000", "#999999", "#000000", "#7CB5EC", "#90ED7D", "#F7A35C", "#8085E9",
  1888. "#F15C80", "#E4D354", "#2B908F", "#F45B5B", "#91E8E1", "#FDA8C7", "#8A4294",
  1889. "#578B5A", "#0033FF", "#849EC1", "#FFDF0C", "#005496", "#00F0FF", "#4D535B",
  1890. "#4F4C34", "#804141", "#86BABD", "#8AA3FF", "#960000", "#A173DB", "#A39340",
  1891. "#CE814A", "#D1D2E6", "#EAB7B7", "#FF2E7A", "#FF4AF8", "#FF785B", "#FF9696", "#FFA800", "#FFBC97", "#FFDFDF"}
  1892. for k, v := range colors {
  1893. colorMap[k] = v
  1894. }
  1895. return colorMap
  1896. }
  1897. // IsPercentage
  1898. // @Description: 判断一个字符串是否是百分比的字符串,并返回百分比的值(比如:50%,返回true 50)
  1899. // @author: Roc
  1900. // @datetime 2023-11-29 14:00:31
  1901. // @param input string
  1902. // @return bool
  1903. // @return string
  1904. func IsPercentage(input string) (bool, string) {
  1905. percentagePattern := `^-?(\d+(\.\d+)?)%$`
  1906. match, _ := regexp.MatchString(percentagePattern, input)
  1907. if match {
  1908. re := regexp.MustCompile(percentagePattern)
  1909. match := re.FindStringSubmatch(input)
  1910. // 区分负数和正数,确保返回结果包含负号(如有)
  1911. sign := "-"
  1912. if input[0] != '-' {
  1913. sign = ""
  1914. }
  1915. return true, sign + match[1]
  1916. }
  1917. return false, ""
  1918. }
  1919. // GetLikeKeyword
  1920. //
  1921. // @Description: 获取sql查询中的like查询字段
  1922. // @author: Roc
  1923. // @datetime2023-10-23 14:46:32
  1924. // @param keyword string
  1925. // @return string
  1926. func GetLikeKeyword(keyword string) string {
  1927. return `%` + keyword + `%`
  1928. }
  1929. // GetLikeKeywordPars
  1930. //
  1931. // @Description: 获取sql查询中的参数切片
  1932. // @author: Roc
  1933. // @datetime2023-10-23 14:50:18
  1934. // @param pars []interface{}
  1935. // @param keyword string
  1936. // @param num int
  1937. // @return newPars []interface{}
  1938. func GetLikeKeywordPars(pars []interface{}, keyword string, num int) (newPars []interface{}) {
  1939. newPars = pars
  1940. if newPars == nil {
  1941. newPars = make([]interface{}, 0)
  1942. }
  1943. for i := 1; i <= num; i++ {
  1944. newPars = append(newPars, `%`+keyword+`%`)
  1945. }
  1946. return
  1947. }
  1948. // FormatMixTableDataShowValue 格式化自定表格显示数据
  1949. func FormatMixTableDataShowValue(x float64) (res string) {
  1950. res = fmt.Sprint(x)
  1951. return
  1952. }
  1953. // FormatTableDataShowValue 格式化自定表格显示数据
  1954. func FormatTableDataShowValue(x float64) (res string) {
  1955. if x > 1 || x < -1 {
  1956. res = decimal.NewFromFloat(x).Round(2).String()
  1957. return
  1958. }
  1959. // 介于-1到1之间
  1960. xStr := strconv.FormatFloat(x, 'f', -10, 64)
  1961. // 使用 strings.Split 函数将小数拆分为整数部分和小数部分
  1962. xParts := strings.Split(xStr, ".")
  1963. if len(xParts) < 2 {
  1964. res = fmt.Sprint(x)
  1965. return
  1966. }
  1967. // 计算小数部分的长度,即小数点后面的位数
  1968. xDecimals := len(xParts[1])
  1969. if xDecimals > 2 {
  1970. parts := xParts[1]
  1971. // 小数点后小于等于两位, 直接拼接返回
  1972. partLen := len(xParts[1])
  1973. if partLen <= 2 {
  1974. res = xParts[0] + "." + parts
  1975. return
  1976. }
  1977. // 找出第一个有效数字, 算出n
  1978. one := 0
  1979. for k, p := range parts {
  1980. // 48->0
  1981. if p != 48 {
  1982. if one == 0 {
  1983. one = k + 1
  1984. }
  1985. }
  1986. }
  1987. n := partLen - one
  1988. if n >= 1 {
  1989. n -= 1
  1990. } else {
  1991. one -= 1
  1992. }
  1993. var partFloat float64
  1994. partInt, _ := strconv.Atoi(parts)
  1995. partFloat, _ = decimal.NewFromInt(int64(partInt)).Div(decimal.NewFromFloat(math.Pow(10, float64(n)))).Float64()
  1996. partFloat = math.Round(partFloat)
  1997. partFloat, _ = decimal.NewFromFloat(partFloat).Div(decimal.NewFromFloat(math.Pow(10, float64(one+1)))).Float64()
  1998. resParts := strings.Split(fmt.Sprint(partFloat), ".")
  1999. resPart := ""
  2000. if len(resParts) > 1 {
  2001. resPart = resParts[1]
  2002. } else {
  2003. resPart = resParts[0]
  2004. }
  2005. res = xParts[0] + "." + resPart
  2006. }
  2007. if xDecimals <= 2 {
  2008. xDecimalsStr := xParts[1]
  2009. x, _ = strconv.ParseFloat(xParts[0]+"."+xDecimalsStr, 64)
  2010. res = xParts[0] + "." + xDecimalsStr
  2011. }
  2012. return
  2013. }
  2014. func DealDateTimeZero(t time.Time, format string) (timeStr string) {
  2015. if !t.IsZero() {
  2016. timeStr = t.Format(format)
  2017. }
  2018. return
  2019. }
  2020. // SecondsToHHMMSS 秒转HH:MM:SS
  2021. func SecondsToHHMMSS(seconds int) string {
  2022. duration := time.Duration(seconds) * time.Second
  2023. hours := int(duration.Hours())
  2024. minutes := int(duration.Minutes()) % 60
  2025. seconds = int(duration.Seconds()) % 60
  2026. if hours > 0 {
  2027. return fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
  2028. } else {
  2029. return fmt.Sprintf("%02d:%02d", minutes, seconds)
  2030. }
  2031. }
  2032. // MillisecondsToHHMMSS 毫秒转HH:MM:SS
  2033. func MillisecondsToHHMMSS(ms int) string {
  2034. duration := time.Duration(ms) * time.Millisecond
  2035. hours := int(duration / (time.Hour))
  2036. minutes := int((duration % (time.Hour)) / time.Minute)
  2037. seconds := int((duration % time.Hour % time.Minute) / time.Second)
  2038. // 将整数小时、分钟和秒数转换为字符串,并添加冒号分隔符
  2039. hourStr := strconv.Itoa(hours)
  2040. minuteStr := strconv.Itoa(minutes)
  2041. secondStr := strconv.Itoa(seconds)
  2042. // 根据不足两位数的情况补零
  2043. if len(hourStr) == 1 {
  2044. hourStr = "0" + hourStr
  2045. }
  2046. if len(minuteStr) == 1 {
  2047. minuteStr = "0" + minuteStr
  2048. }
  2049. if len(secondStr) == 1 {
  2050. secondStr = "0" + secondStr
  2051. }
  2052. return hourStr + ":" + minuteStr + ":" + secondStr
  2053. }
  2054. func ByteToMB(byteCount int) float64 {
  2055. return float64(byteCount) / (1024 * 1024)
  2056. }
  2057. // DateConvMysqlConvMongo
  2058. // @Description: 将mysql中的日期比较符转换成mongo中的日期比较符
  2059. // @author: Roc
  2060. // @datetime 2024-05-08 11:03:26
  2061. // @param dateCon string
  2062. func DateConvMysqlConvMongo(dateCon string) string {
  2063. cond := ""
  2064. switch dateCon {
  2065. case "=":
  2066. cond = "$eq"
  2067. case "<":
  2068. cond = "$lt"
  2069. case "<=":
  2070. cond = "$lte"
  2071. case ">":
  2072. cond = "$gt"
  2073. case ">=":
  2074. cond = "$gte"
  2075. }
  2076. return cond
  2077. }
  2078. // handleSystemAppointDateT
  2079. // @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  2080. // @author: Roc
  2081. // @datetime2023-10-27 09:31:35
  2082. // @param Frequency string
  2083. // @param Day string
  2084. // @return date string
  2085. // @return err error
  2086. // @return errMsg string
  2087. func HandleSystemAppointDateT(currDate time.Time, appointDay, frequency string) (date string, err error, errMsg string) {
  2088. //currDate := time.Now()
  2089. switch frequency {
  2090. case "本周":
  2091. day := int(currDate.Weekday())
  2092. if day == 0 { // 周日
  2093. day = 7
  2094. }
  2095. num := 0
  2096. switch appointDay {
  2097. case "周一":
  2098. num = 1
  2099. case "周二":
  2100. num = 2
  2101. case "周三":
  2102. num = 3
  2103. case "周四":
  2104. num = 4
  2105. case "周五":
  2106. num = 5
  2107. case "周六":
  2108. num = 6
  2109. case "周日":
  2110. num = 7
  2111. }
  2112. day = num - day
  2113. date = currDate.AddDate(0, 0, day).Format(FormatDate)
  2114. case "本旬":
  2115. day := currDate.Day()
  2116. var tmpDate time.Time
  2117. switch appointDay {
  2118. case "第一天":
  2119. if day <= 10 {
  2120. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  2121. } else if day <= 20 {
  2122. tmpDate = time.Date(currDate.Year(), currDate.Month(), 11, 0, 0, 0, 0, currDate.Location())
  2123. } else {
  2124. tmpDate = time.Date(currDate.Year(), currDate.Month(), 21, 0, 0, 0, 0, currDate.Location())
  2125. }
  2126. case "最后一天":
  2127. if day <= 10 {
  2128. tmpDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, currDate.Location())
  2129. } else if day <= 20 {
  2130. tmpDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, currDate.Location())
  2131. } else {
  2132. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  2133. }
  2134. }
  2135. date = tmpDate.Format(FormatDate)
  2136. case "本月":
  2137. var tmpDate time.Time
  2138. switch appointDay {
  2139. case "第一天":
  2140. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  2141. case "最后一天":
  2142. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  2143. }
  2144. date = tmpDate.Format(FormatDate)
  2145. case "本季":
  2146. month := currDate.Month()
  2147. var tmpDate time.Time
  2148. switch appointDay {
  2149. case "第一天":
  2150. if month <= 3 {
  2151. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  2152. } else if month <= 6 {
  2153. tmpDate = time.Date(currDate.Year(), 4, 1, 0, 0, 0, 0, currDate.Location())
  2154. } else if month <= 9 {
  2155. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  2156. } else {
  2157. tmpDate = time.Date(currDate.Year(), 10, 1, 0, 0, 0, 0, currDate.Location())
  2158. }
  2159. case "最后一天":
  2160. if month <= 3 {
  2161. tmpDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, currDate.Location())
  2162. } else if month <= 6 {
  2163. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  2164. } else if month <= 9 {
  2165. tmpDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, currDate.Location())
  2166. } else {
  2167. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  2168. }
  2169. }
  2170. date = tmpDate.Format(FormatDate)
  2171. case "本半年":
  2172. month := currDate.Month()
  2173. var tmpDate time.Time
  2174. switch appointDay {
  2175. case "第一天":
  2176. if month <= 6 {
  2177. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  2178. } else {
  2179. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  2180. }
  2181. case "最后一天":
  2182. if month <= 6 {
  2183. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  2184. } else {
  2185. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  2186. }
  2187. }
  2188. date = tmpDate.Format(FormatDate)
  2189. case "本年":
  2190. var tmpDate time.Time
  2191. switch appointDay {
  2192. case "第一天":
  2193. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  2194. case "最后一天":
  2195. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  2196. }
  2197. date = tmpDate.Format(FormatDate)
  2198. default:
  2199. errMsg = "错误的日期频度:" + frequency
  2200. err = errors.New(errMsg)
  2201. return
  2202. }
  2203. return
  2204. }
  2205. func CompareFloatByOpStrings(op string, a, b float64) bool {
  2206. switch op {
  2207. case "=":
  2208. return a == b
  2209. case ">":
  2210. return a > b
  2211. case ">=":
  2212. return a >= b
  2213. case "<=":
  2214. return a <= b
  2215. case "<":
  2216. return a < b
  2217. }
  2218. return false
  2219. }