REBOL for COBOL programmers |
TODAYS-DATE
Date written: October 12, 2012 This page explains the REBOL equivalent of various date formats. COBOL date formatsCOBOL has several ways of getting the date in several formats, depending on the version of COBOL. For example, MOVE TODAYS-DATE TO identifier-1. ACCEPT identifier-1 FROM DATE. ACCEPT identifier-1 FROM CENTURY-DATE. The REBOL equivalentREBOL has a function that returns date, namely, "now." The "now" function has "refinements," which are key words attached to the word "now" with a slash, as in the following examples pasted from the interpreter command line. >> now == 12-Oct-2012/10:44:33-5:00 >> now == 12-Oct-2012/10:44:50-5:00 >> now/year == 2012 >> now/month == 10 >> now/day == 12 >> now/time == 10:45:02 >> now/zone == -5:00 >> now/date == 12-Oct-2012 >> now == 12-Oct-2012/10:45:29-5:00 >> now/date == 12-Oct-2012 >> now/time == 10:45:35 >> now/year == 2012 >> now/month == 10 >> now/day == 12 >> now/zone == -5:00 >> now/weekday == 5 >> now/yearday == 286 >> now/precise == 12-Oct-2012/10:46:05.358-5:00 >> In COBOL, a request for a date returns a fixed-format string, as in yyyymmdd. In REBOL you can accomplish the same thing by joining the year, month, and day together. A handy function for doing that is the "rejoin" function which takes as a parameter a block of things to be joined. The word "rejoin" is short for "reduce" and "join," which means that the block will be "reduced," that is, any REBOL words or functions in the block will be evaluated, and then the results will be "joined" together without any embedded spaces. For example: >> rejoin [now/year now/month now/day] == "20121012" >> If the above example had been created in a month prior to October, you would see that the month and day refinements produce a month or day number without leading zeros. In COBOL, it often is desired to get the date in that fixed format, for use in date stamps, or with editing pictures. If you want to do that in REBOL, you have to work a bit, as in the following example. A note about code samples like this. The way I do something is not necessarily the best way. It is a way I have figured out or (more likely) harvested from the internet. Remember, whenever you see a code sample here, there might be a better way of doing it. Do not accept any code sample here as the gospel. So here is a way to get a date in yyyymmdd format. It gets the month and day and makes sure they are two digits. It does this by adding a zero to the front of each to make sure it is at least two digits, then it reverses each and takes two digits to make sure it is only two digits, and then reverses it again to put the digits in the right order. In other words, if the month is May, here is what you would do to the "5." 1. Add a zero to the front to get 05. 2. Reverse it to get 50. 3. Take two digits to get 50. 4. Reverse it to get 05.If the month is December, this works also, it just is not necessary. So here is a way to get the current date in yyyymmdd format. The line of REBOL code is chopped into two lines just to make it fit better on this page. >> rejoin [now/year reverse copy/part reverse join 0 now/month 2 reverse copy/part reverse join 9 now/day 2] == "20121012" >> The above code sample is a good introduction to the concept of your personal "mezzanine" as suggested earlier. That line of code is a lot to type and a lot to remember. So, if you need the current date in the yyyymmdd format in a lot of your programs, why not make your own function to produce it. That has been done, and has been put into the "Global Services Module" mentioned below and elswhere. When you "do" the file that contains all those "global services," you will get a REBOL word called GLB-YYYYMMDD that contains the date. The code that produces it looks like this: GLB-NOW: now GLB-YYYYMMDD: to-string rejoin [ GLB-NOW/year reverse copy/part reverse join 0 GLB-NOW/month 2 reverse copy/part reverse join 0 GLB-NOW/day 2 ]If you look at the code in the whole file, you will see that the code to produce GLB-YYYYMMDD is not a funcion, it is just the in-line code that is executed when you "do" the %glb.r file. The reason for that is that normally, getting the current date is something you would do once in a program, since most program run over a period of much less than one whole day. Another thing to note about the above code is that the "now" function is executed only once to get the date into the "variable" GLB-NOW. Then, in the code that follows that line, the year, month, and day refinements are applied to GLB-NOW. That can be done because GLB-NOW is a "date" datatype by virtue of being set by the "now" function. That is an interesting part of how REBOL works. 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 |