time.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Author : Eric Kim
  2. // Build Date : 23 Jul 2018 Last Update 02 Aug 2018
  3. // End-Agent for Passcon Multi OS go binding with Windows, MacOS, iOS, and Android
  4. // All rights are reserved.
  5. package etc
  6. import (
  7. "errors"
  8. time "time"
  9. )
  10. func getNow() time.Time {
  11. loc, _ := time.LoadLocation("UTC")
  12. return time.Now().In(loc)
  13. }
  14. func GetNowUnix(sec ...int) int64 {
  15. var ret int64
  16. if sec == nil {
  17. ret = time.Now().UTC().Unix()
  18. } else {
  19. ret = time.Now().Add(time.Duration(sec[0]) * time.Second).UTC().Unix()
  20. }
  21. return ret
  22. }
  23. func GetNowDate(i int) string {
  24. format := "060102"
  25. if i == 8 {
  26. format = "20060102"
  27. } else if i == 6 {
  28. format = "060102"
  29. }
  30. return time.Now().Format(format)
  31. }
  32. func YyyyMmDdToUnixInterval(yyyymmdd string) (string, string, error) {
  33. // fmt.Println("yyyymmdd", yyyymmdd)
  34. if len(yyyymmdd) != 8 {
  35. return "", "", ErrLog(FuncRun("xcawrq ", CurrFuncName()), errors.New("yyyymmdd format size should be 8"))
  36. }
  37. firstDayStr := ""
  38. lastDayStr := ""
  39. monthStr := yyyymmdd[6:8]
  40. if monthStr == "00" {
  41. // fmt.Println("monthStr", yyyymmdd[:6])
  42. date, err := time.Parse("200601", yyyymmdd[:6])
  43. if err != nil {
  44. return "", "", err
  45. }
  46. firstDay := time.Date(date.Year(), date.Month(), 1, -0, 0, 0, 0, time.UTC)
  47. lastDay := firstDay.AddDate(0, 1, 0).Add(-time.Second)
  48. firstDayStr = NumToStr(firstDay.Unix())
  49. lastDayStr = NumToStr(lastDay.Unix())
  50. // fmt.Println("firstDay", firstDay)
  51. // fmt.Println("lastDay", lastDay)
  52. } else {
  53. date, err := time.Parse("20060102", yyyymmdd)
  54. if err != nil {
  55. return "", "", err
  56. }
  57. firstDay := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.UTC)
  58. lastDay := time.Date(date.Year(), date.Month(), date.Day(), 23, 59, 59, 999999999, time.UTC)
  59. firstDayStr = NumToStr(firstDay.Unix())
  60. lastDayStr = NumToStr(lastDay.Unix())
  61. // fmt.Println("firstDay", firstDay)
  62. // fmt.Println("lastDay", lastDay)
  63. }
  64. return firstDayStr, lastDayStr, nil
  65. }
  66. func YyyyMmToStringInterval(yyyymm string) (string, string, error) {
  67. if len(yyyymm) != 6 {
  68. return "", "", ErrLog(FuncRun("xdcawrq ", CurrFuncName()), errors.New("yyyymmdd format size should be 6"))
  69. }
  70. date, err := time.Parse("200601", yyyymm)
  71. if err != nil {
  72. return "", "", err
  73. }
  74. firstDay := time.Date(date.Year(), date.Month(), 1, -0, 0, 0, 0, time.UTC)
  75. lastDay := firstDay.AddDate(0, 1, 0).Add(-time.Second)
  76. // fmt.Println("firstDay", firstDay.Format("20060102"))
  77. // fmt.Println("lastDay", lastDay.Format("20060102"))
  78. return firstDay.Format("20060102"), lastDay.Format("20060102"), nil
  79. }
  80. func UnixToYyyyMm(unix int64) string {
  81. if !IsUnixTime(unix) {
  82. LogErr("JHLSAKAS", "", errors.New(NumToStr(unix)+" is Not a Unix Time"))
  83. return "ERRORS"
  84. }
  85. return time.Unix(unix, 0).Format("200601")
  86. }
  87. func IsUnixTime(timestamp int64) bool {
  88. minUnixTime := int64(0)
  89. maxUnixTime := time.Now().Unix() + 10*365*24*60*60 // 10 years in seconds
  90. return timestamp >= minUnixTime && timestamp <= maxUnixTime
  91. }