?
Solved

Roguewave Date Time Conversion

Posted on 2003-12-03
5
Medium Priority
?
1,595 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

719 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