Solved

Roguewave Date Time Conversion

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now