Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Add one day in output date field

3 views
Skip to first unread message

Aftab

unread,
Oct 26, 2009, 3:51:28 PM10/26/09
to
Hi,

I need to add one day in input date value when formatting
for output field. Does any existing built-in function
provide the facility?

Do I have to develop a custom code? Does anyone have sample
custom code.

Thanks

Aftab

Chris

unread,
Oct 27, 2009, 10:21:54 AM10/27/09
to
I've had to write things like this in the past because the
builtin isn't there

This should work (below)
since you didn't specify, I made some assumptions on format

I also assume that the input is a date and not some type of
string or other group of numbers

Using Julian avoids needing to do leap year and month length
calculations...

Let me know how it goes..


// assume valid input date in hl7 format "YYYYMMDD[hhmmss]"
// return just date portion "YYYYMMDD"
//

string szTemp;
int iYear, iDay;

if ( pb->size() < 8 )
return 1;

pb->truncate(8);
fmtDate( pb, mode, "%Y%j", "YYYYMMDD"); // convert to
julian

szTemp = *pb; // isolate Year as integer
szTemp[4] = 0;
iYear = szTemp;

szTemp = *pb; // isolate Day as integer
szTemp.offsetCopy(szTemp, 4);
iDay = szTemp;

iDay = iDay + 1; // add a day

pb->set( "" + iDay); // set blob to day
justify( pb , mode , "R03" ); // add zeroes
pb->paste(0, "" + iYear ); // include year

fmtJulian( pb, mode, "%Y%m%d" );

if (pb->size() < 8) // failed - end of year?
{
szTemp = iYear + 1;
pb->set(szTemp + "0101");
}
return 1;

Aftab

unread,
Oct 27, 2009, 1:54:50 PM10/27/09
to
Hi Chris,

Thank you for the help, yes it works perfectly. I made a
little modification to the code because my input is six
digit date (yymmdd). Here is modified code

string szTemp;
int iYear, iDay, mYear;


szTemp = *pb; // isolate Year as integer

szTemp[2] = 0;
mYear = szTemp;

szTemp = *pb;

if (mYear > 50)
{
pb->set("19" + szTemp);
}
else
pb->set("20" + szTemp);

pb->truncate(8);
fmtDate( pb, mode, "%Y%j", "YYYYMMDD"); // convert to
julian

szTemp = *pb; // isolate Year as integer
szTemp[4] = 0;
iYear = szTemp;

szTemp = *pb; // isolate Day as integer
szTemp.offsetCopy(szTemp, 4);
iDay = szTemp;

iDay = iDay + 1; // add a day

pb->set( "" + iDay); // set blob to day
justify( pb , mode , "R03" ); // add zeroes
pb->paste(0, "" + iYear ); // include year

fmtJulian( pb, mode, "%Y%m%d" );

if (pb->size() < 8) // failed - end of year?
{
szTemp = iYear + 1;
pb->set(szTemp + "0101");
}
return 1;


Thanks again

Aftab

Josh

unread,
Oct 27, 2009, 3:31:51 PM10/27/09
to
What if your day is the 31st ? Your code would make it the
32nd....

Aftab

unread,
Oct 27, 2009, 4:15:28 PM10/27/09
to
I tested the code and it gives 1st of next month. I also
tested for leap year and it is working fine.

Aftab

Chris

unread,
Oct 27, 2009, 5:02:42 PM10/27/09
to
Josh..
The trick is using Julian day..
Than the numbers you deal with are 1-365 (more or less)
Adding one works fine until you get to the end of the year..

Which is dealt with at the end of the code
-
if the day was 31 - that would be Jan 31
Adding one is 32 - Which is Feb 1
it also does leap year for you ..

Chris

unread,
Oct 27, 2009, 5:35:37 PM10/27/09
to
Aftab,
the solution for the missing century may be problematic..
as the years move forward your code will become invalid..
Also, you need to consider the context of the field
A year of "21" when in a DOB isn't going to be in the year
2021
(until the current year is 2021)

Consider the field's usage,
if what you need is a date plus one (tomorrow)
I'd guess the year always starts with a "20"

however, if things vary; try this code:
input is YYMMDD
output is YYYYMMDD
[includes notes on making the output YYMMDD]

the main "trick" is changes to this line:
fmtDate( pb, '\004', "%Y%j", "YYMMDD");

the "YYMMDD" specifies the input will be a 2 character year
the '\004' specifies using the Impact! solution for Y2K
it will assume "19" for "86" and "20" for "09"

--- code starts --------------------


string szTemp;
int iYear, iDay;

if ( pb->size() < 6 )
return 1;

pb->truncate(6);
fmtDate( pb, '\004', "%Y%j", "YYMMDD"); // convert to julian
day

szTemp = *pb; // isolate Year as integer
szTemp[4] = 0;
iYear = szTemp;

szTemp = *pb; // isolate Day as integer
szTemp.offsetCopy(szTemp, 4);
iDay = szTemp;

iDay = iDay + 1; // add a day

pb->set( "" + iDay); // set blob to day
justify( pb , mode , "R03" ); // add zeroes
pb->paste(0, "" + iYear ); // include year

fmtJulian( pb, mode, "%Y%m%d" ); // %y for two digit year

if (pb->size() < 8) // end of year - change to 6 for two
digit year

0 new messages