This post is from a low-reputation account and contains an unverified outbound link. Be cautious before clicking external links.

Bikram Samvat Datetime Library in Ethereum/Solidity

Words
81
Reading
1 min
Listen
Play
8y

Bikram Samvat (Vikram Sambat, abbr: B.S./V.S.) is one of the ancient Hindu calendar system currently used as official calendar of Nepal. This DApp is optimized to calculate unix timestamps to B.S. dates and vice versa. It currently supports a range of dates from B.S. 2027 to 2090 (i.e., 1980-2034 A.D.)

Basic UI :
Bikram Samvat Datetime Library in Ethereum/Solidity
https://nepal-blockchain.github.io/bikram-samvat/

Github Repo: https://github.com/nepal-blockchain/bikram-samvat
This Dapp is deployed on Rinkeby Testnet and source code verified on etherscan 0x7ffa4942bfc4b25f9bfe56e21110d9df45c301f4

Example use of Datetime Library in other smart contracts.

pragma solidity >0.5.0;
contract HodlNepal {
    iBikram public _bs; // interface
    struct Vault {
        uint timestamp;
        uint value;
    }
    mapping(address => Vault) internal _vault;
    
    constructor() public {
        _bs = iBikram(0x7fFa4942bfC4b25f9Bfe56e21110d9dF45C301f4); 
        // Rinkeby Testnet
    }
    
    function info(address _addr) 
    external view 
    returns (uint _wei, uint year, uint8 month, uint8 date, 
    uint8 day, uint timestamp) {
        Vault memory _v = _vault[_addr];
        _wei = _v.value;
        (year, month, date, , , , day, timestamp) = _bs.TS2BS(_v.timestamp); 
    }
    
    function unHodl() // withdraw afterwards 
    external {    
        Vault storage _v = _vault[msg.sender];
        require(_v.timestamp < now, "HODL_TIME");
        uint _wei = _v.value;
        _v.value = 0;
        (msg.sender).transfer(_wei);
    }
    
    function hodl(uint year, uint8 month, uint8 date) // Hodl ETH / time locked
    external payable {
        uint _ts = _helper(year, month, date);
        require(_ts > now, "PAST_DATE_NOT_VALID");
        Vault storage _v = _vault[msg.sender];
        if(_v.value > 0) {
            _ts = (_v.timestamp + _ts) / 2;
        }
        _v.timestamp = _ts;
        _v.value += msg.value;
    }
    
    function _helper(uint year, uint8 month, uint8 date) 
    internal view 
    returns(uint _ts) {
        uint8 _d = _bs.getDaysInMonth(year, month);
        if(date == 0) {
            date = 1;
        } else if(date > _d) {
            date = _d;
        }
        ( , , , , , , , _ts) = _bs.BS2TS(year, month, date);
    }
    
    function () external payable {
        revert();
    }
}

Interface :

interface iBikram {
    function getDaysInMonth(uint256 _year, uint8 _month) 
        external view 
        returns(uint8);

    function isLeapYear(uint _year) 
        external view 
        returns(bool);
    
    function maxBSYear()
        external view 
        returns(uint);
    
    function nowBS() // latest block time
        external view 
        returns(uint256 year, uint8 month, uint8 date, 
        uint8 hour, uint8 min, uint8 sec, 
        uint8 day, uint256 timestamp);
    
    function BS2TS(uint256 _year, uint8 _month, uint8 _date) 
        external view 
        returns(uint256 year, uint8 month, uint8 date, 
        uint8 hour, uint8 min, uint8 sec,
        uint8 day, uint256 timestamp);
    
    function NST2TS(
        uint256 _year, uint8 _month, uint8 _date, 
        uint8 _hour, uint8 _min, uint8 _sec) 
        external view 
        returns(uint256 year, uint8 month, uint8 date,
        uint8 hour, uint8 min, uint8 sec,
        uint8 day, uint256 timestamp);
    
    function TS2BS(uint256 _timestamp) 
        external view 
        returns(uint256 year, uint8 month, uint8 date, 
        uint8 hour, uint8 min, uint8 sec, 
        uint8 day, uint256 timestamp);
}
Bikram Samvat Datetime Library in Ethereum/Solidity | Ecency