common.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. package utils
  2. import (
  3. "crypto/hmac"
  4. "crypto/md5"
  5. "crypto/sha1"
  6. "crypto/sha256"
  7. "encoding/base64"
  8. "encoding/hex"
  9. "encoding/json"
  10. "errors"
  11. "fmt"
  12. "image"
  13. "image/png"
  14. "math"
  15. "math/rand"
  16. "net"
  17. "os"
  18. "os/exec"
  19. "regexp"
  20. "strconv"
  21. "strings"
  22. "time"
  23. )
  24. func GetRandString(size int) string {
  25. 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", "!", "@", "#", "$", "%", "^", "&", "*"}
  26. randomSb := ""
  27. digitSize := len(allLetterDigit)
  28. rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
  29. for i := 0; i < size; i++ {
  30. randomSb += allLetterDigit[rnd.Intn(digitSize)]
  31. }
  32. return randomSb
  33. }
  34. func GetRandStringNoSpecialChar(size int) string {
  35. 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"}
  36. randomSb := ""
  37. digitSize := len(allLetterDigit)
  38. rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
  39. for i := 0; i < size; i++ {
  40. randomSb += allLetterDigit[rnd.Intn(digitSize)]
  41. }
  42. return randomSb
  43. }
  44. func StringsToJSON(str string) string {
  45. rs := []rune(str)
  46. jsons := ""
  47. for _, r := range rs {
  48. rint := int(r)
  49. if rint < 128 {
  50. jsons += string(r)
  51. } else {
  52. jsons += "\\u" + strconv.FormatInt(int64(rint), 16) // json
  53. }
  54. }
  55. return jsons
  56. }
  57. func ToString(v interface{}) string {
  58. data, _ := json.Marshal(v)
  59. return string(data)
  60. }
  61. func MD5(data string) string {
  62. m := md5.Sum([]byte(data))
  63. return hex.EncodeToString(m[:])
  64. }
  65. func GetToday(format string) string {
  66. today := time.Now().Format(format)
  67. return today
  68. }
  69. func GetTodayLastSecond() time.Duration {
  70. today := GetToday(FormatDate) + " 23:59:59"
  71. end, _ := time.ParseInLocation(FormatDateTime, today, time.Local)
  72. return time.Duration(end.Unix()-time.Now().Local().Unix()) * time.Second
  73. }
  74. func GetBrithDate(idcard string) string {
  75. l := len(idcard)
  76. var s string
  77. if l == 15 {
  78. s = "19" + idcard[6:8] + "-" + idcard[8:10] + "-" + idcard[10:12]
  79. return s
  80. }
  81. if l == 18 {
  82. s = idcard[6:10] + "-" + idcard[10:12] + "-" + idcard[12:14]
  83. return s
  84. }
  85. return GetToday(FormatDate)
  86. }
  87. func WhichSexByIdcard(idcard string) string {
  88. var sexs = [2]string{"女", "男"}
  89. length := len(idcard)
  90. if length == 18 {
  91. sex, _ := strconv.Atoi(string(idcard[16]))
  92. return sexs[sex%2]
  93. } else if length == 15 {
  94. sex, _ := strconv.Atoi(string(idcard[14]))
  95. return sexs[sex%2]
  96. }
  97. return "男"
  98. }
  99. func SubFloatToString(f float64, m int) string {
  100. n := strconv.FormatFloat(f, 'f', -1, 64)
  101. if n == "" {
  102. return ""
  103. }
  104. if m >= len(n) {
  105. return n
  106. }
  107. newn := strings.Split(n, ".")
  108. if m == 0 {
  109. return newn[0]
  110. }
  111. if len(newn) < 2 || m >= len(newn[1]) {
  112. return n
  113. }
  114. return newn[0] + "." + newn[1][:m]
  115. }
  116. func SubFloatToFloat(f float64, m int) float64 {
  117. newn := SubFloatToString(f, m)
  118. newf, _ := strconv.ParseFloat(newn, 64)
  119. return newf
  120. }
  121. func GetYearDiffer(start_time, end_time string) int {
  122. t1, _ := time.ParseInLocation("2006-01-02", start_time, time.Local)
  123. t2, _ := time.ParseInLocation("2006-01-02", end_time, time.Local)
  124. age := t2.Year() - t1.Year()
  125. if t2.Month() < t1.Month() || (t2.Month() == t1.Month() && t2.Day() < t1.Day()) {
  126. age--
  127. }
  128. return age
  129. }
  130. func GetSecondDifferByTime(start_time, end_time time.Time) int64 {
  131. diff := end_time.Unix() - start_time.Unix()
  132. return diff
  133. }
  134. func FixFloat(f float64, m int) float64 {
  135. newn := SubFloatToString(f+0.00000001, m)
  136. newf, _ := strconv.ParseFloat(newn, 64)
  137. return newf
  138. }
  139. func StrListToString(strList []string) (str string) {
  140. if len(strList) > 0 {
  141. for k, v := range strList {
  142. if k == 0 {
  143. str = v
  144. } else {
  145. str = str + "," + v
  146. }
  147. }
  148. return
  149. }
  150. return ""
  151. }
  152. func ErrNoRow() string {
  153. return "<QuerySeter> no row found"
  154. }
  155. func ValidateEmailFormatat(email string) bool {
  156. reg := regexp.MustCompile(RegularEmail)
  157. return reg.MatchString(email)
  158. }
  159. func ValidateMobileFormatat(mobileNum string) bool {
  160. reg := regexp.MustCompile(RegularMobile)
  161. return reg.MatchString(mobileNum)
  162. }
  163. func FileIsExist(filePath string) bool {
  164. _, err := os.Stat(filePath)
  165. return err == nil || os.IsExist(err)
  166. }
  167. func GetImgExt(file string) (ext string, err error) {
  168. var headerByte []byte
  169. headerByte = make([]byte, 8)
  170. fd, err := os.Open(file)
  171. if err != nil {
  172. return "", err
  173. }
  174. defer fd.Close()
  175. _, err = fd.Read(headerByte)
  176. if err != nil {
  177. return "", err
  178. }
  179. xStr := fmt.Sprintf("%x", headerByte)
  180. switch {
  181. case xStr == "89504e470d0a1a0a":
  182. ext = ".png"
  183. case xStr == "0000010001002020":
  184. ext = ".ico"
  185. case xStr == "0000020001002020":
  186. ext = ".cur"
  187. case xStr[:12] == "474946383961" || xStr[:12] == "474946383761":
  188. ext = ".gif"
  189. case xStr[:10] == "0000020000" || xStr[:10] == "0000100000":
  190. ext = ".tga"
  191. case xStr[:8] == "464f524d":
  192. ext = ".iff"
  193. case xStr[:8] == "52494646":
  194. ext = ".ani"
  195. case xStr[:4] == "4d4d" || xStr[:4] == "4949":
  196. ext = ".tiff"
  197. case xStr[:4] == "424d":
  198. ext = ".bmp"
  199. case xStr[:4] == "ffd8":
  200. ext = ".jpg"
  201. case xStr[:2] == "0a":
  202. ext = ".pcx"
  203. default:
  204. ext = ""
  205. }
  206. return ext, nil
  207. }
  208. func SaveImage(path string, img image.Image) (err error) {
  209. imgfile, err := os.Create(path)
  210. defer imgfile.Close()
  211. err = png.Encode(imgfile, img)
  212. return err
  213. }
  214. func SaveBase64ToFile(content, path string) error {
  215. data, err := base64.StdEncoding.DecodeString(content)
  216. if err != nil {
  217. return err
  218. }
  219. f, err := os.Create(path)
  220. defer f.Close()
  221. if err != nil {
  222. return err
  223. }
  224. f.Write(data)
  225. return nil
  226. }
  227. func SaveBase64ToFileBySeek(content, path string) (err error) {
  228. data, err := base64.StdEncoding.DecodeString(content)
  229. exist, err := PathExists(path)
  230. if err != nil {
  231. return
  232. }
  233. if !exist {
  234. f, err := os.Create(path)
  235. if err != nil {
  236. return err
  237. }
  238. n, _ := f.Seek(0, 2)
  239. _, err = f.WriteAt([]byte(data), n)
  240. defer f.Close()
  241. } else {
  242. f, err := os.OpenFile(path, os.O_WRONLY, 0644)
  243. if err != nil {
  244. return err
  245. }
  246. n, _ := f.Seek(0, 2)
  247. _, err = f.WriteAt([]byte(data), n)
  248. defer f.Close()
  249. }
  250. return nil
  251. }
  252. func PathExists(path string) (bool, error) {
  253. _, err := os.Stat(path)
  254. if err == nil {
  255. return true, nil
  256. }
  257. if os.IsNotExist(err) {
  258. return false, nil
  259. }
  260. return false, err
  261. }
  262. func StartIndex(page, pagesize int) int {
  263. if page > 1 {
  264. return (page - 1) * pagesize
  265. }
  266. return 0
  267. }
  268. func PageCount(count, pagesize int) int {
  269. if count%pagesize > 0 {
  270. return count/pagesize + 1
  271. } else {
  272. return count / pagesize
  273. }
  274. }
  275. func TrimHtml(src string) string {
  276. re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
  277. src = re.ReplaceAllStringFunc(src, strings.ToLower)
  278. re, _ = regexp.Compile("\\<img[\\S\\s]+?\\>")
  279. src = re.ReplaceAllString(src, "[图片]")
  280. re, _ = regexp.Compile("class[\\S\\s]+?>")
  281. src = re.ReplaceAllString(src, "")
  282. re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
  283. src = re.ReplaceAllString(src, "")
  284. return strings.TrimSpace(src)
  285. }
  286. func TimeToTimestamp() {
  287. fmt.Println(time.Unix(1556164246, 0).Format("2006-01-02 15:04:05"))
  288. }
  289. func ToUnicode(text string) string {
  290. textQuoted := strconv.QuoteToASCII(text)
  291. textUnquoted := textQuoted[1 : len(textQuoted)-1]
  292. return textUnquoted
  293. }
  294. func VersionToInt(version string) int {
  295. version = strings.Replace(version, ".", "", -1)
  296. n, _ := strconv.Atoi(version)
  297. return n
  298. }
  299. func IsCheckInList(list []int, s int) bool {
  300. for _, v := range list {
  301. if v == s {
  302. return true
  303. }
  304. }
  305. return false
  306. }
  307. func round(num float64) int {
  308. return int(num + math.Copysign(0.5, num))
  309. }
  310. func toFixed(num float64, precision int) float64 {
  311. output := math.Pow(10, float64(precision))
  312. return float64(round(num*output)) / output
  313. }
  314. func GetWilsonScore(p, n float64) float64 {
  315. if p == 0 && n == 0 {
  316. return 0
  317. }
  318. 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)
  319. }
  320. func ChangeWordsToNum(str string) (numStr string) {
  321. words := ([]rune)(str)
  322. num := 0
  323. n := 0
  324. for i := 0; i < len(words); i++ {
  325. word := string(words[i : i+1])
  326. switch word {
  327. case "万":
  328. if n == 0 {
  329. n = 1
  330. }
  331. n = n * 10000
  332. num = num*10000 + n
  333. n = 0
  334. case "千":
  335. if n == 0 {
  336. n = 1
  337. }
  338. n = n * 1000
  339. num += n
  340. n = 0
  341. case "百":
  342. if n == 0 {
  343. n = 1
  344. }
  345. n = n * 100
  346. num += n
  347. n = 0
  348. case "十":
  349. if n == 0 {
  350. n = 1
  351. }
  352. n = n * 10
  353. num += n
  354. n = 0
  355. case "一":
  356. n += 1
  357. case "二":
  358. n += 2
  359. case "三":
  360. n += 3
  361. case "四":
  362. n += 4
  363. case "五":
  364. n += 5
  365. case "六":
  366. n += 6
  367. case "七":
  368. n += 7
  369. case "八":
  370. n += 8
  371. case "九":
  372. n += 9
  373. case "零":
  374. default:
  375. if n > 0 {
  376. num += n
  377. n = 0
  378. }
  379. if num == 0 {
  380. numStr += word
  381. } else {
  382. numStr += strconv.Itoa(num) + word
  383. num = 0
  384. }
  385. }
  386. }
  387. if n > 0 {
  388. num += n
  389. n = 0
  390. }
  391. if num != 0 {
  392. numStr += strconv.Itoa(num)
  393. }
  394. return
  395. }
  396. func Sha1(data string) string {
  397. sha1 := sha1.New()
  398. sha1.Write([]byte(data))
  399. return hex.EncodeToString(sha1.Sum([]byte("")))
  400. }
  401. func GetWeekDay() (weekStr string) {
  402. nowWeek := time.Now().Weekday().String()
  403. switch nowWeek {
  404. case "Monday":
  405. weekStr = "周一"
  406. break
  407. case "Tuesday":
  408. weekStr = "周二"
  409. break
  410. case "Wednesday":
  411. weekStr = "周三"
  412. break
  413. case "Thursday":
  414. weekStr = "周四"
  415. break
  416. case "Friday":
  417. weekStr = "周五"
  418. break
  419. case "Saturday":
  420. weekStr = "周六"
  421. break
  422. case "Sunday":
  423. weekStr = "周日"
  424. break
  425. default:
  426. weekStr = ""
  427. break
  428. }
  429. return
  430. }
  431. func GetNowWeekMonday() time.Time {
  432. offset := int(time.Monday - time.Now().Weekday())
  433. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  434. offset = -6
  435. }
  436. mondayTime := time.Now().AddDate(0, 0, offset)
  437. mondayTime = time.Date(mondayTime.Year(), mondayTime.Month(), mondayTime.Day(), 0, 0, 0, 0, mondayTime.Location())
  438. return mondayTime
  439. }
  440. func GetLastWeekMonday() time.Time {
  441. offset := int(time.Monday - time.Now().Weekday())
  442. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  443. offset = -6
  444. }
  445. mondayTime := time.Now().AddDate(0, 0, offset-7)
  446. mondayTime = time.Date(mondayTime.Year(), mondayTime.Month(), mondayTime.Day(), 0, 0, 0, 0, mondayTime.Location())
  447. return mondayTime
  448. }
  449. func GetNowWeekTuesday() time.Time {
  450. offset := int(time.Tuesday - time.Now().Weekday())
  451. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  452. offset = -6
  453. }
  454. mondayTime := time.Now().AddDate(0, 0, offset)
  455. mondayTime = time.Date(mondayTime.Year(), mondayTime.Month(), mondayTime.Day(), 0, 0, 0, 0, mondayTime.Location())
  456. return mondayTime
  457. }
  458. func GetLastWeekTuesday() time.Time {
  459. offset := int(time.Tuesday - time.Now().Weekday())
  460. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  461. offset = -6
  462. }
  463. mondayTime := time.Now().AddDate(0, 0, offset-7)
  464. mondayTime = time.Date(mondayTime.Year(), mondayTime.Month(), mondayTime.Day(), 0, 0, 0, 0, mondayTime.Location())
  465. return mondayTime
  466. }
  467. func GetNowWeekThursday() time.Time {
  468. offset := int(time.Thursday - time.Now().Weekday())
  469. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  470. offset = -6
  471. }
  472. fridayTime := time.Now().AddDate(0, 0, offset)
  473. fridayTime = time.Date(fridayTime.Year(), fridayTime.Month(), fridayTime.Day(), 0, 0, 0, 0, fridayTime.Location())
  474. return fridayTime
  475. }
  476. func GetLastWeekThursday() time.Time {
  477. offset := int(time.Thursday - time.Now().Weekday())
  478. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  479. offset = -6
  480. }
  481. fridayTime := time.Now().AddDate(0, 0, offset-7)
  482. fridayTime = time.Date(fridayTime.Year(), fridayTime.Month(), fridayTime.Day(), 0, 0, 0, 0, fridayTime.Location())
  483. return fridayTime
  484. }
  485. func GetNowWeekFriday() time.Time {
  486. offset := int(time.Friday - time.Now().Weekday())
  487. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  488. offset = -6
  489. }
  490. fridayTime := time.Now().AddDate(0, 0, offset)
  491. fridayTime = time.Date(fridayTime.Year(), fridayTime.Month(), fridayTime.Day(), 0, 0, 0, 0, fridayTime.Location())
  492. return fridayTime
  493. }
  494. func GetLastWeekFriday() time.Time {
  495. offset := int(time.Friday - time.Now().Weekday())
  496. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  497. offset = -6
  498. }
  499. fridayTime := time.Now().AddDate(0, 0, offset-7)
  500. fridayTime = time.Date(fridayTime.Year(), fridayTime.Month(), fridayTime.Day(), 0, 0, 0, 0, fridayTime.Location())
  501. return fridayTime
  502. }
  503. func GetNowWeekLastDay() time.Time {
  504. offset := int(time.Monday - time.Now().Weekday())
  505. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  506. offset = -6
  507. }
  508. firstDayTime := time.Now().AddDate(0, 0, offset)
  509. firstDayTime = time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 0, 0, 0, 0, firstDayTime.Location()).AddDate(0, 0, 6)
  510. lastDayTime := time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 23, 59, 59, 0, firstDayTime.Location())
  511. return lastDayTime
  512. }
  513. func GetNowMonthFirstDay() time.Time {
  514. nowMonthFirstDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location())
  515. return nowMonthFirstDay
  516. }
  517. func GetNowMonthLastDay() time.Time {
  518. nowMonthLastDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  519. nowMonthLastDay = time.Date(nowMonthLastDay.Year(), nowMonthLastDay.Month(), nowMonthLastDay.Day(), 23, 59, 59, 0, nowMonthLastDay.Location())
  520. return nowMonthLastDay
  521. }
  522. func GetNowQuarterFirstDay() time.Time {
  523. month := int(time.Now().Month())
  524. var nowQuarterFirstDay time.Time
  525. if month >= 1 && month <= 3 {
  526. nowQuarterFirstDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  527. } else if month >= 4 && month <= 6 {
  528. nowQuarterFirstDay = time.Date(time.Now().Year(), 4, 1, 0, 0, 0, 0, time.Now().Location())
  529. } else if month >= 7 && month <= 9 {
  530. nowQuarterFirstDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
  531. } else {
  532. nowQuarterFirstDay = time.Date(time.Now().Year(), 10, 1, 0, 0, 0, 0, time.Now().Location())
  533. }
  534. return nowQuarterFirstDay
  535. }
  536. func GetNowQuarterLastDay() time.Time {
  537. month := int(time.Now().Month())
  538. var nowQuarterLastDay time.Time
  539. if month >= 1 && month <= 3 {
  540. nowQuarterLastDay = time.Date(time.Now().Year(), 3, 31, 23, 59, 59, 0, time.Now().Location())
  541. } else if month >= 4 && month <= 6 {
  542. nowQuarterLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
  543. } else if month >= 7 && month <= 9 {
  544. nowQuarterLastDay = time.Date(time.Now().Year(), 9, 30, 23, 59, 59, 0, time.Now().Location())
  545. } else {
  546. nowQuarterLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  547. }
  548. return nowQuarterLastDay
  549. }
  550. func GetNowHalfYearFirstDay() time.Time {
  551. month := int(time.Now().Month())
  552. var nowHalfYearLastDay time.Time
  553. if month >= 1 && month <= 6 {
  554. nowHalfYearLastDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  555. } else {
  556. nowHalfYearLastDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
  557. }
  558. return nowHalfYearLastDay
  559. }
  560. func GetNowHalfYearLastDay() time.Time {
  561. month := int(time.Now().Month())
  562. var nowHalfYearLastDay time.Time
  563. if month >= 1 && month <= 6 {
  564. nowHalfYearLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
  565. } else {
  566. nowHalfYearLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  567. }
  568. return nowHalfYearLastDay
  569. }
  570. func GetNowYearFirstDay() time.Time {
  571. nowYearFirstDay := time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  572. return nowYearFirstDay
  573. }
  574. func GetNowYearLastDay() time.Time {
  575. nowYearLastDay := time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  576. return nowYearLastDay
  577. }
  578. func CalculationDate(startDate, endDate time.Time) (beetweenDay string, err error) {
  579. numYear := endDate.Year() - startDate.Year()
  580. numMonth := int(endDate.Month()) - int(startDate.Month())
  581. numDay := 0
  582. endDateDays := getMonthDay(endDate.Year(), int(endDate.Month()))
  583. endDatePrevMonthDate := endDate.AddDate(0, -1, 0)
  584. endDatePrevMonthDays := getMonthDay(endDatePrevMonthDate.Year(), int(endDatePrevMonthDate.Month()))
  585. startDateMonthDays := getMonthDay(startDate.Year(), int(startDate.Month()))
  586. if endDate.Day() == endDateDays {
  587. numDay = startDateMonthDays - startDate.Day() + 1
  588. if numDay == startDateMonthDays {
  589. numMonth++
  590. numDay = 0
  591. if numMonth == 12 {
  592. numYear++
  593. numMonth = 0
  594. }
  595. }
  596. } else {
  597. numDay = endDate.Day() - startDate.Day() + 1
  598. }
  599. if numDay < 0 {
  600. numDay += endDatePrevMonthDays
  601. numMonth = numMonth - 1
  602. }
  603. if numMonth < 0 {
  604. numMonth += 12
  605. numYear = numYear - 1
  606. }
  607. if numYear < 0 {
  608. err = errors.New("日期异常")
  609. return
  610. }
  611. if numYear > 0 {
  612. beetweenDay += fmt.Sprint(numYear, "年")
  613. }
  614. if numMonth > 0 {
  615. beetweenDay += fmt.Sprint(numMonth, "个月")
  616. }
  617. if numDay > 0 {
  618. beetweenDay += fmt.Sprint(numDay, "天")
  619. }
  620. return
  621. }
  622. func getMonthDay(year, month int) (days int) {
  623. if month != 2 {
  624. if month == 4 || month == 6 || month == 9 || month == 11 {
  625. days = 30
  626. } else {
  627. days = 31
  628. }
  629. } else {
  630. if ((year%4) == 0 && (year%100) != 0) || (year%400) == 0 {
  631. days = 29
  632. } else {
  633. days = 28
  634. }
  635. }
  636. return
  637. }
  638. func SubStr(str string, subLen int) string {
  639. strRune := []rune(str)
  640. bodyRuneLen := len(strRune)
  641. if bodyRuneLen > subLen {
  642. bodyRuneLen = subLen
  643. }
  644. str = string(strRune[:bodyRuneLen])
  645. return str
  646. }
  647. func InArrayByInt(idIntList []int, searchId int) (has bool) {
  648. for _, id := range idIntList {
  649. if id == searchId {
  650. has = true
  651. return
  652. }
  653. }
  654. return
  655. }
  656. func InArrayByStr(idStrList []string, searchId string) (has bool) {
  657. for _, id := range idStrList {
  658. if id == searchId {
  659. has = true
  660. return
  661. }
  662. }
  663. return
  664. }
  665. func GetLocalIP() (ip string, err error) {
  666. addrs, err := net.InterfaceAddrs()
  667. if err != nil {
  668. return
  669. }
  670. for _, addr := range addrs {
  671. ipAddr, ok := addr.(*net.IPNet)
  672. if !ok {
  673. continue
  674. }
  675. if ipAddr.IP.IsLoopback() {
  676. continue
  677. }
  678. if !ipAddr.IP.IsGlobalUnicast() {
  679. continue
  680. }
  681. return ipAddr.IP.String(), nil
  682. }
  683. return
  684. }
  685. func GetRichText(content string) (contentSub string) {
  686. contentSub = strings.Replace(content, "<p data-f-id=\"pbf\" style=\"text-align: center; font-size: 14px; margin-top: 30px; opacity: 0.65; font-family: sans-serif;\">Powered by <a href=\"https://www.froala.com/wysiwyg-editor?pb=1\" title=\"Froala Editor\">Froala Editor</a></p>", "", -1)
  687. return
  688. }
  689. func GetOrmInReplace(num int) string {
  690. template := make([]string, num)
  691. for i := 0; i < num; i++ {
  692. template[i] = "?"
  693. }
  694. return strings.Join(template, ",")
  695. }
  696. func SubFloatToFloatStr(f float64, m int) string {
  697. newn := SubFloatToString(f, m)
  698. return newn
  699. }
  700. func GetVideoPlaySeconds(videoPath string) (playSeconds float64, err error) {
  701. cmd := fmt.Sprintf(`ffmpeg -i %s 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//`, videoPath)
  702. out, err := exec.Command("bash", "-c", cmd).Output()
  703. if err != nil {
  704. return
  705. }
  706. outTimes := string(out)
  707. fmt.Println("outTimes:", outTimes)
  708. if outTimes != "" {
  709. timeArr := strings.Split(outTimes, ":")
  710. h := timeArr[0]
  711. m := timeArr[1]
  712. s := timeArr[2]
  713. hInt, err := strconv.Atoi(h)
  714. if err != nil {
  715. return playSeconds, err
  716. }
  717. mInt, err := strconv.Atoi(m)
  718. if err != nil {
  719. return playSeconds, err
  720. }
  721. s = strings.Trim(s, " ")
  722. s = strings.Trim(s, "\n")
  723. sInt, err := strconv.ParseFloat(s, 64)
  724. if err != nil {
  725. return playSeconds, err
  726. }
  727. playSeconds = float64(hInt)*3600 + float64(mInt)*60 + float64(sInt)
  728. }
  729. return
  730. }
  731. func GetTimeSubDay(t1, t2 time.Time) int {
  732. var day int
  733. swap := false
  734. if t1.Unix() > t2.Unix() {
  735. t1, t2 = t2, t1
  736. swap = true
  737. }
  738. t1_ := t1.Add(time.Duration(t2.Sub(t1).Milliseconds()%86400000) * time.Millisecond)
  739. day = int(t2.Sub(t1).Hours() / 24)
  740. if t1_.Day() != t1.Day() {
  741. day += 1
  742. }
  743. if swap {
  744. day = -day
  745. }
  746. return day
  747. }
  748. func HmacSha256(key string, data string) []byte {
  749. mac := hmac.New(sha256.New, []byte(key))
  750. _, _ = mac.Write([]byte(data))
  751. return mac.Sum(nil)
  752. }
  753. func HmacSha256ToBase64(key string, data string) string {
  754. return base64.URLEncoding.EncodeToString(HmacSha256(key, data))
  755. }
  756. func MinusInt(a []int, b []int) []int {
  757. var diff []int
  758. mpA, mpB := make(map[int]bool), make(map[int]bool)
  759. for _, v := range a {
  760. mpA[v] = true
  761. }
  762. for _, v := range b {
  763. mpB[v] = true
  764. }
  765. for _, s := range a {
  766. if !mpB[s] {
  767. diff = append(diff, s)
  768. }
  769. }
  770. for _, s := range b {
  771. if !mpA[s] {
  772. diff = append(diff, s)
  773. }
  774. }
  775. return diff
  776. }