time.monkey2 3.0 KB

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