time.monkey2 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. Namespace std.time
  2. Private
  3. Global _us0:Long
  4. Public
  5. #rem monkeydoc @hidden Time class.
  6. #end
  7. Class Time
  8. #rem monkeydoc Seconds (0-61)
  9. May include 'leap' seconds.
  10. #end
  11. Property Seconds:Int()
  12. Return _tm.tm_sec
  13. End
  14. #rem monkeydoc Minutes (0-59)
  15. #end
  16. Property Minutes:Int()
  17. Return _tm.tm_min
  18. End
  19. #rem monkeydoc Hours since midnight (0-23)
  20. #end
  21. Property Hours:Int()
  22. Return _tm.tm_hour
  23. End
  24. #rem monkeydoc Day of the month (1-31)
  25. #end
  26. Property Day:Int()
  27. Return _tm.tm_mday
  28. End
  29. #rem monkeydoc Week day since Sunday (0-6)
  30. #end
  31. Property WeekDay:Int()
  32. Return _tm.tm_wday
  33. End
  34. #rem monkeydoc Days since January 1 (0-365)
  35. #end
  36. Property YearDay:Int()
  37. Return _tm.tm_yday
  38. End
  39. #rem monkeydoc Month since January (0-11)
  40. #end
  41. Property Month:Int()
  42. Return _tm.tm_mon
  43. End
  44. #rem monkeydoc Year
  45. #end
  46. Property Year:Int()
  47. Return _tm.tm_year+1900
  48. End
  49. #rem monkeydoc True if daylight savings is in effect.
  50. #end
  51. Property DaylightSavings:Bool()
  52. Return _tm.tm_isdst
  53. End
  54. #rem monkeydoc Converts time to a string.
  55. The string format is: WeekDay Day Month Year Hours:Minutes:Seconds
  56. #end
  57. Method ToString:String()
  58. Return _days[ WeekDay ]+" "+Day+" "+_months[ Month ]+" "+Year+" "+ Hours+":"+Minutes+":"+Seconds
  59. End
  60. #rem monkeydoc Overloaded compare operator.
  61. Time x is 'less than' time y if time x represents a time 'earlier' than time y.
  62. #end
  63. Operator<=>:Int( time:Time )
  64. Return libc.difftime( _timer,time._timer )<=>0
  65. End
  66. #rem monkeydoc Gets current time.
  67. #end
  68. Function Now:Time()
  69. Local timer:=libc.time( Null )
  70. Local tm:=libc.localtime( Varptr timer )
  71. Return New Time( timer,tm )
  72. End
  73. Private
  74. Const _days:=New String[]( "Sun","Mon","Tue","Wed","Thu","Fri","Sat" )
  75. Const _months:=New String[]( "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" )
  76. Field _timer:libc.time_t
  77. Field _tm:libc.tm_t
  78. Method New( timer:libc.time_t,tm:libc.tm_t Ptr )
  79. _timer=timer
  80. _tm=tm[0]
  81. End
  82. End
  83. #rem monkeydoc Gets the number of microseconds since the app started.
  84. #end
  85. Function Microsecs:Long()
  86. Local tv:timeval
  87. gettimeofday( Varptr tv )
  88. Local us:=tv.tv_sec*1000000+tv.tv_usec
  89. If _us0 Return us-_us0
  90. _us0=us
  91. Return 0
  92. End
  93. #rem monkeydoc Gets the number of milliseconds since the app started.
  94. #end
  95. Function Millisecs:Int()
  96. Return Microsecs()/1000
  97. End
  98. #rem monkeydoc @hidden Gets the number of seconds since the app started.
  99. #end
  100. Function Seconds:Double()
  101. Return Microsecs()/1000000.0
  102. End