github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/tslib/todate.go (about)

     1  package tslib
     2  
     3  import (
     4  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
     5  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/rpc"
     6  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/utils"
     7  	"github.com/bykof/gostradamus"
     8  )
     9  
    10  // FromBnToDate returns a chain-specific date given a block number.
    11  func FromBnToDate(chain string, bn base.Blknum) (gostradamus.DateTime, error) {
    12  	ts, err := FromBnToTs(chain, bn)
    13  	if err != nil {
    14  		dt, _ := FromTsToDate(utils.EarliestEvmTs)
    15  		return dt, err
    16  	}
    17  	return FromTsToDate(ts)
    18  }
    19  
    20  // FromNameToDate returns a chain-specific date (if found) given its chain-specific name
    21  func FromNameToDate(chain, name string) (gostradamus.DateTime, error) {
    22  	specials, _ := GetSpecials(chain)
    23  	if len(specials) == 0 || name == "0" {
    24  		return FromBnToDate(chain, 0)
    25  	}
    26  
    27  	if name == "latest" {
    28  		conn := rpc.TempConnection(chain)
    29  		meta, _ := conn.GetMetaData(false)
    30  		ts := conn.GetBlockTimestamp(meta.Latest)
    31  		return FromTsToDate(ts)
    32  	}
    33  
    34  	for _, value := range specials {
    35  		if value.Name == name {
    36  			return FromBnToDate(chain, value.BlockNumber)
    37  		}
    38  	}
    39  
    40  	// default to first
    41  	return FromBnToDate(chain, specials[0].BlockNumber)
    42  }
    43  
    44  // FromTsToDate returns a date given a Linux timestamp (not chain-specific)
    45  func FromTsToDate(ts base.Timestamp) (gostradamus.DateTime, error) {
    46  	return gostradamus.FromUnixTimestamp(ts.Int64()), nil
    47  }