'Timestamp of when address received a token in solidity

Trying to find a way or documentation in obtaining the timestamp of when an address received the token in a smart contract.

I have read many reflection based contracts that apply different levels of fees on receiving and sending the token based on the time, but naively, isn't the following just taking from the start of the swap to that point in the function being called?

uint256 private _start_timestamp = block.timestamp;

function _getMultiplier() private view returns (uint256) {
    uint256 time_since_start = block.timestamp - _start_timestamp;
    uint256 hour = 60 * 60;
    if (time_since_start < 1 * hour) {
        return (3);
    } else if (time_since_start < 2 * hour) {
        return (2);
    } else {
        return (1);
    }
}

Am I correct in my understanding that block.timestamp is only applicable from contract creation?

If so, I need to know if the current address is sending the token, when did they receive the token. How is this possible?

Another thing that has crossed my mind, I had initially thought I could use the Pancakeswap router to identify the timestamp. However, whilst the initial liquidity will be added to Pancakeswap, the same logic needs to be applied to exchanges.



Solution 1:[1]

block.timestamp in Solidity is same as Date.now() of javascript except that solidity do not returns miliseconds

e.g.

if block.timestamp return 1647410428, then at the same time

Date.now() will return 1647410428,000

Solidity don't give new Date() like JavaScript, so to get Date time Week etc. you need to use solidity library like https://github.com/bokkypoobah/BokkyPooBahsDateTimeLibrary or others available on github OR search @Google

sample solidity code to store & return Time:-

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Test{
    uint timeBro=block.timestamp;
    uint functionCallTime;
    function FunTime() public returns(uint){
      functionCallTime=block.timestamp;
      return block.timestamp;
    }
}

Solution 2:[2]

Am I correct in my understanding that block.timestamp is only applicable from contract creation?

No, block.timestamp applies to every transaction. It does not apply to reads.

If so, I need to know if the current address is sending the token, when did they receive the token. How is this possible?

This is block.timestamp when transferFrom was called.

Isn't the following just taking from the start of the swap to that point in the function being called?

Your question does not have enough source code and context information to answer this question, like how is calling of the function done and by whom.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 aakash4dev
Solution 2 Mikko Ohtamaa