datetime.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package datetime
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. // Time get timestamp
  7. // php time
  8. func Time() int64 {
  9. return time.Now().Unix()
  10. }
  11. // StrToTime parse time_string to timestamp
  12. // php strtotime
  13. func StrToTime(timeStr, layout string) (int64, error) {
  14. t, err := time.Parse(layout, timeStr)
  15. if err != nil {
  16. return 0, err
  17. }
  18. return t.Unix(), nil
  19. }
  20. // Date parse timestamp to time string
  21. // php date
  22. func Date(timestamp int64, layout string) string {
  23. return time.Unix(timestamp, 0).Format(layout)
  24. }
  25. // CheckDate check date is right
  26. // php checkdate
  27. func CheckDate(month, day, year uint) bool {
  28. layout := "2006-01-02"
  29. timeStr := fmt.Sprintf("%d-%02d-%02d", year, month, day)
  30. _, err := time.Parse(layout, timeStr)
  31. if err != nil {
  32. return false
  33. }
  34. return true
  35. }
  36. // Sleep pauses the current goroutine for seconds
  37. // php sleep
  38. func Sleep(seconds int64) {
  39. time.Sleep(time.Duration(seconds) * time.Second)
  40. }
  41. // Usleep pauses the current goroutine for microseconds
  42. // php usleep
  43. func Usleep(microseconds int64) {
  44. time.Sleep(time.Duration(microseconds) * time.Microsecond)
  45. }