.NET Journey – Convert System Date/Time to .Net DateTime
I recently had a requirement of being able to get the current date and time from the system and use that value in a .Net program. After trying a couple of approaches I chose to call the “Retrieve System Value” API and convert the system date/time to a .Net DateTime type.
First, you will need to add a reference to the ActiveX control that allows your program to call programs on the system. Right click the “References” section under your project and under the “COM” tab on the “Add Reference” dialog look for “IBM AS/4400 iSeries Access for Windows ActiveX Object Library”. Add this to your solution. Make sure to also add “using cwbx;” to your program.
Now the code to get the date/time and convert to .Net DateTime:
private DateTime GetDB2SystemDateTime()
{
DateTime sysDateTime = new DateTime();
string wrkString;
cwbx.AS400System AS400 = new cwbx.AS400System();
cwbx.Program pgm = new cwbx.Program();
cwbx.StringConverter strCnv = new cwbx.StringConverter();
cwbx.LongConverter longCnv = new cwbx.LongConverter();
cwbx.Structure ERRC0100 = new cwbx.Structure();
cwbx.Structure SysValRcv = new Structure();
cwbx.Structure SysValInfo = new Structure();
// Define the connection and the program to call
AS400.Define(db2Conn.DataSource);
AS400.UserID = Properties.Settings.Default.iSeriesUser;
AS400.Password = Properties.Settings.Default.iSeriesPwd;
pgm.system = AS400;
pgm.LibraryName = "*LIBL";
pgm.ProgramName = "QWCRSVAL";
// Connect to the remote command server
try
{
AS400.Connect(cwbcoServiceEnum.cwbcoServiceRemoteCmd);
}
catch (Exception as400E)
{
fsw.WriteLine(LogFileDateTime() + "Error occured during AS400 logon: " + as400E.Message);
fsw.Flush();
return sysDateTime;
}
if (AS400.IsConnected(cwbcoServiceEnum.cwbcoServiceAny) == 0)
{
fsw.WriteLine(LogFileDateTime() + "Could not connect to AS400 remote command server");
fsw.Flush();
return sysDateTime;
}
// Create a parm structure for the system value receiver variable
SysValInfo.Fields.Append("SystemValue", 10);
SysValInfo.Fields.Append("TypeOfData", 1);
SysValInfo.Fields.Append("InfoStatus", 1);
SysValInfo.Fields.Append("LengthOfData", 4);
SysValInfo.Fields.Append("Data", 20);
// Create a parm structure for the system value data
SysValRcv.Fields.Append("NumSysValsRtn", 4);
SysValRcv.Fields.Append("SysValOffset", 4);
SysValRcv.Fields.Append("SysValInfo", SysValInfo.Length);
// Standard error reporting structure
ERRC0100.Fields.Append("BytesProvided", 4);
ERRC0100.Fields[1].Value = longCnv.ToBytes(272);
ERRC0100.Fields.Append("BytesAvail", 4);
ERRC0100.Fields[2].Value = longCnv.ToBytes(0);
ERRC0100.Fields.Append("ExceptionID", 7);
ERRC0100.Fields.Append("Reserved", 1);
ERRC0100.Fields.Append("ExceptionData", 272);
// Define the parms for the API call
ProgramParameters parms = new ProgramParameters();
parms.Append("RcvVar", cwbrcParameterTypeEnum.cwbrcOutput, SysValRcv.Length);
parms.Append("RcvVarLen", cwbrcParameterTypeEnum.cwbrcInput, 4);
parms["RcvVarLen"].Value = longCnv.ToBytes(parms["RcvVar"].Length);
parms.Append("NumSysVals", cwbrcParameterTypeEnum.cwbrcInput, 4);
parms["NumSysVals"].Value = longCnv.ToBytes(1);
parms.Append("SysVals", cwbrcParameterTypeEnum.cwbrcInput, 10);
parms["SysVals"].Value = strCnv.ToBytes("QDATETIME ");
parms.Append("ErrorCode", cwbrcParameterTypeEnum.cwbrcInout, ERRC0100.Length);
try
{
pgm.Call(parms);
}
catch (Exception callE)
{
fsw.WriteLine(LogFileDateTime() + "An error occured during API call to QWCRSVAL: " + callE.Message);
fsw.Flush();
AS400.Disconnect(cwbcoServiceEnum.cwbcoServiceAll);
return sysDateTime;
}
ERRC0100.Bytes = parms["ErrorCode"].Value;
SysValRcv.Bytes = parms["RcvVar"].Value;
SysValInfo.Bytes = SysValRcv.Fields["SysValInfo"].Value;
wrkString = strCnv.FromBytes(SysValInfo.Fields["Data"].Value);
try
{
sysDateTime = DateTime.Parse(wrkString.Substring(0, 4) + "-" +
wrkString.Substring(4, 2) + "-" +
wrkString.Substring(6, 2) + "T" +
wrkString.Substring(8, 2) + ":" +
wrkString.Substring(10, 2) + ":" +
wrkString.Substring(12, 2) + "." +
wrkString.Substring(14));
}
catch (Exception dtE)
{
fsw.WriteLine(LogFileDateTime() + "Error occured extracting AS400 date/time: " + dtE.Message);
fsw.Flush();
}
AS400.Disconnect(cwbcoServiceEnum.cwbcoServiceAll);
return sysDateTime;
}
Note the ERRC0100, SysValRcv and SysValInfo variables. These are a type of cwbx.Structure and you can think of these are creating data structures programatically. Also note the "fsw" object...this is basically a text file write that I'm using to log errors during the conversion process and is defined in the main section of my class as a type of StreamWriter.
When this routine is called it returns the system date/time converted to a .Net DateTime value or a DateTime value with the default for the object type.
Posted on 10th February 2009
Under: .NET Journey | Comments Off
