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

//@version=5

// @description TODO: add library description here
library("LibraryCheckNthBar")

// @function this function can be used if current bar is in last Nth bar
// @param UTC is UTC of the chart
// @param prd is the length of last Nth bar
// @returns true if the current bar is in N bar
export canwestart(float UTC, int prd) =>
    // get GMT time
    int timenow_ = timenow + math.round(UTC * 3600000)
    int time_ = time + math.round(UTC * 3600000)

    // calculate D/H/M/S for the bar which we are on and time now
    int daynow_ = math.floor((timenow_ + 86400000 * 3) / 86400000) % 7
    int day_ = math.floor((time_ + 86400000 * 3) / 86400000) % 7
    int hournow_ = math.floor(timenow_ / 3600000) % 24
    int hour_ = math.floor(time_ / 3600000) % 24
    int minutenow_ = math.floor(timenow_ / 60000) % 60
    int minute_ = math.floor(time_ / 60000) % 60
    int secondnow_ = math.floor(timenow_ / 60000) % 60
    int second_ = math.floor(time_ / 60000) % 60
    int hmstime_ = hour_ * 3600000 + minute_ * 60000 + second_ * 1000
    int hmstimenow_ = hournow_ * 3600000 + minutenow_ * 60000 + secondnow_ * 1000

    var int starttime = na
    if na(starttime) or time_ < nz(starttime)  // bar_index > prd and 
        if timeframe.isdaily
            starttime := timenow_ - timeframe.multiplier * prd * 86400000  // 1 day = 86400000
            starttime
            //if day_ == daynow_ // session day
            //    diff = time_ - time_[prd]
            //    starttime := timenow_ - diff
            //if daynow_ > day_ and daynow_ > day_[1] and day_[1] > day_ // weekend
            //    diff = time_[1] - time_[prd + 1] + (daynow_ - day_[1]) * 86400000
            //    starttime := timenow_ - diff

        if timeframe.isintraday
            if day_ == daynow_ and day_[1] == daynow_ and hmstime_ > hmstimenow_ and hmstime_[1] <= hmstimenow_  // in session
                diff = time_[1] - time_[prd + 1] + math.round(UTC * 3600000)
                starttime := timenow_ - diff
                starttime
            if day_ != daynow_ and day_[1] == daynow_ and hmstime_ < hmstimenow_ and hmstime_[1] <= hmstimenow_  // in same day and after session
                diff = time_[1] - time_[prd + 1] + hmstimenow_ - hmstime_[1] + math.round(UTC * 3600000)
                starttime := timenow_ - diff
                starttime
            if day_ == daynow_ and day_[1] + 1 == daynow_ and hmstime_ > hmstimenow_ and hmstime_[1] >= hmstimenow_  // in week (not first day) and before session
                diff = time_[1] - time_[prd + 1] + 24 * 3600000 - hmstime_[1] + hmstimenow_ + math.round(UTC * 3600000)
                starttime := timenow_ - diff
                starttime
            if day_ == daynow_ and day_[1] > daynow_ and hmstime_ > hmstimenow_ and hmstime_[1] >= hmstimenow_  // the day after weekend before session
                diff = time_[1] - time_[prd + 1] + 2 * 86400000 + 24 * 3600000 - hmstime_[1] + hmstimenow_ + math.round(UTC * 3600000)
                starttime := timenow_ - diff
                starttime
            if daynow_ > day_ and daynow_ > day_[1] and day_[1] > day_  // weekend
                diff = time_[1] - time_[prd + 1] + (daynow_ - day_[1] - 1) * 86400000 + 24 * 3600000 - hmstime_[1] + hmstimenow_ + math.round(UTC * 3600000)
                starttime := timenow_ - diff
                starttime

        if timeframe.isweekly
            starttime := timenow_ - timeframe.multiplier * prd * 604800000  // 1 week = 604800000ms
            starttime

        if timeframe.ismonthly
            starttime := timenow_ - timeframe.multiplier * prd * 2629743000  // 1 month = 2629743000ms
            starttime

    ret_ = not na(starttime) and time >= starttime
    ret_