REBOL for COBOL programmers |
TIME
Date written: October 12, 2012 This page shows obtaining the time from REBOL, and some COBOL-like ways of using it. COBOL TIMEIn COBOL, the time of day can be obtained from the operating system, and is returned in the hhmmss format, or maybe with tenths of a second also, as hhmmsstt. ACCEPT identifier-1 FROM TIME. The REBOL equivalentIn REBOL, the time of day is part of the date function, "now." >> now/time == 10:45:02 >> In COBOL, a request for the time returns a fixed-format string, as in hhmmss. Interestingly, one of the common things to do with that format is to make it more readable by inserting colons. In REBOL, the time is returned WITH the colons, and turning it into a fixed format involves some fussing. One might want to do that if one wanted to use the time in, for example, a timestamp, or as part of a file name. Also of interest, if the time is before noon, the leading zero will be omitted. Here is a way to create a fixed-format time. >> now/time == 17:54:09 >> reverse copy/part reverse join "0" trim/with to-string now/time ":" 6 == "175412" >>Now that is a fine-looking piece of REBOL code. It merits an explanation. The "reverse" function reverses a string. The "join" function joins what immediately follows it with whatever follows next. The "copy/part" makes a copy of a string, for as many characters as are specified after the thing being copied. The "trim" function cuts off leading and trailing spaces, and the "with" refinement also cuts out the characters specified after the thing being trimmed. Putting it all together: The "now/time" function returns the time. The "to-string" function makes sure the "now/time" result is a string for compatibility with the "join" function. The "trim-with" function cuts out all blanks plus all colons. The "join" function puts a zero on the front of the trimmed time, in case a leading zero was missing. The "reverse" function reverses the time so that the first six digits are the time, and if there is a seventh digit, it is that leading zero. The "copy/part" function copies off six digits from the reversed string. The other (first) "reverse" function reverses the reversed time, making it in the correct order. This kind of stringing together of functions is common in REBOL so you better get used to it. REBOL documentation says you may use parentheses if it helps, but says that you just slow things down if you do. But if you need to, you could try this: reverse (copy/part (reverse (join "0" (trim/with (to-string now/time) ":"))) 6)Just to show how years of using one tool can have an influence, I personlly prefer the above format with the parentheses. I find it strangely comforting, and easier to follow. But it definitely is not the REBOL way. If you prefer to do that kind of coding only once, this operation is another candidate, just like the date function mentioned earlier, for your personal mezzanine. Coding could look something like this: GLB-HHMMSS: to-string rejoin [ reverse copy/part reverse join "0" trim/with to-string now/time ":" 6 ]The above code obtained, from the %glb.r file mentioned below, obtains the time but once. One really ought to put that operation into a function that can be called repeatedly, because many times, at least in the COBOL world, that would be a common operation. It would be very easy to do by wrapping the above code, like this: GLB-GET-TIMESTAMP: does [ above-code ] Code samplesLeft-clicking a link below will give a result that will depend on your computer, your browser, and maybe in whether or not you have REBOL installed. You should get either a new window with the code displayed, or a dialog box for saving the file on your own computer, or "opening" the file which makes no sense in this situation. Right-clicking should give you a menu from which you can save it to your own computer. Global services module |