In RPG, use a date, in *job format from a file - date

I'm trying to convert, in a RPG program, a date from a file (it's from the DSPJRN command, so the field si 6 numeric, in JOB format).
I want to use it as a date in my program, but I can't achieve it correctly.
I have tried to describe a field with type "D": date, keyword datfmt(*job) to convert the value from the file, but datfmt(*job) is incorrect (error RNF0612)
I have tried to retrieve the job Date format from a CLP program with RTVJOBA DATFMT(&FMT), and use the variable in RPG to convert the date like this
eval ztJODATE = %date(JODATE:FMT)
but it doesn't compile : error RNF0606 . I think that I can't use a variable for the format in the %date built-in function.
Is this a way to do what I want, or am I forced to transform the date value in SQL before using its value in RPG?
PS: I don't want to hardcode the format in my RPG program!

You can specify *JOBRUN for %DATE.
eval ztJODATE = %date(JODATE:*JOBRUN)
Note that RPG retrieves the job date format during initialization of the module, so if you change the job date format while the program is running, RPG will not understand the date.

Rather than using DSPJRN to an outfile, the recommended way to retrieve and process journal entries would be to use one of the programatic interfaces provided by IBM i.
Retrieve Journal Entries (QjoRetrieveJournalEntries) API
QSYS2.DISPLAY_JOURNAL() stored procedure
You could also use a *TYPE3 or higher format for the output file. Rather than the separate job formatted data & time fields, there's a single char(26) system timestamp field.
Having said that, there is a Convert Date and Time Format (QWCCVTDT) API that accepts '*JOB' as an input format specifier..

Related

How do I convert Stata dates (%td format e.g. 30jan2015) into YYYYMMDD format (e.g. 20150130)

* date is in %td format
gen date1 = real(string(mofd(daily(date, "DMY")), "%tmCYN"))
* type mismatch error
tostring date, gen(dt)
gen date1 = real(string(mofd(daily(dt, "DMY")), "%tmCYN"))
* the code runs but generates no results
tostring date, gen(dt)
gen date2=date(dt, "YMD")
* the code runs but generates no results
If a date variable has a display format %td it must be numeric and stored as some kind of integer. The display format is, and is only, an instruction to Stata on how to display such integers. Confusions about conversion often seem to hinge on a misunderstanding about what format means, as format is an overloaded word in computing, referring variously to file format (as in graphics file format, .png or jpg or whatever); data layout (as in wide or long layout, structure or format); variable or storage type; and (here) display format. There could well be yet other meanings.
A date displayed as 30jan2015 is stored as an integer, namely
. display mdy(1, 30, 2015)
20118
and a glance at help data types shows that your variable date could be stored as an int, float, long or double. All would work, although int is least demanding of memory. You would need (e.g.) to run describe date to find out which type is being used in your case, but nothing to come in this answer depends on knowing that type. Note that finding out what Stata is doing and thinking can be illuminated by running display with simple, single examples.
Your question is ambiguous.
Want to change display format? If you wish merely to see your dates in a display format exemplified by 20150130 then consulting help datetime display formats shows that the display format is as tested here with display, which can be abbreviated all the way down to di
. di %tdCCYYNNDD 20118
20150130
so
format date %tdCCYYNNDD
is what you need. That instructs Stata to change the display format, but the numbers stored remain precisely as they were.
Want such dates as variables held as integers? If you want the dates to be held as integers like 20150130 then you could convert it to string using the display format above, and then to a real value. A minimal sandbox dataset shows this:
. clear
. set obs 1
Number of observations (_N) was 0, now 1.
. gen date = 20118
. gen wanted = real(strofreal(date, "%tdCCYYNNDD"))
. format wanted %8.0f
. l
+------------------+
| date wanted |
|------------------|
1. | 20118 20150130 |
+------------------+
A display format such as %8.0f is needed to see such values directly.
Another method is to generate a large integer directly. You need to be explicit about a suitable storage type and (as just mentioned) need to set an appropriate format, but it can be got to work:
. gen long also = 10000 * year(date) + 100 * month(date) + day(date)
. format also %8.0f
Want such dates as variables held as strings? This is the previous solution, but leave off the real(). The default display format will work fine.
. gen WANTED = strofreal(date, "%tdCCYYNNDD")
. l
+-----------------------------+
| date wanted WANTED |
|-----------------------------|
1. | 20118 20150130 20150130 |
+-----------------------------+
I have not used tostring here but as its original author I have no bias against it. The principles needed here are better illustrated using the underlying function strofreal(). The older name string() will still work.
Turning to your code,
tostring date, gen(dt)
will just put integers like 20118 in string form, so "20118", but there is no way that Stata can understand that alone to be a daily date. You could have run tostring with a format argument, which would have been equivalent to the code above. The advantage of tostring would only be if you had several such variables you wished to convert at once, as tostring would loop over such variables for you.
I can't follow why you thought that conversion to a monthly date or use of a monthly date display format was needed or helpful, as at best you'd lose the information on day of the month. Thus at best Stata can only map a monthly date back to the first day of that month, and at worst a monthly date (here 660) could not be understood as anything you want.
. di mofd(20118)
660
. di %td mofd(20118)
22oct1961
. di %td dofm(mofd(20118))
01jan2015
There is no shortcut to understanding how Stata thinks about dates that doesn't involve reading the needed parts of help datetime and help datetime display formats.
Yet more explanation and examples can be found at https://www.stata-journal.com/article.html?article=dm0067

