DateNow
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RozaniGhani-RG

//@version=5

// @description TODO: Provide today's date based on UNIX time
library('DateNow', overlay = true)

// ————————————————————————————————————————————————————————————————————————————— DateNow {
// @function : DateNow
// @param    : _timezone
// @returns  : YYYY, YY, M, MM, MMM, DD
export dateNow(string _timezone = 'GMT+8') =>
    dt   = timenow
    Y1   = year(      dt)
    M1   = month(     dt)
    D1   = dayofmonth(dt)
    Y2   = year(timestamp(      _timezone, 1970, 1, 1, 0, 0, 0))
    M2   = month(timestamp(     _timezone, 1970, 1, 1, 0, 0, 0))
    D2   = dayofmonth(timestamp(_timezone, 1970, 1, 1, 0, 0, 0))
    YY   = Y1 - Y2 - 31
    YYYY = Y1 - Y2 - 31 + 2000
    LEAP = Y1 %  4 ? 31 : 30
    M    = M1 - M2 + 12
    [MM, MMM] = switch M
        1  => ['JAN',   'JANUARY']
        2  => ['FEB',  'FEBRUARY']
        3  => ['MAR',     'MARCH']
        4  => ['APR',     'APRIL']
        5  => ['MAY',       'MAY']
        6  => ['JUN',      'JUNE']
        7  => ['JUL',      'JULY']
        8  => ['AUG',    'AUGUST']
        9  => ['SEP', 'SEPTEMBER']
        10 => ['OCT',   'OCTOBER']
        11 => ['NOV',  'NOVEMBER']
        12 => ['DEC',  'DECEMBER']
    DD   = YYYY <= 1984 ? D1 - D2 + LEAP + 1 : D1 - D2 + LEAP
    [YYYY, YY, M, MM, MMM, DD]
// }
// ————————————————————————————————————————————————————————————————————————————— Example {
[YYYY, YY, M, MM, MMM, DD] = dateNow()
var dateTable = table.new(position.middle_center, 2, 4, border_width = 1)
if barstate.islast
    table.cell(dateTable, 0, 0,                                                              'FORMAT', bgcolor = color.white)
    table.cell(dateTable, 0, 1,                                                          'DD MM YYYY', bgcolor = color.white)
    table.cell(dateTable, 0, 2,                                                         'DD MMM YYYY', bgcolor = color.white)
    table.cell(dateTable, 0, 3,                                                             'DD/M/YY', bgcolor = color.white)
    
    table.cell(dateTable, 1, 0,                                                              'OUTPUT', bgcolor = color.white)
    table.cell(dateTable, 1, 1, str.tostring(DD) + ' ' + str.tostring(MM)  + ' ' + str.tostring(YYYY), bgcolor = color.white)
    table.cell(dateTable, 1, 2, str.tostring(DD) + ' ' + str.tostring(MMM) + ' ' + str.tostring(YYYY), bgcolor = color.white)
    table.cell(dateTable, 1, 3, str.tostring(DD) + '/' + str.tostring(M)   + '/' + str.tostring(YY),   bgcolor = color.white)
// }