3

I'm implementing a function and I'm getting an unexplained bug from the compiler

function _incrementMonths(uint16 _months, uint _timestamp) internal returns (uint){
    DateTime dateTime = new DateTime();
    uint16 year = dateTime.getYear(_timestamp);
    uint16 month = dateTime.getMonth(_timestamp);

    uint16 newMonth = month + _months;
    while(newMonth > 12){
        newMonth -= 12;
        year++;
    }

    // get new timestamp
    return dateTime.toTimestamp(
        year,
        newMonth,
        dateTime.getDay(_timestamp),
        dateTime.getHour(_timestamp),
        dateTime.getMinute(_timestamp),
        dateTime.getSecond(_timestamp)
    );
}

This function gives the following error

browser/Payroll.sol:327:16: TypeError: Member "toTimestamp" is not available in contract DateTime outside of storage.
        return dateTime.toTimestamp(
               ^------------------^

The contract I'm referencing is found here: https://github.com/pipermerriam/ethereum-datetime/blob/master/contracts/DateTime.sol . The method I'm calling is found here https://github.com/pipermerriam/ethereum-datetime/blob/master/contracts/DateTime.sol#L165

I've been trying to debug this, but I don't see what is causing this error. What is the cause?

2

As per the solution given for No matching declaration found after argument-dependent lookup, my problem was due to incorrect typecasting of the month variable. I had typecast the month as uint16 when it should have been uint8, thanks to piper-merriam for pointing out my folly.

Changing the function to as shown below resolved the issue:

function _incrementMonths(uint8 _months, uint _timestamp) internal returns (uint){
    DateTime dateTime = new DateTime();
    uint16 year = dateTime.getYear(_timestamp);
    uint8 month = dateTime.getMonth(_timestamp);

    uint8 newMonth = month + _months;
    while(newMonth > 12){
        newMonth -= 12;
        year++;
    }

    // get new timestamp
    return dateTime.toTimestamp(
        year,
        newMonth,
        dateTime.getDay(_timestamp),
        dateTime.getHour(_timestamp),
        dateTime.getMinute(_timestamp),
        dateTime.getSecond(_timestamp)
    );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.