Firetron's FormatDateTime

stable
By Firetron in Miscellaneous Published July 2020 👁 1,669 views 💬 0 comments

Description

Formats a unix datetime number (to "YYYY-MM-DD at HH:MM" by default). Custom Command Dependencies: None Test Script:
if not Load('done', false) then

  local dateDelimiter
  local dateTimeDelimiter
  local timeDelimiter
  local includeSeconds
  local includeTime
  local includeYear

  local unix = Time()

  local formatted = CC_FormatDateTime(unix)

  Log(' ')
  Log(formatted)
  Log('Formatted Default:')
  Log(' ')

  includeSeconds = true
  local formatted1 = CC_FormatDateTime(unix, dateDelimiter, dateTimeDelimiter, timeDelimiter, includeSeconds, includeTime, includeYear)

  Log(formatted1)
  Log('With Seconds:')
  Log(' ')

  includeTime = false
  local formatted2 = CC_FormatDateTime(unix, dateDelimiter, dateTimeDelimiter, timeDelimiter, includeSeconds, includeTime, includeYear)

  Log(formatted2)
  Log('Without Time:')
  Log(' ')

  includeTime = true
  includeYear = false
  local formatted3 = CC_FormatDateTime(unix, dateDelimiter, dateTimeDelimiter, timeDelimiter, includeSeconds, includeTime, includeYear)

  Log(formatted3)
  Log('Without Year:')
  Log(' ')

  dateDelimiter = '/'
  dateTimeDelimiter = ' - '
  timeDelimiter = '.'
  includeSeconds = true
  includeTime = true
  includeYear = true
  local formatted4 = CC_FormatDateTime(unix, dateDelimiter, dateTimeDelimiter, timeDelimiter, includeSeconds, includeTime, includeYear)

  Log(formatted4)
  Log('Formatted Another Way:')
  Log(' ')

  Save('done', true)

end
HaasScript
--  ============================================================================
--    Firetron's FormatDateTime
--
--    Formats a unix datetime number (to "YYYY-MM-DD at HH:MM" by default).
--
--    Custom Command Dependencies:
--    None
--
--    Discord: @FiretronP75
--  ============================================================================

--  ========================================================
--    Variables
--  ========================================================

--  ------------------------------------
--    Definition
--  ------------------------------------

local dDefault
local dDescription
local dName
local dOutput
local dRequired
local dSuggestions
local dType

--  ------------------------------------
--    Function
--  ------------------------------------

local FormatDate
local FormatDateTime
local FormatTime

--  ------------------------------------
--    Parameter
--  ------------------------------------

local pUnix

local pDelimiterDate
local pDelimiterDateTime
local pDelimiterTime

local pIncludeSeconds
local pIncludeTime
local pIncludeYear

--  ========================================================
--    Command Definition
--  ========================================================

dName        = 'FormatDateTime'
dDescription = 'Formats a unix datetime number (to "YYYY-MM-DD at HH:MM" by default).'
DefineCommand(dName, dDescription)

--  ========================================================
--    Parameter Definitions
--  ========================================================

dType        = NumberType
dName        = 'unix'
dDescription = 'The unix datetime number to format.'
dRequired    = false
dDefault     = Time()
dSuggestions = 'Time'
pUnix = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)

--  ----------------

dType        = StringType
dName        = 'dateDelimiter'
dDescription = 'The characters between the year and month, and between the month and day.'
dRequired    = false
dDefault     = '-'
dSuggestions = 'Text'
pDelimiterDate = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)

dType        = StringType
dName        = 'dateTimeDelimiter'
dDescription = 'The characters between the date and time.'
dRequired    = false
dDefault     = ' at '
dSuggestions = 'Text'
pDelimiterDateTime = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)

dType        = StringType
dName        = 'timeDelimiter'
dDescription = 'The characters between the hour and minute, and between the minute and second.'
dRequired    = false
dDefault     = ':'
dSuggestions = 'Text'
pDelimiterTime = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)

--  ----------------

dType        = BooleanType
dName        = 'includeSeconds'
dDescription = 'If true, the seconds will be included in the formatted time.'
dRequired    = false
dDefault     = false
dSuggestions = 'True, False'
pIncludeSeconds = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)

dType        = BooleanType
dName        = 'includeTime'
dDescription = 'If true, the formatted time will be included.'
dRequired    = false
dDefault     = true
dSuggestions = 'True, False'
pIncludeTime = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)

dType        = BooleanType
dName        = 'includeYear'
dDescription = 'If true, the year will be included in the formatted date.'
dRequired    = false
dDefault     = true
dSuggestions = 'True, False'
pIncludeYear = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)

--  ========================================================
--    Functions
--  ========================================================

FormatDateTime = function ()

  local date = FormatDate()

  if not pIncludeTime then return date end

  local time = FormatTime()

  return date..pDelimiterDateTime..time

end

FormatDate = function ()

  local date  = CurrentDate( pUnix)
  local month = CurrentMonth(pUnix)

  if date  < 10 then date  = '0'..date  end
  if month < 10 then month = '0'..month end

  if not pIncludeYear then return month..pDelimiterDate..date end

  local year = CurrentYear(pUnix)

  return year..pDelimiterDate..month..pDelimiterDate..date

end

FormatTime = function ()

  local hour   = CurrentHour(  pUnix)
  local minute = CurrentMinute(pUnix)

  if hour   < 10 then hour   = '0'..hour   end
  if minute < 10 then minute = '0'..minute end

  if not pIncludeSeconds then return hour..pDelimiterTime..minute end

  local second = CurrentSecond(pUnix)

  if second < 10 then second = '0'..second end

  return hour..pDelimiterTime..minute..pDelimiterTime..second

end

--  ========================================================
--    Output Definition
--  ========================================================

dType        = StringType
dOutput      = FormatDateTime()
dDescription = 'The formatted unix datetime number.'
dSuggestions = 'Memory'
DefineOutput(dType, dOutput, dDescription, dSuggestions)

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!