Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1622
  • Last Modified:

Roguewave Date Time Conversion

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
Allochka187
Asked:
Allochka187
  • 2
1 Solution
 
itsmeandnobodyelseCommented:
> 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
 
Allochka187Author Commented:
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
 
itsmeandnobodyelseCommented:
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
 
migoEXCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now