Solved

Roguewave Date Time Conversion

Posted on 2003-12-03
5
1,542 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

751 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