Created
May 31, 2017 17:45
-
-
Save anonymous/90019d14b89f1865c7bf3668e5c22847 to your computer and use it in GitHub Desktop.
Created using browser-solidity: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.11+commit.68ef5810.js&optimize=undefined&gist=
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.11; | |
contract BikramSamvat{ | |
/* | |
* Unix timestamp to Bikram Samvat (Date Time Library) | |
* @NepalBitcoin | |
*/ | |
address Bikram; | |
enum DaysInWeek{SUN,MON,TUE,WED,THU,FRI,SAT} | |
struct MITI { | |
uint16 YY; | |
uint8 MM; | |
uint8 DD; | |
uint8 hh; | |
uint8 mm; | |
uint8 ss; | |
uint8 day; | |
uint64 timestamp; | |
} | |
struct BSYears{ | |
bytes16 _hash; | |
uint32 _leapCount; | |
int8[12] _months; | |
uint16 _daysIn6Months; | |
bool _isLeap; | |
} | |
/* struct CORE { | |
int8[12] _months; | |
uint16 _daysIn6Months; | |
bool _isLeap; | |
}*/ | |
mapping(uint16 => BSYears) public BS; | |
//mapping (bytes16 => CORE) public DBCore; | |
uint64 subEpoch = 5 years; | |
uint64 NST = 5 hours + 45 minutes; | |
uint64 epochBalance = 103 days - NST; | |
uint16 MaxBSYear = 2027; | |
function BikramSamvat(){ | |
Bikram = msg.sender; | |
} | |
function toTimestamp (uint16 _YY, uint8 _MM, uint8 _DD, uint8 _hh, uint8 _mm, uint8 _ss) private constant checkBS2TS(_YY,_MM,_DD,_hh,_mm,_ss) returns (MITI miti){ | |
miti.timestamp = epochBalance; | |
miti.timestamp += 366 days * BS[_YY]._leapCount; | |
miti.timestamp += 365 days * (_YY - 2027 - BS[_YY]._leapCount); | |
miti.MM = 1; | |
if(_MM > 6){ | |
miti.MM = 7; | |
miti.timestamp += 1 days * getDaysIn6Months(_YY); | |
} | |
while(miti.MM < _MM){ | |
miti.timestamp += 1 days * getDaysInMonth(_YY, miti.MM); | |
miti.MM++; | |
} | |
miti.YY= _YY; | |
miti.DD= _DD; | |
miti.timestamp += 1 days * (_DD -1); | |
miti.timestamp += uint64(_hh) * 1 hours; | |
miti.timestamp += uint64(_mm) * 1 minutes; | |
miti.timestamp += _ss; | |
miti.day = getWeekDay(miti.timestamp); | |
miti.hh = _hh; | |
miti.mm = _mm; | |
miti.ss = _ss; | |
} | |
modifier checkBS2TS(uint16 _YY, uint8 _MM, uint8 _DD, uint8 _hh, uint8 _mm, uint8 _ss){ | |
if(_YY < 2027 || _YY > MaxBSYear) throw; | |
if(_MM < 1 || _MM > 12) throw; | |
if(_DD < 1 || _DD > getDaysInMonth(_YY, _MM)) throw; | |
if(_hh > 23) throw; | |
if(_mm > 59) throw; | |
if(_ss > 59) throw; | |
_; | |
} | |
function BS2TS(uint16 _YY, uint8 _MM, uint8 _DD, uint8 _hh, uint8 _mm, uint8 _ss) public constant returns (uint16 YY, uint8 MM, uint8 DD, uint8 hh, uint8 mm, uint8 ss, uint8 day, uint64 timestamp){ | |
MITI memory miti = toTimestamp(_YY,_MM,_DD,_hh,_mm,_ss); | |
return (miti.YY, miti.MM, miti.DD, miti.hh,miti.mm,miti.ss,miti.day,miti.timestamp); | |
} | |
function TS2BS(uint64 _timestamp) public constant returns (uint16 YY, uint8 MM, uint8 DD, uint8 hh, uint8 mm, uint8 ss, uint8 day, uint64 timestamp){ | |
MITI memory miti = toBikramSamvat(_timestamp); | |
return (miti.YY, miti.MM, miti.DD, miti.hh,miti.mm,miti.ss,miti.day,miti.timestamp); | |
} | |
function toBikramSamvat(uint64 timestamp) private constant returns(MITI miti){ | |
miti.timestamp = timestamp; | |
miti = TSYearCounter(miti); | |
miti = TSMonthCounter(miti); | |
miti.DD = 1 + uint8(miti.timestamp / 1 days); | |
miti.timestamp = miti.timestamp % 1 days; | |
miti.hh = getHour(timestamp);//uint8(miti.timestamp / 1 hours); | |
miti.mm = getMinute(timestamp); //uint8(miti.timestamp / 1 minutes); | |
miti.ss = getSecond(timestamp);//uint8(miti.timestamp); | |
miti.timestamp = timestamp; | |
miti.day = getWeekDay(timestamp); | |
return miti; | |
} | |
function getHour(uint64 _timestamp) constant returns (uint8) { | |
return uint8(((_timestamp - epochBalance) / 1 minutes / 1 minutes) % 24); | |
} | |
function getMinute(uint64 _timestamp) constant returns (uint8) { | |
return uint8(((_timestamp - epochBalance) / 1 minutes) % 1 minutes); | |
} | |
function getSecond(uint64 _timestamp) constant returns (uint8) { | |
return uint8((_timestamp - epochBalance) % 1 minutes); | |
} | |
function getWeekDay(uint64 _timestamp) returns (uint8){ | |
return uint8(((_timestamp - epochBalance) / 1 days + 2) % 7); | |
} | |
function TSYearCounter(MITI miti) internal constant returns(MITI){ | |
miti.YY = 2027; | |
miti.timestamp -= epochBalance; | |
miti.YY += uint16(5 * miti.timestamp / subEpoch); | |
miti.timestamp = miti.timestamp % subEpoch; | |
miti.timestamp -= 1 days * BS[miti.YY]._leapCount; | |
while(miti.timestamp > 364 days){ | |
if(!BS[miti.YY]._isLeap){ | |
miti.timestamp -= 365 days; | |
}else{ | |
miti.timestamp -= 366 days; | |
} | |
miti.YY++; | |
} | |
return miti; | |
} | |
function TSMonthCounter(MITI miti) internal constant returns (MITI){ | |
miti.MM = 1; | |
uint16 daysIn6Months = getDaysIn6Months(miti.YY); | |
if((miti.timestamp/1 days) > daysIn6Months){ | |
miti.MM = 7; | |
miti.timestamp -= (daysIn6Months * 1 days); | |
} | |
uint8 daysInMonth; | |
for(miti.MM; miti.MM < 12; miti.MM++){ | |
daysInMonth = getDaysInMonth(miti.YY,miti.MM); | |
if(miti.timestamp < (daysInMonth * 1 days)) | |
break; | |
miti.timestamp -= (daysInMonth * 1 days); | |
} | |
return miti; | |
} | |
function setBS(uint16 _YY, int8[12] _months) public onlyBikram() { | |
//bytes16 hash = bytes16(sha3(_months)); | |
if(BS[_YY]._daysIn6Months == 0){ | |
setBSCore(_YY, _months); | |
} | |
if(!isLeapYear(_YY)){ | |
BS[_YY + 1]._leapCount = BS[_YY]._leapCount; | |
}else{ | |
BS[_YY + 1]._leapCount = BS[_YY]._leapCount + 1; | |
} | |
//BS[_YY]._hash = hash; | |
} | |
function setBSCore(uint16 _YY, int8[12] _months) internal constant{ | |
int16 countDays; | |
uint16 daysIn6Months; | |
for(uint8 i = 0; i < 12;i++){ | |
if(_months[i] < -1 || _months[i] > 2){ | |
throw; | |
} | |
countDays += _months[i]; | |
if(i<6) { | |
daysIn6Months += uint16(int8(30)+_months[i]); | |
} | |
} | |
if(countDays == 5){ | |
BS[_YY]._isLeap = false; | |
}else if(countDays == 6){ | |
BS[_YY]._isLeap = true; | |
}else { | |
throw; | |
} | |
BS[_YY]._daysIn6Months = daysIn6Months; | |
BS[_YY]._months = _months; | |
} | |
function TSCorrection(uint64 _timestamp) internal constant returns (uint64){ | |
return (((_timestamp) / 1 days) * 1 days); | |
} | |
function getDaysInMonth(uint16 _YY, uint8 _MM) internal constant returns(uint8){ | |
return uint8(int8(30) + BS[_YY]._months[_MM - 1]); | |
} | |
function getDaysIn6Months(uint16 _YY) internal constant returns(uint16){ | |
return BS[_YY]._daysIn6Months; | |
} | |
function isLeapYear(uint16 _YY) internal constant returns(bool){ | |
return BS[_YY]._isLeap; | |
} | |
modifier checkTimestamp(uint64 timestamp){ | |
//if(timestamp < 8878500 || timestamp > ){ | |
// throw; | |
//} | |
_; | |
} | |
modifier onlyBikram(){ | |
if(msg.sender != Bikram) | |
throw; | |
_; | |
} | |
function DBUpdate(); | |
function(){ | |
throw; | |
} | |
} | |
contract XX is BikramSamvat { | |
enum dbStage{zero,one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen} | |
dbStage stageNow; | |
modifier dbStaging(){ | |
if(stageNow > dbStage.twelve) | |
throw; | |
_; | |
} | |
function DBUpdate() onlyBikram() dbStaging(){ | |
if(stageNow == dbStage.zero){ | |
EpochUpdate1(); | |
stageNow = dbStage.one; | |
}else if(stageNow == dbStage.one){ | |
EpochUpdate2(); | |
stageNow = dbStage.two; | |
}else if(stageNow == dbStage.two){ | |
EpochUpdate3(); | |
stageNow = dbStage.three; | |
}else if(stageNow == dbStage.three){ | |
EpochUpdate4(); | |
stageNow = dbStage.four; | |
}else if(stageNow == dbStage.four){ | |
EpochUpdate5(); | |
stageNow = dbStage.five; | |
}else if(stageNow == dbStage.five){ | |
EpochUpdate6(); | |
stageNow = dbStage.six; | |
}else if(stageNow == dbStage.six){ | |
EpochUpdate7(); | |
stageNow = dbStage.seven; | |
}else if(stageNow == dbStage.seven){ | |
EpochUpdate8(); | |
stageNow = dbStage.eight; | |
}else if(stageNow == dbStage.eight){ | |
EpochUpdate9(); | |
stageNow = dbStage.nine; | |
}else if(stageNow == dbStage.nine){ | |
EpochUpdate10(); | |
stageNow = dbStage.ten; | |
}else if(stageNow == dbStage.ten){ | |
EpochUpdate11(); | |
stageNow = dbStage.eleven; | |
}else if(stageNow == dbStage.eleven){ | |
EpochUpdate12(); | |
stageNow = dbStage.twelve; | |
}else if(stageNow == dbStage.eleven){ | |
EpochUpdate13(); | |
stageNow = dbStage.thirteen; | |
} | |
} | |
function EpochUpdate1() internal { | |
setBS(2027,[int8(0),2,1,2,1,0,0,0,-1,0,-1,1]); | |
setBS(2028,[int8(1),1,2,1,1,1,0,-1,0,-1,0,0]); | |
setBS(2029,[int8(1),1,2,1,2,0,0,-1,0,-1,0,0]); | |
} | |
function EpochUpdate2() internal { | |
setBS(2030,[int8(1),2,1,2,1,0,0,0,-1,-1,0,1]); | |
setBS(2031,[int8(0),2,1,2,1,0,0,0,-1,0,-1,1]); | |
setBS(2032,[int8(1),1,2,1,1,1,0,-1,0,-1,0,0]); | |
setBS(2033,[int8(1),1,2,2,1,0,0,-1,0,-1,0,0]); | |
} | |
function EpochUpdate3() internal { | |
setBS(2034,[int8(1),2,1,2,1,0,0,0,-1,-1,0,1]); | |
setBS(2035,[int8(0),2,1,2,1,1,-1,0,0,-1,-1,1]); | |
setBS(2036,[int8(1),1,2,1,1,1,0,-1,0,-1,0,0]); | |
setBS(2037,[int8(1),1,2,2,1,0,0,-1,0,-1,0,0]); | |
} | |
function EpochUpdate4() internal { | |
setBS(2038,[int8(1),2,1,2,1,0,0,0,-1,-1,0,1]); | |
setBS(2039,[int8(1),1,1,2,1,1,-1,0,0,-1,0,0]); | |
setBS(2040,[int8(1),1,2,1,1,1,0,-1,0,-1,0,0]); | |
setBS(2041,[int8(1),1,2,2,1,0,0,-1,0,-1,0,0]); | |
} | |
function EpochUpdate5() internal { | |
setBS(2042,[int8(1),2,1,2,1,0,0,0,-1,-1,0,1]); | |
setBS(2043,[int8(1),1,1,2,1,1,-1,0,0,-1,0,0]); | |
setBS(2044,[int8(1),1,2,1,1,1,0,-1,0,-1,0,0]); | |
setBS(2045,[int8(1),2,1,2,1,0,0,-1,0,-1,0,0]); | |
} | |
function EpochUpdate6() internal { | |
setBS(2046,[int8(1),2,1,2,1,0,0,0,-1,-1,0,1]); | |
setBS(2047,[int8(1),1,1,2,1,1,0,-1,0,-1,0,0]); | |
setBS(2048,[int8(1),1,2,1,1,1,0,-1,0,-1,0,0]); | |
setBS(2049,[int8(1),2,1,2,1,0,0,0,-1,-1,0,0]); | |
setBS(2050,[int8(1),2,1,2,1,0,0,0,-1,0,-1,1]); | |
} | |
function EpochUpdate7() internal { | |
setBS(2051,[int8(1),1,1,2,1,1,0,-1,0,-1,0,0]); | |
setBS(2052,[int8(1),1,2,1,1,1,0,-1,0,-1,0,0]); | |
setBS(2053,[int8(1),2,1,2,1,0,0,0,-1,-1,0,0]); | |
setBS(2054,[int8(1),2,1,2,1,0,0,0,-1,0,-1,1]); | |
setBS(2055,[int8(1),1,2,1,1,1,0,-1,0,-1,0,0]); | |
} | |
function EpochUpdate8() internal { | |
setBS(2056,[int8(1),1,2,1,2,0,0,-1,0,-1,0,0]); | |
setBS(2057,[int8(1),2,1,2,1,0,0,0,-1,-1,0,1]); | |
setBS(2058,[int8(0),2,1,2,1,0,0,0,-1,0,-1,1]); | |
setBS(2059,[int8(1),1,2,1,1,1,0,-1,0,-1,0,0]); | |
setBS(2060,[int8(1),1,2,2,1,0,0,-1,0,-1,0,0]); | |
} | |
function EpochUpdate9() internal { | |
setBS(2061,[int8(1),2,1,2,1,0,0,0,-1,-1,0,1]); | |
setBS(2062,[int8(0),2,1,2,1,1,-1,0,-1,0,-1,1]); | |
setBS(2063,[int8(1),1,2,1,1,1,0,-1,0,-1,0,0]); | |
setBS(2064,[int8(1),1,2,2,1,0,0,-1,0,-1,0,0]); | |
setBS(2065,[int8(1),2,1,2,1,0,0,0,-1,-1,0,1]); | |
} | |
function EpochUpdate10() internal { | |
setBS(2066,[int8(1),1,1,2,1,1,-1,0,0,-1,-1,1]); | |
setBS(2067,[int8(1),1,2,1,1,1,0,-1,0,-1,0,0]); | |
setBS(2068,[int8(1),1,2,2,1,0,0,-1,0,-1,0,0]); | |
setBS(2069,[int8(1),2,1,2,1,0,0,0,-1,-1,0,1]); | |
setBS(2070,[int8(1),1,1,2,1,1,-1,0,0,-1,0,0]); | |
} | |
function EpochUpdate11() internal { | |
setBS(2071,[int8(1),1,2,1,1,1,0,-1,0,-1,0,0]); | |
setBS(2072,[int8(1),2,1,2,1,0,0,-1,0,-1,0,0]); | |
setBS(2073,[int8(1),2,1,2,1,0,0,0,-1,-1,0,1]); | |
setBS(2074,[int8(1),1,1,2,1,1,0,-1,0,-1,0,0]); | |
setBS(2075,[int8(1),1,2,1,1,1,0,-1,0,-1,0,0]); | |
setBS(2076,[int8(1),2,1,2,1,0,0,0,-1,-1,0,0]); | |
setBS(2077,[int8(1),2,1,2,1,0,0,0,-1,0,-1,1]); | |
} | |
function EpochUpdate12() internal { | |
setBS(2078,[int8(1),1,1,2,1,1,0,-1,0,-1,0,0]); | |
setBS(2079,[int8(1),1,2,1,1,1,0,-1,0,-1,0,0]); | |
setBS(2080,[int8(1),2,1,2,1,0,0,0,-1,-1,0,0]); | |
setBS(2081,[int8(1),1,2,2,1,0,0,0,-1,0,0,0]); | |
setBS(2082,[int8(0),2,1,2,1,0,0,0,-1,0,0,0]); | |
setBS(2083,[int8(1),1,2,1,1,0,0,0,-1,0,0,0]); | |
} | |
function EpochUpdate13() internal { | |
setBS(2084,[int8(1),1,2,1,1,0,0,0,-1,0,0,0]); | |
setBS(2085,[int8(1),2,1,2,0,1,0,0,-1,0,0,0]); | |
setBS(2086,[int8(0),2,1,2,1,0,0,0,-1,0,0,0]); | |
setBS(2087,[int8(1),1,2,1,1,1,0,0,-1,0,0,0]); | |
setBS(2088,[int8(0),1,2,2,0,1,0,0,-1,0,0,0]); | |
setBS(2089,[int8(0),2,1,2,1,0,0,0,-1,0,0,0]); | |
setBS(2090,[int8(0),2,1,2,1,0,0,0,-1,0,0,0]); | |
} | |
} |
Unix timestamp started in 1970, this dapp inits in Baishak 1, 2027 BS, i.e. Jan 1, 1970 + 103 days.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
heyo..
How much back in time, this calendar seem to work?? 🤔