Get system date & time, not as a string
BlitzMax Forums/BlitzMax Programming/Get system date & time, not as a string
| ||
How can I read the system clock without the asinine overhead of messing around with strings? |
| ||
"CurrentTime" is just a wrapper around the C Standard Library's "time" function, which then converts the C time to a string. So, you can call "time" directly if you import Pub.StdC (as "_time"), and do whatever you want with the populated time_t buffer. Remember you can view the source of "CurrentTime" in BRL.System. It's pretty straightforward. Last edited 2012 |
| ||
I did look at that, and was completely lost when I actually tried to use the data. How do I interpret the array of bytes? |
| ||
Well technically it's implementation-dependent, which is always a pain - BlitzMax solves this by handing off the interpretation of the value to the C Standard Library as well, so no BlitzMax code ever actually handles the value. In practice, on *nix systems it will be an integer containing the number of seconds since the Unix Epoch, and on Windows... I have no idea, I assume MinGW returns the same value as on Linux, and that MSVC returns something different because MSVC is like that. I think it's 32-bit by default on MinGW, but that may have changed. The best option is to find a suitable C function to interpret the data in a way useful to you, and not attempt to interpret this data yourself because it is free to change shape and size at any time. If you really have to handle it in BlitzMax... find out whether it's a 32-bit or 64-bit int, but be aware that your program can break without warning if the C runtime library updates. EDIT: I think the short answer is to look at the documentation for strftime, and construct a format-string that produces an easier output for you to parse (e.g. just seconds, no colons) and wrap this in a function similar to the way CurrentTime is set up. You still have string handling overhead, but you can make the value much easier to read, so you can e.g. just get three separate plain values for hour, minute and seconds, pass the strings straight to Int(), and do some multiplications by 60. Last edited 2012 |
| ||
on the off chance that this topic comes up in some future programmer's search, here's the code I've ended up using:Type timestamp Field hour%,minute%,second% Field day%,month%,year% Method getdate$() Return padbefore(day,"0",2)+"/"+padbefore(month,"0",2)+"/"+Right(year,2) End Method Method gettime$() Local suff$=" am" If hour>11 Then suff=" pm" Return padbefore((hour Mod 12),"0",2)+":"+padbefore(minute,"0",2)+":"+padbefore(second,"0",2)+suff End Method Method write(f:TStream) WriteInt f,hour WriteInt f,minute WriteInt f,second WriteInt f,day WriteInt f,month WriteInt f,year End Method Function read:timestamp(f:TStream) Local n:timestamp=New timestamp n.hour=ReadInt(f) n.minute=ReadInt(f) n.second=ReadInt(f) n.day=ReadInt(f) n.month=ReadInt(f) n.year=ReadInt(f) Return n End Function Function now:timestamp() Local n:timestamp=New timestamp Local time[256],buff:Byte[256] time_(time) Local localtime:Byte Ptr=localtime_( time ) strftime_(buff,256,"%d",localtime) n.day=Int(String.FromCString(buff)) strftime_(buff,256,"%m",localtime) n.month=Int(String.FromCString(buff)) strftime_(buff,256,"%Y",localtime) n.year=Int(String.FromCString(buff)) strftime_(buff,256,"%H",localtime) n.hour=Int(String.FromCString(buff)) strftime_(buff,256,"%M",localtime) n.minute=Int(String.FromCString(buff)) strftime_(buff,256,"%S",localtime) n.second=Int(String.FromCString(buff)) Return n End Function Method compare(o1:Object) Local o:timestamp=timestamp(o1) If year >o.year Return 1 ElseIf year <o.year Return -1 If month >o.month Return 1 ElseIf month <o.month Return -1 If day >o.day Return 1 ElseIf day <o.day Return -1 If hour >o.hour Return 1 ElseIf hour <o.hour Return -1 If minute >o.minute Return 1 ElseIf minute <o.minute Return -1 If second >o.second Return 1 ElseIf second <o.second Return -1 Return 0 End Method End Type Function padbefore$(str$,char$,num%) While Len(str)<num str=char+str Wend Return str End Function |
| ||
http://www.blitzbasic.com/Community/posts.php?topic=84517#954993 As CurrentDate() and CurrentTime() are based on C standard library you can change the 'parametric string' to get what you want without too many efforts (just changing the Bmax source) http://www.cplusplus.com/reference/clibrary/ctime/strftime/ http://php.net/manual/en/function.strftime.php Cheers Last edited 2012 |
| ||
Hi, I found this while Iīm looking for to calculate a unix timestamp from a FileTime. Iīm using LibCurl, but the function to get the FileTime from the server doesnīt work. Maybe the server givenīt the info or something else. These code looks great, but I didnīt understand. Is there somebody to give an example? Thanks a lot Winfried |