stdc.bmx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. Rem
  2. Standard C library (And friends!) functions.
  3. To simplify life, this is a 'structless' interface meaning some functions have been wrapped.
  4. End Rem
  5. SuperStrict
  6. Rem
  7. bbdoc: Standard C Library functions.
  8. End Rem
  9. Module Pub.StdC
  10. ModuleInfo "Version: 1.14"
  11. ModuleInfo "Author: Various"
  12. ModuleInfo "License: zlib/libpng"
  13. ModuleInfo "Modserver: BRL"
  14. ModuleInfo "Credit: Adapted for BlitzMax by Mark Sibly"
  15. ModuleInfo "History: 1.14"
  16. ModuleInfo "History: Added ftruncate_"
  17. ModuleInfo "History: 1.13 Release"
  18. ModuleInfo "History: Added totally awesome stat_() hack for '<' and '>' in Win32 paths"
  19. ModuleInfo "History: 1.12 Release"
  20. ModuleInfo "History: remove_ now does a chmod 0x1b6 beforehand on Win32 - ie: will remove write protected files"
  21. ModuleInfo "History: chmod_ now does something on Win32"
  22. ModuleInfo "History: 1.11 Release"
  23. ModuleInfo "History: Fixed getsockopt"
  24. ModuleInfo "History: 1.10 Release"
  25. ModuleInfo "History: Fixed network byte ordering for sento_ and recvfrom_"
  26. ModuleInfo "History: 1.09 Release"
  27. ModuleInfo "History: More socket stuff added"
  28. ModuleInfo "History: 1.07 Release"
  29. ModuleInfo "History: More socket stuff added"
  30. ModuleInfo "History: 1.06 Release"
  31. ModuleInfo "History: Cleaned up Win32 system_"
  32. ModuleInfo "History: 1.05 Release"
  33. ModuleInfo "History: 1.04 Release"
  34. ModuleInfo "History: Fixed C Compiler warnings"
  35. ?linux
  36. ModuleInfo "CC_OPTS: -D_FILE_OFFSET_BITS=64"
  37. ?win32
  38. Import "-lws2_32"
  39. Import "inet_pton.c"
  40. ?haiku
  41. Import "-lnetwork"
  42. ?
  43. Import "stdc.c"
  44. Type TAddrInfo
  45. Field infoPtr:Byte Ptr
  46. Field shouldFreeInfo:Int '
  47. Method New()
  48. infoPtr = bmx_stdc_addrinfo_new()
  49. End Method
  50. Method New(family:Int, sockType:Int, flags:Int = 0)
  51. infoPtr = bmx_stdc_addrinfo_new()
  52. setFamily(family)
  53. setSockType(sockType)
  54. If flags Then
  55. setFlags(flags)
  56. End If
  57. End Method
  58. Method New(infoPtr:Byte Ptr, shouldFreeInfo:Int)
  59. Self.infoPtr = infoPtr
  60. Self.shouldFreeInfo = shouldFreeInfo
  61. End Method
  62. Function _Create:TAddrInfo(infoPtr:Byte Ptr, shouldFreeInfo:Int) { nomangle }
  63. Return New TAddrInfo(infoPtr, shouldFreeInfo)
  64. End Function
  65. Function _CreateArray:TAddrInfo[](length:Int) { nomangle }
  66. Return New TAddrInfo[length]
  67. End Function
  68. Function _SetAtIndex(arr:TAddrInfo[], info:TAddrInfo, index:Int) { nomangle }
  69. arr[index] = info
  70. End Function
  71. Method flags:Int()
  72. Return bmx_stdc_addrinfo_flags(infoPtr)
  73. End Method
  74. Method setFlags(flags:Int)
  75. bmx_stdc_addrinfo_setflags(infoPtr, flags)
  76. End Method
  77. Method family:Int()
  78. Return bmx_stdc_addrinfo_family(infoPtr)
  79. End Method
  80. Method setFamily(family:Int)
  81. bmx_stdc_addrinfo_setfamily(infoPtr, family)
  82. End Method
  83. Method sockType:Int()
  84. Return bmx_stdc_addrinfo_socktype(infoPtr)
  85. End Method
  86. Method setSockType(sockType:Int)
  87. bmx_stdc_addrinfo_setsocktype(infoPtr, sockType)
  88. End Method
  89. Method protocol:Int()
  90. Return bmx_stdc_addrinfo_protocol(infoPtr)
  91. End Method
  92. Method setProtocol(protocol:Int)
  93. bmx_stdc_addrinfo_setprotocol(infoPtr, protocol)
  94. End Method
  95. Method addrLen:Int()
  96. Return bmx_stdc_addrinfo_addrlen(infoPtr)
  97. End Method
  98. Method addr:Byte Ptr()
  99. Return bmx_stdc_addrinfo_addr(infoPtr)
  100. End Method
  101. Method canonName:String()
  102. Return bmx_stdc_addrinfo_canonname(infoPtr)
  103. End Method
  104. Method HostName:String()
  105. Return bmx_stdc_addrinfo_hostname(infoPtr, 0)
  106. End Method
  107. Method HostIp:String()
  108. Return bmx_stdc_addrinfo_hostname(infoPtr, NI_NUMERICHOST)
  109. End Method
  110. Method Delete()
  111. If shouldFreeInfo Then
  112. freeaddrinfo_(infoPtr)
  113. End If
  114. End Method
  115. End Type
  116. Type TSockaddrStorage
  117. Field storagePtr:Byte Ptr
  118. Method New()
  119. storagePtr = bmx_stdc_sockaddrestorage_new()
  120. End Method
  121. Method family:Int()
  122. End Method
  123. Method address:String()
  124. Return bmx_stdc_sockaddrestorage_address(storagePtr)
  125. End Method
  126. Method Delete()
  127. If storagePtr Then
  128. free_(storagePtr)
  129. storagePtr = Null
  130. End If
  131. End Method
  132. End Type
  133. 'c lib
  134. Extern "c"
  135. Const SEEK_SET_:Int=0
  136. Const SEEK_CUR_:Int=1
  137. Const SEEK_END_:Int=2
  138. Const S_IFMT_:Int=$f000
  139. Const S_IFIFO_:Int=$1000
  140. Const S_IFCHR_:Int=$2000
  141. Const S_IFBLK_:Int=$3000
  142. Const S_IFDIR_:Int=$4000
  143. Const S_IFREG_:Int=$8000
  144. Global stdin_:Byte Ptr
  145. Global stdout_:Byte Ptr
  146. Global stderr_:Byte Ptr
  147. Function getchar_:Int()
  148. Function puts_:Int( str:String )
  149. Function putenv_:Int( str:String )
  150. Function getenv_:String( env:String )
  151. 'file system
  152. Function fopen_:Byte Ptr( file:String,Mode:String )
  153. Function fclose_:Int( c_stream:Byte Ptr )="int fclose( FILE* ) !"
  154. Function fread_:Long( buf:Byte Ptr,size:Long,count:Long,c_stream:Byte Ptr )="size_t fread( void* , size_t, size_t, FILE* ) !"
  155. Function fwrite_:Long( buf:Byte Ptr,size:Long,count:Long,c_stream:Byte Ptr )="size_t fwrite( void* ,size_t, size_t, FILE* ) !"
  156. Function fflush_( c_stream:Byte Ptr )="int fflush( FILE* ) !"
  157. Function fseek_:Int( c_stream:Byte Ptr,offset:Long,origin:Int )'="fseek"
  158. Function ftell_:Long( c_stream:Byte Ptr )'="ftell"
  159. Function feof_:Int( c_stream:Byte Ptr )="int feof( FILE* ) !"
  160. Function fgetc_:Int( c_stream:Byte Ptr )="int fgetc( FILE* ) !"
  161. Function ungetc_:Int( char:Int,c_stream:Byte Ptr )="int ungetc( int,FILE* ) !"
  162. Function fputs_:Int( str:String,c_stream:Byte Ptr )
  163. Function ftruncate_:Int(c_stream:Byte Ptr, size:Long)
  164. 'posix
  165. Function chdir_:Int( dir:String )
  166. Function getcwd_:String()
  167. Function chmod_:Int( path:String,Mode:Int )
  168. Function mkdir_:Int( path:String,Mode:Int )
  169. Function rmdir_:Int( path:String )
  170. Function rename_:Int( from_path:String,to_path:String )
  171. Function remove_( path:String )
  172. Function opendir_:Byte Ptr( path:String )
  173. Function closedir_:Int( dir:Byte Ptr )
  174. Function readdir_:String( dir:Byte Ptr )
  175. Function stat_:Int( path:String,st_mode:Int Var,st_size:Long Var,st_mtime:Int Var,st_ctime:Int Var,st_atime:Int Var )
  176. Function stat64_:Int( path:String,st_mode:Int Var,st_size:Long Var,st_mtime:Long Var,st_ctime:Long Var,st_atime:Long Var )
  177. Function system_:Int( cmd:String )
  178. Function utime_:Int( path:String, ty:Int, time:Long)
  179. 'misc
  180. Function abort_()="void abort() !"
  181. Function malloc_:Byte Ptr( size:Size_T )="void * malloc( size_t ) !"
  182. Function realloc_:Byte Ptr( p:Byte Ptr,size:Size_T )="void * realloc( void * , size_t ) !"
  183. Function free_( buf:Byte Ptr )="void free( void * ) !"
  184. Function exit_( exit_code:Int )="void exit( int ) !"
  185. Function atexit_:Int( fun() )="int atexit(void (*)() ) !"
  186. Function memset_:Byte Ptr( buf:Byte Ptr,val:Int,size:Size_T )="void * memset( void * , int ,size_t ) !"
  187. Function memcmp_:Int( lhs:Byte Ptr,rhs:Byte Ptr,size:Size_T )="int memcmp( void * , void * , size_t ) !"
  188. Function memcpy_:Byte Ptr( dst:Byte Ptr,src:Byte Ptr,size:Size_T )="void * memcpy( void * , void * , size_t ) !"
  189. Function memmove_:Byte Ptr( dst:Byte Ptr,src:Byte Ptr,size:Size_T )="void * memmove( void * , void * , size_t ) !"
  190. Function strlen_:Size_T( str:Byte Ptr )="size_t strlen( const char *) !"
  191. 'math
  192. Function sin_!( n! )="sin"
  193. Function cos_!( n! )="cos"
  194. Function tan_!( n! )="tan"
  195. Function sinh_!( n! )="sinh"
  196. Function cosh_!( n! )="cosh"
  197. Function tanh_!( n! )="tanh"
  198. Function asin_!( n! )="asin"
  199. Function acos_!( n! )="acos"
  200. Function atan_!( n! )="atan"
  201. 'sockets
  202. Const AF_INET_:Int=2 'address types
  203. Const SOCK_STREAM_:Int=1,SOCK_DGRAM_:Int=2 'communication types
  204. Const SOCKET_ERROR_:Int=-1
  205. Const AF_INET6_:Int=10 ' IPv6
  206. Const AF_UNSPEC_:Int = 0
  207. Const SO_DEBUG:Int=1 'turn on debugging info recording
  208. Const SO_ACCEPTCONN:Int=2 'socket has had listen()
  209. Const SO_REUSEADDR:Int=4 'allow local address reuse
  210. Const SO_KEEPALIVE:Int=8 'keep connections alive
  211. Const SO_DONTROUTE:Int=$10 'just use interface addresses
  212. Const SO_BROADCAST:Int=$20 'permit sending of broadcast msgs
  213. Const SO_USELOOPBACK:Int=$40 'bypass hardware when possible
  214. Const SO_LINGER:Int=$80 'linger on close if data present
  215. Const SO_OOBINLINE:Int=$100 'leave received OOB data in line
  216. Const AI_PASSIVE:Int = $001 ' Socket address is intended for 'bind'
  217. Const AI_CANONNAME:Int = $002 ' Request for canonical name
  218. Const AI_NUMERICHOST:Int = $004 ' Don't use name resolution
  219. Const AI_V4MAPPED:Int = $008 ' IPv4 mapped addresses are acceptable
  220. Const AI_ALL:Int = $010 ' Return IPv4 mapped and IPv6 addresses
  221. Const AI_ADDRCONFIG:Int = $020 ' Use configuration of this host to choose returned address type
  222. Const AI_NUMERICSERV:Int = $400 ' Don't use name resolution
  223. 'Additional options.
  224. Const SO_SNDBUF:Int=$1001 'sendbuffersize
  225. Const SO_RCVBUF:Int=$1002 'receivebuffersize
  226. Const SO_SNDLOWAT:Int=$1003 'sendlow-watermark
  227. Const SO_RCVLOWAT:Int=$1004 'receivelow-watermark
  228. Const SO_SNDTIMEO:Int=$1005 'sendtimeout
  229. Const SO_RCVTIMEO:Int=$1006 'receivetimeout
  230. Const SO_ERROR:Int=$1007 'geterrorstatusandclear
  231. Const SO_TYPE:Int=$1008 'getsockettype
  232. 'Option for opening sockets for synchronous access.
  233. Const SO_SYNCHRONOUS_ALERT:Int=$10
  234. Const SO_SYNCHRONOUS_NONALERT:Int=$20
  235. ?Win32
  236. Const SO_OPENTYPE:Int=$7008
  237. Const SO_MAXDG:Int=$7009
  238. Const SO_MAXPATHDG:Int=$700A
  239. Const SO_UPDATE_ACCEPT_CONTEXT:Int=$700B
  240. Const SO_CONNECT_TIME:Int=$700C
  241. ?
  242. Const TCP_NODELAY:Int=$0001
  243. Const TCP_BSDURGENT:Int=$7000
  244. Const IPPROTO_UDP:Int=17
  245. Const IPPROTO_TCP:Int=6
  246. Const NI_DGRAM:Int = $0001
  247. Const NI_NAMEREQD:Int = $0002
  248. Const NI_NOFQDN:Int = $0004
  249. Const NI_NUMERICHOST:Int = $0008
  250. Const NI_NUMERICSERV:Int = $0010
  251. Const SOL_SOCKET:Int = $ffff ' options for socket level
  252. 'how params for shutdown_
  253. Const SD_SEND:Int=1
  254. Const SD_RECEIVE:Int=0
  255. Const SD_BOTH:Int=2
  256. Function htons_:Int( n:Int )
  257. Function ntohs_:Int( n:Int )
  258. Function htonl_:Int( n:Int )
  259. Function ntohl_:Int( n:Int )
  260. ?win32 and ptr64
  261. Function socket_:Long( addr_type:Int,comm_type:Int,protocol:Int=0 )
  262. Function closesocket_( socket:Long )
  263. Function bind_:Int( socket:Long,addr_type:Int,port:Int )
  264. ?win32 and ptr32
  265. Function socket_:Int( addr_type:Int,comm_type:Int,protocol:Int=0 )
  266. Function closesocket_( socket:Int )
  267. Function bind_:Int( socket:Int,addr_type:Int,port:Int )
  268. ?not win32
  269. Function socket_:Int( addr_type:Int,comm_type:Int,protocol:Int=0 )
  270. Function closesocket_( socket:Int )
  271. Function bind_:Int( socket:Int,addr_type:Int,port:Int )
  272. ?
  273. Function gethostbyaddr_:Byte Ptr( addr:Byte Ptr,addr_len:Int,addr_type:Int )
  274. 'Function gethostbyname_:Byte Ptr Ptr( name:String,addr_type:Int Var,addr_len:Int Var )
  275. Function getaddrinfo_:TAddrInfo[](name:String, service:String = "http", family:Int = AF_UNSPEC_)
  276. Function getaddrinfo_hints:TAddrInfo[](name:String, service:String, hints:Byte Ptr)
  277. ?not win32
  278. Function connect_:Int( socket:Int, addrinfo:Byte Ptr )
  279. Function listen_:Int( socket:Int,backlog:Int )
  280. Function accept_:Int( socket:Int,addr:Byte Ptr,addr_len:Byte Ptr)
  281. Function select_:Int( n_read:Int,read_socks:Int Ptr,n_write:Int,write_socks:Int Ptr,n_except:Int,except_socks:Int Ptr,millis:Int )
  282. Function sendto_:Int( socket:Int,buf:Byte Ptr,size:Int,flags:Int,dest_ip:Byte Ptr,dest_port:Int, addr_type:Int = AF_INET_ )
  283. Function recvfrom_:Int( socket:Int,buf:Byte Ptr,size:Int,flags:Int,sender_ip:Int Var,sender_port:Int Var)
  284. Function setsockopt_:Int( socket:Int,level:Int,optname:Int,optval:Byte Ptr,count:Int)
  285. Function getsockopt_:Int( socket:Int,level:Int,optname:Int,optval:Byte Ptr,count:Int Var)
  286. Function shutdown_:Int( socket:Int,how:Int )
  287. Function getsockname_:Int( socket:Int,addr:Byte Ptr,addr_len:Int Var )
  288. Function getpeername_:Int( socket:Int,addr:Byte Ptr,addr_len:Int Var )
  289. ?not win32 and ptr64
  290. Function send_:Long( socket:Int,buf:Byte Ptr,size:Size_T,flags:Int )
  291. Function recv_:Long( socket:Int,buf:Byte Ptr,size:Size_T,flags:Int )
  292. ?not win32 and Not ptr64
  293. Function send_:Int( socket:Int,buf:Byte Ptr,size:Size_T,flags:Int )
  294. Function recv_:Int( socket:Int,buf:Byte Ptr,size:Size_T,flags:Int )
  295. ?
  296. ?win32 and ptr32
  297. Function connect_:Int( socket:Int, addrinfo:Byte Ptr )
  298. Function listen_:Int( socket:Int,backlog:Int )
  299. Function accept_:Int( socket:Int,addr:Byte Ptr,addr_len:Byte Ptr)
  300. Function select_:Int( n_read:Int,read_socks:Int Ptr,n_write:Int,write_socks:Int Ptr,n_except:Int,except_socks:Int Ptr,millis:Int )
  301. Function sendto_:Int( socket:Int,buf:Byte Ptr,size:Int,flags:Int,dest_ip:Byte Ptr,dest_port:Int, addr_type:Int = AF_INET_ )
  302. Function recvfrom_:Int( socket:Int,buf:Byte Ptr,size:Int,flags:Int,sender_ip:Int Var,sender_port:Int Var)
  303. Function setsockopt_:Int( socket:Int,level:Int,optname:Int,optval:Byte Ptr,count:Int)
  304. Function getsockopt_:Int( socket:Int,level:Int,optname:Int,optval:Byte Ptr,count:Int Var)
  305. Function shutdown_:Int( socket:Int,how:Int )
  306. Function getsockname_:Int( socket:Int,addr:Byte Ptr,addr_len:Int Var )
  307. Function getpeername_:Int( socket:Int,addr:Byte Ptr,addr_len:Int Var )
  308. Function send_:Int( socket:Int,buf:Byte Ptr,size:Size_T,flags:Int )
  309. Function recv_:Int( socket:Int,buf:Byte Ptr,size:Size_T,flags:Int )
  310. ?
  311. ?win32 and ptr64
  312. Function connect_:Int( socket:Long, addrinfo:Byte Ptr )
  313. Function listen_:Int( socket:Long,backlog:Int )
  314. Function accept_:Int( socket:Long,addr:Byte Ptr,addr_len:Byte Ptr)
  315. Function select_:Int( n_read:Int,read_socks:Long Ptr,n_write:Int,write_socks:Long Ptr,n_except:Int,except_socks:Long Ptr,millis:Int )
  316. Function sendto_:Int( socket:Long,buf:Byte Ptr,size:Int,flags:Int,dest_ip:Byte Ptr,dest_port:Int, addr_type:Int = AF_INET_ )
  317. Function recvfrom_:Int( socket:Long,buf:Byte Ptr,size:Int,flags:Int,sender_ip:Int Var,sender_port:Int Var)
  318. Function setsockopt_:Int( socket:Long,level:Int,optname:Int,optval:Byte Ptr,count:Int)
  319. Function getsockopt_:Int( socket:Long,level:Int,optname:Int,optval:Byte Ptr,count:Int Var)
  320. Function shutdown_:Int( socket:Long,how:Int )
  321. Function getsockname_:Int( socket:Long,addr:Byte Ptr,addr_len:Int Var )
  322. Function getpeername_:Int( socket:Long,addr:Byte Ptr,addr_len:Int Var )
  323. Function send_:Long( socket:Long,buf:Byte Ptr,size:Size_T,flags:Int )
  324. Function recv_:Long( socket:Long,buf:Byte Ptr,size:Size_T,flags:Int )
  325. ?
  326. Function freeaddrinfo_(res:Byte Ptr)
  327. Function bmx_stdc_addrinfo_flags:Int(info:Byte Ptr)
  328. Function bmx_stdc_addrinfo_family:Int(info:Byte Ptr)
  329. Function bmx_stdc_addrinfo_socktype:Int(info:Byte Ptr)
  330. Function bmx_stdc_addrinfo_protocol:Int(info:Byte Ptr)
  331. Function bmx_stdc_addrinfo_addrlen:Int(info:Byte Ptr)
  332. Function bmx_stdc_addrinfo_addr:Byte Ptr(info:Byte Ptr)
  333. Function bmx_stdc_addrinfo_hostname:String(info:Byte Ptr, flags:Int)
  334. Function bmx_stdc_addrinfo_canonname:String(info:Byte Ptr)
  335. Function inet_pton_:Int(family:Int, src:String, dst:Byte Ptr)
  336. Function bmx_stdc_addrinfo_new:Byte Ptr()
  337. Function bmx_stdc_addrinfo_setflags(info:Byte Ptr, flags:Int)
  338. Function bmx_stdc_addrinfo_setfamily(info:Byte Ptr, family:Int)
  339. Function bmx_stdc_addrinfo_setsocktype(info:Byte Ptr, sockType:Int)
  340. Function bmx_stdc_addrinfo_setprotocol(info:Byte Ptr, protocol:Int)
  341. Function bmx_stdc_sockaddrestorage_new:Byte Ptr()
  342. Function bmx_stdc_sockaddrestorage_address:String(handle:Byte Ptr)
  343. ?not win32
  344. Function bmx_stdc_bind_info:Int(socket:Int, info:Byte Ptr)
  345. Function bmx_stdc_accept_:Int(socket:Int, storage:Byte Ptr)
  346. Function bmx_stdc_getsockname:Int(socket:Int, port:Int Var, address:String Var)
  347. Function bmx_stdc_getpeername:Int(socket:Int, port:Int Var, address:String Var)
  348. ?win32 and ptr32
  349. Function bmx_stdc_bind_info:Int(socket:Int, info:Byte Ptr)
  350. Function bmx_stdc_accept_:Int(socket:Int, storage:Byte Ptr)
  351. Function bmx_stdc_getsockname:Int(socket:Int, port:Int Var, address:String Var)
  352. Function bmx_stdc_getpeername:Int(socket:Int, port:Int Var, address:String Var)
  353. ?win32 and ptr64
  354. Function bmx_stdc_bind_info:Int(socket:Long, info:Byte Ptr)
  355. Function bmx_stdc_accept_:Int(socket:Long, storage:Byte Ptr)
  356. Function bmx_stdc_getsockname:Int(socket:Long, port:Int Var, address:String Var)
  357. Function bmx_stdc_getpeername:Int(socket:Long, port:Int Var, address:String Var)
  358. ?
  359. 'time
  360. Function time_:Int( time:Byte Ptr )
  361. Function localtime_:Byte Ptr( time:Byte Ptr ) ' note : not thread safe
  362. Function strftime_:Int( buf:Byte Ptr,size:Int,fmt:String,time:Byte Ptr )
  363. ?Not macos
  364. Function clock_gettime_(id:Int, spec:STimeSpec Var)
  365. ?macos
  366. Function mach_absolute_time_ns:ULong()
  367. ?
  368. Function errno_:Int()
  369. Rem
  370. bbdoc: Returns the current date and time.
  371. End Rem
  372. Function CurrentDateTime(dt:SDateTime Var, utc:Int = True)="bmx_current_datetime"
  373. End Extern
  374. Struct STimeSpec
  375. Field tv_sec:Size_T
  376. Field tv_nsec:Size_T
  377. Method New(tv_sec:Size_T, tv_nsec:Size_T)
  378. Self.tv_sec = tv_sec
  379. Self.tv_nsec = tv_nsec
  380. End Method
  381. End Struct
  382. Rem
  383. bbdoc: A basic DateTime struct.
  384. End Rem
  385. Struct SDateTime
  386. Rem
  387. bbdoc: The year.
  388. End Rem
  389. Field year:Int
  390. Rem
  391. bbdoc: The month, 1-12.
  392. End Rem
  393. Field month:Int
  394. Rem
  395. bbdoc: The day of the month, 1-31.
  396. End Rem
  397. Field day:Int
  398. Rem
  399. bbdoc: The hour, 0-23.
  400. End Rem
  401. Field hour:Int
  402. Rem
  403. bbdoc: The minute, 0-59.
  404. End Rem
  405. Field minute:Int
  406. Rem
  407. bbdoc: The second, 0-59.
  408. End Rem
  409. Field second:Int
  410. Rem
  411. bbdoc: The millisecond, 0-999.
  412. End Rem
  413. Field millisecond:Int
  414. Rem
  415. bbdoc: #True if the date time is in UTC, #False if it is in local time.
  416. End Rem
  417. Field utc:Int
  418. Rem
  419. bbdoc: The offset from UTC, in minutes.
  420. End Rem
  421. Field offset:Int
  422. Rem
  423. bbdoc: 1 if the date time is observing daylight savings time, 0 if not and -1 if it is not known.
  424. about: Daylight Saving Time (DST) is the practice of setting the clock ahead by one hour from standard time
  425. during the warmer months, and then back again in the fall, in order to extend evening daylight and reduce the
  426. need for artificial lighting. This can affect local time calculations, and so it's important to track whether a
  427. given datetime object is observing DST. Note that not all regions observe DST, and the start and end dates
  428. for DST can vary from one region to another.
  429. End Rem
  430. Field dst:Int = -1
  431. Rem
  432. bbdoc: Creates a new #SDateTime instance from the given date and time information.
  433. End Rem
  434. Method New(year:Int, month:Int, day:Int, hour:Int, minute:Int, second:Int, millisecond:Int = 0, utc:Int = True, offset:Int = 0, dst:Int = -1)
  435. Self.year = year
  436. Self.month = month
  437. Self.day = day
  438. Self.hour = hour
  439. Self.minute = minute
  440. Self.second = second
  441. Self.millisecond = millisecond
  442. Self.utc = utc
  443. Self.offset = offset
  444. Self.dst = dst
  445. End Method
  446. Rem
  447. bbdoc: Returns a string representation of the date time in ISO 8601 format.
  448. about: Without millisecond precision.
  449. End Rem
  450. Method ToString:String()
  451. Return ToIso8601()
  452. End Method
  453. Rem
  454. bbdoc: Returns a string representation in ISO 8601 format.
  455. about: ISO 8601 is an international standard covering the exchange of date- and time-related data.
  456. It was issued by the International Organization for Standardization (ISO) and provides a well-defined method of representing dates and
  457. times in a way that avoids ambiguity.
  458. An example of a date in ISO 8601 format is "2023-06-24T18:30:00Z", representing 6:30 pm on June 24, 2023, in Coordinated Universal Time (UTC).
  459. If @showMillis is set to #True, the output string will include millisecond precision. For instance, "2023-06-24T18:30:00.123Z"
  460. where "123" represents milliseconds.
  461. End Rem
  462. Method ToIso8601:String(showMillis:Int = False)
  463. Return bmx_datetime_iso8601(Self, showMillis)
  464. End Method
  465. Rem
  466. bbdoc: Returns an instance of #SDateTime representing the date and time corresponding to the given epoch timestamp.
  467. about: The 'epoch' refers to the Unix epoch, which is a system for describing a point in time, defined as the number of seconds
  468. that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, minus the number of leap seconds.
  469. If @fracNanoSecs is provided, it will be used to set the 'millisecond' field of the SDateTime instance.
  470. The nanosecond fraction is effectively divided by a million to provide millisecond precision.
  471. If @isLocal is set to #True, the epoch timestamp is assumed to be in local time, otherwise it is assumed to be in UTC.
  472. End Rem
  473. Function FromEpoch:SDateTime(epochSecs:Long, fracNanoSecs:Long = 0, isLocal:Int = False)
  474. Return bmx_datetime_from_epoch(epochSecs, fracNanoSecs, isLocal:Int)
  475. End Function
  476. Rem
  477. bbdoc: Converts the current date and time to the number of seconds that have elapsed since the Unix Epoch.
  478. returns: The number of seconds that have elapsed since the Unix Epoch, or -1 if the conversion failed.
  479. about: The 'epoch' refers to the Unix epoch, which is a system for describing a point in time, defined as the number of seconds
  480. that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, minus the number of leap seconds.
  481. End Rem
  482. Method ToEpochSecs:Long()
  483. Return bmx_datetime_to_epoch(Self)
  484. End Method
  485. Rem
  486. bbdoc: Converts the current date time to the equivalent in UTC.
  487. returns: The equivalent date time in UTC, or the original date time if the conversion failed.
  488. End Rem
  489. Method ToUtc:SDateTime()
  490. If utc Then
  491. Return Self
  492. End If
  493. Local dt:SDateTime
  494. Local res:Int = bmx_datetime_convert_to_utc(Self, dt)
  495. If res = 0 Then
  496. Return dt
  497. Else
  498. Return Self
  499. End If
  500. End Method
  501. End Struct
  502. Rem
  503. bbdoc: Gets the current date string.
  504. returns: The current date as a string
  505. about:
  506. By default, it returns the current date in the format: DD MON YYYY (i.e. 10 DEC 2000).
  507. You can also specify some parameters to return the date in a format of your choice:
  508. [ @parameter | @description
  509. * %%a | Abbreviated day name (sun - mon).
  510. * %%A | Long day name (Sunday - Monday).
  511. * %%b | Abbreviated month name (jan - feb).
  512. * %%B | Long month name (January...).
  513. * %%c | Locale date & time.
  514. * %%d | day - in number (1..31).
  515. * %%H | hour - in number (0..23).
  516. * %%I | hour - in number (1..12).
  517. * %%j | day of the year (1..366).
  518. * %%m | month - in number (1..12).
  519. * %%M | minutes - in number (00..59).
  520. * %%P | AM / PM.
  521. * %%S | seconds - in number (00..59).
  522. * %%U | week number
  523. * %%w | day of the week (0..6).
  524. * %%W | week of the year (0..53).
  525. * %%x | locale data representation.
  526. * %%y | year without century (2014 --> 14).
  527. * %%Y | Year (2014).
  528. * %%Z | Time zone name.
  529. ]
  530. You can use these parameters together:<br/>
  531. CurrentDate("Month: %%a Day: %%d")<br/>
  532. End Rem
  533. Function CurrentDate:String(_format:String="%d %b %Y")
  534. Return bmx_current_datetime_format(_format)
  535. End Function
  536. Rem
  537. bbdoc: Gets the current time string.
  538. returns: The current time as a string
  539. about:
  540. Returns the current time in the format: HH:MM:SS (i.e. 14:31:57).
  541. End Rem
  542. Function CurrentTime:String()
  543. Return bmx_current_datetime_format("%H:%M:%S")
  544. End Function
  545. Private
  546. Extern "c"
  547. Function bmx_datetime_iso8601:String(dt:SDateTime Var, showMillis:Int = False)
  548. Function Startup()="bb_stdc_Startup"
  549. Function bmx_datetime_from_epoch:SDateTime(epochSecs:Long, fracNanoSecs:Long, isLocal:Int)
  550. Function bmx_current_datetime_format:String(format:String)
  551. Function bmx_datetime_to_epoch:Long(dt:SDateTime Var)
  552. Function bmx_datetime_convert_to_utc:Int(dt:SDateTime Var, dtUtc:SDateTime Var)
  553. End Extern
  554. Startup
  555. Public