Getting date format from string

I have what I think it's a simple question.
I have a file named like this: 'prec/CHIRPS/P_CHIRPS.v2.0_mm-day-1_daily_2020.01.01.tif' and you can see it has a date within.
I've used successfully the package datefinder to extract the date from that string, but now what I want to do it's actually get the format from which datefinder read that date. That means that I want an output to be '%Y.%m%.d%' so I can use it to write a file with that same date format.
This is for example to be able to use whichever format of date and whichever file name I have, extract both the date and its format, and finally rewrite something like 'this is just an example of the file name with the date 2020.01.01'.
Thanks!!

Reading Time format data in spreadsheet using perl

while reading the spreadsheet using perl, how the time format data (in a cell) in spreadsheet got read by perl ?
I tried to read text and numeric type data from a cell in worksheet, as a result, it got read as text and numeric in perl,
but while reading a time format data, it got converted to some float value(of format - HH:MM:SS/HH:MM/MM:SS), my doubt is that, how the time format ([H]:MM:SS) from a cell got read by perl ?
For example:
$cell holds the value of 13:7(is in time format [H]:MM:SS format in spreadsheet)
while reading it using perl,
$type = $cell->type() //returns the type : Numeric/Text
$format = $cell->get_format() // returns the format (i.e)if Numeric :returns time format of HH:MM:SS / HH:MM / MM:SS formats only
In such case, how data present in $cell can be read by perl? and how
other time formats such as MM:SS.0/[H]:MM:SS/YYYY-MM-DD HH:MM:SS/ [HH]:MM:SS...are identified?
In most cases (maybe all, but I've learned to be wary of statements like that), dates in Excel spreadsheets are stored as floating-point numbers where the integer part of the number is the number of days since the start of 1900 and the decimal part is how far through the day we are.
You can, of course, use that information to parse the data yourself. But there's no need to do that in a world where DateTime::Format::Excel exists.

How to format input for SAS's MONYY format

I currently have a dataset with dates in the format "FY15 FEB". In attempting to format this variable for use with SAS's times and dates, I've done the following:
data temp;
set pre_temp;
yr = substr(fiscal,3,2);
month = substr(fiscal,6,length(fiscal));
mmmyy = month||yr;
input mmmyy MONYY5.;
datalines;
run;
So, I have the strings representing the year and corresponding month. However, running this code gives me the error "The informat $MONYY was not found or could not be loaded." Doing some background on this error tells me that it has something to do with passing the informat a value with the wrong type; what should I alter in order to get the correct output?
*Edit: I see on the SAS support page for formats that "MONYYw. expects a SAS date value as input;" given this, how do I go from strings to a different date format before this one?
When you see a $, it means character value. In this case, you're feeding SAS a character value and giving it a numeric format. SAS inserts the $ for you, but there is no such format in existence.
I'm going to ignore the datalines statement, because I'm not sure why it's there (though I do notice there is no set statement). You might have an easier time just changing your program to:
data temp;
yr = substr(fiscal,3,2);
month = substr(fiscal,6,length(fiscal));
pre_mmmyy = strip(month)||strip(yr);
mmmyy=input(pre_mmmyy,MONYY5.);
run;
you can also remove the "length(fiscal))" from the substring function. The 3rd argument to the substring function is optional, and will go to the end of the string by default.

logstash reformat dates at OUTPUT time

I'm outputting to CSV and I'd like my dates in ISO8601 format, such as 2014-04-02T19:21:36.292Z, but I keep getting dates like Mar 27 2014 17:56:33 in my csv.
I'm fine to create a second intermediate string variable to do the formatting, but it yields the same result.
I see that there's a "sprintf" function in Logstash, but it seems you can do EITHER variable references OR date formats (which I assume will get the current system date time), but I don't think you can do both. I other words, I don't think it lets you apply a date format to an existing date variable, or if it does I'm not sure what the syntax would be.
Plenty of false hits on Google and stack, but all are about parsing.
Ironically stdout happens to output in the format I want, using stdout { debug => true codec => "rubydebug"}. Maybe that could somehow help in my case, not sure? Although other folks might want some other arbitrary format.
Try this one. Add a new "date" field then output to csv.
filter {
ruby {
code => '
require "time"
event["date"] = Time.parse(event["#timestamp"].to_s).iso8601;
'
}
}