Solved

Roguewave Date Time Conversion

Posted on 2003-12-03
5
1,519 Views
Last Modified: 2011-09-20
I am using Roguewave libraries to compile my code.
I am getting errors when trying to insert RWDatetime variable into Sybase Database.

If I pass aCleanupDate = " " only then the error occurs
If I pass aCleanupDate = "12/02/03 00:00:00.000" it works.

This is the code that I have:

const char * aCleanupDate;
RWDBDateTime cu_eff_dt;

        if (*aCleanupDate == NULL || aCleanupDate == " ")
        {
            aCleanupDate = cu_eff_dt.asString();
        }


        RWCString clDate = RWCString(aCleanupDate);
        RWCTokenizer token = clDate;

        unsigned long vals[7];
        const RWLocale& globalLoc = RWLocale::global();
        RWCString str;
        for(size_t i=0; !((str=token(" /:.\t\n")).isNull()); ++i)
        {
                globalLoc.stringToNum(str, &(vals[i]));
        }

        RWDBDateTime rwdbdt(vals[2], vals[0], vals[1], vals[3],
                            vals[4], vals[5], vals[6]);

        m_pPartialCuResSp->SetStoredProc() << rwdbdt;
0
Comment
Question by:Allochka187
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 125 total points
ID: 9873272
> If I pass aCleanupDate = " " only then the error occurs

There are some errors in the code below, and i wonder that it has compiled

const char * aCleanupDate;         // That pointer is not initialized but const
                                                // Normally, you cannot assign it to any other pointer because it is const;
RWDBDateTime cu_eff_dt;

        // Somewhere between you should get a valid pointer for aCleanupDate

        // The second comparision (aCleanupDate == " ") is always false
        // because you compare pointers of different allocated memory
        // if you want compare strings you have to use  strcmp or a string class

        if (*aCleanupDate == NULL || aCleanupDate == " ")
        {
            aCleanupDate = cu_eff_dt.asString();
        }

I didn't check the rest of your code but if you say it works, that is fine.

Regards, Alex


0
 

Author Comment

by:Allochka187
ID: 9874655
Hi Alex,
    Actaully aCleanupDate variable is passed to a function initialized as " ".
I think the problem is when I assign date value to aCleanupDate.
aCleanupDate = cu_eff_dt.asString();
It assigns current date and time.

When aCleanupDate is initialized to "12/02/03 00:00:00.000"
then the code below works.

RWCString clDate = RWCString(aCleanupDate);
        RWCTokenizer token = clDate;

        unsigned long vals[7];
        const RWLocale& globalLoc = RWLocale::global();
        RWCString str;
        for(size_t i=0; !((str=token(" /:.\t\n")).isNull()); ++i)
        {
                globalLoc.stringToNum(str, &(vals[i]));
        }

        RWDBDateTime rwdbdt(vals[2], vals[0], vals[1], vals[3],
                            vals[4], vals[5], vals[6]);

        m_pPartialCuResSp->SetStoredProc() << rwdbdt;


0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 9875254
You should make a difference between a const char * - that is a pointer - and string that is an instance of a string class like RWCString.

If you have a code like

       const char* pc = dt.asString();

then dt.asString() returns a RWCString object. As the compiler is not able to assign a RWCString to a const char* it would give a compiler error.  However,  RWCString has a so-called cast operator,  operator const char*,  that does the job, i. e. it converts the RWCString to const char* by returning the internal character buffer of a RWCString as const char*.
For the example above this works, because the variable 'pc' is initialized at time of definition.

However, in your previous code, you had the definition 'const char* aCleanupDate' somewhere and an assignment  'aCleanupDate =  cu_eff_dt.asString();' somewhere else. My compiler would give an error message 'cannot assign to a const char*', because this assignment would change the const pointer.

But, as i tried to explain with my first answer: If you don't get compiler errors, it is ok. The assignment is correct beside of the const issue, so you should get a valid date string if (and only if) cu_eff_dt  is a valid date-time object.

But, definitively wrong is to compare aCleanupDate with " " because that is never true. aCleanupDate is a pointer that is say 0x000bccdd. The temporary constant " " has always another address value and is never 0x000bccdd. With

        if (*aCleanupDate == NULL || aCleanupDate == " ")    
        {
            aCleanupDate = cu_eff_dt.asString();  
        }

you have made 3 mistakes:

1. *aCleanupDate == NULL means that the first character of your string is 0 (binary zero),
     that means the string is empty. If you want to test your pointer notbeeing NULL you've to use

      if (aCleanupDate == NULL)  // test if pointer is NULL

2. aCleanupDate == " " is always false as explained before
3. aCleanupDate = cu_eff_dt.asString();   will therefore only reached if you intialize aCleanupDate
    with an empty string:

            aCleanup = "";


If you want to compare string values you may use strcmp

  if ( strcmp(aCleanupDate, " ") == 0 )   // string values are equal ?

or convert aCleanupDate to a RWCString

  if ( RWCString(aCleanupDate) == "" )  // RWCString has an operator== that takes a right-side const char*

I hope that is clear now.

Regards, Alex
       
0
 
LVL 5

Expert Comment

by:migoEX
ID: 10364863
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept itsmeandnobodyelse's comment as answer.

Please leave any comments here within the next four days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

migoEX
EE Cleanup Volunteer
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question