FrizLabz_Time_Utility_Methods
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © FFriZz

//@version=5
library("FrizLabz_Time_Utility_Methods")


// @description [FrizLabz] Time & Utility - Methods some simple methods for time to bar_index conversion and vice versa
// also a Line Label Box function 

// @function UTC helper function this adds the + to the positive utc times, add "UTC" to the string
// and can be used in the timezone arg of for format_time() 
// @param utc (int) +/- utc offset
// @returns string string to be added to the timezone paramater for utc timezone usage
export UTC_helper(int utc=-6) =>
    //TODO : Exapand on this and make it cover all timezone options
	out = ''
	if utc >= 0
		out := 'UTC+'+str.tostring(utc)
	if utc <= 0
		out := 'UTC'+str.tostring(utc)
	out

// @function from a time to index
// @param bar_amount (int) default - 1)
// @returns int bar_time
export method bar_time(int bar_amount = 1)=> 
    bar_time = (time_close - time) * bar_amount
    bar_time

// @function from time to bar_index 
// @param _time (int) 
// @returns int time_to_index bar_index that corresponds to time provided
export method time_to_index(int _time) =>
    switch 
        _time < 1000000 => runtime.error("_time is not Unix time\nFor index to time use method index_to_time()")
    time_diff_index = (time-_time)/bar_time()
    time_to_index   = bar_index - time_diff_index
    time_to_index 

// @function from a time quanity to bar quanity for use with [historical ref]. 
// @param _time (int) 
// @returns int bars_back yeilds the amount of bars from current bar to reach _time provided
export method time_to_bars_back(int _time) =>
    timediff  = time - _time
    bars_back = timediff/bar_time()
    bars_back

// @function from bars_back to time
// @param _time (int) 
// @returns int using same logic as [historical ref.] this will return the 
//          time of the bar = to the bar that corresponds to [x] bars_back
export method bars_back_to_time(int bars_back) => time - bar_time(bars_back)

// @function bar_index to UNIX time
// @param index (int) 
// @returns int time time in unix that corrresponds to the bar_index  
export method index_to_time(int index) =>
    switch 
        index > 1000000 => runtime.error("index = Unix time [detected]\nFor time to index use method time_to_index()")
    index_diff    = bar_index - index
    time_of_index = index_diff.bars_back_to_time() 
    time_of_index

// @function method to use with a time or bar_index variable that will detect if it is an index or unix time
// and convert it to a printable string
// @param time_or_index (int) required) time in unix or bar_index
// @param timezone (int) required) utc offset to be appled to output
// @param format (string) default - "yyyy-MM-dd'T'HH:mm:ssZ") the format for the time, provided string is default
// one from str.format_time()
// @returns string time formatted string
export method to_utc(int time_or_index,int timezone=-6,string format = na) =>
    
    _format = na(format) ? "yyyy-MM-dd'T'HH:mm:ssZ" : format
    check = time_or_index
    if time_or_index < 1000000
        check := time_or_index.index_to_time()

    out = str.format_time(check,format,UTC_helper(timezone))





















