Go to content Go to navigation

Inc and Dec Time

16 August 2010, 14:28 by Chad Reynoldson

Here is an easy to implement NetLinx function that will increment and decrement a time variable by a +/- seconds offset. It limits you to 24 hours and is pretty self-explanatory.

Hope you can find value in it and remember how easy this is in AMX!

(*---------------------------------------------------------*)
(* Inc/dec cTime by x number of seconds.                 --*)
(*---------------------------------------------------------*)
DEFINE_FUNCTION SLONG getTimeOffset (CHAR cTime[8], SLONG slOffsetSec)
STACK_VAR
  SLONG slHr
  SLONG slMin
  SLONG slSec
  SLONG slTotalSec
  CHAR  cTimeOffset[8]
{
(*-- Offset range limited to 24 hours --*)
  IF((slOffsetSec > 86400) || (slOffsetSec < -86400))
    RETURN(-1)

(*-- Get seconds total --*)
  slHr  = MAX_VALUE(0, TIME_TO_HOUR  (cTime))
  slMin = MAX_VALUE(0, TIME_TO_MINUTE(cTime))
  slSec = MAX_VALUE(0, TIME_TO_SECOND(cTime))

  slTotalSec = (slHr * 3600) + (slMin * 60) + slSec

  slTotalSec = slTotalSec + slOffsetSec

(*-- Need the time --*)
  slHr  = (slTotalSec / 60) / 60
  slMin = (slTotalSec -  (slHr*60*60)) / 60
  slSec =  slTotalSec - ((slHr*60*60) + (slMin*60))

  cTimeOffset = "RIGHT_STRING("'00',ITOA(slHr )",2),':',
                 RIGHT_STRING("'00',ITOA(slMin)",2),':',
                 RIGHT_STRING("'00',ITOA(slSec)",2)"

(*-- Only passback what they sent in (i.e. HH:MM:SS or HH:MM) --*)
  SET_LENGTH_STRING(cTimeOffset, LENGTH_STRING(cTime))

(*-- Passback --*)
  cTime = cTimeOffset
  RETURN(1)
}


Here’s a simple implementation of it:

BUTTON_EVENT[dvPnls1a,1] // Up
BUTTON_EVENT[dvPnls1a,2] // Down
{
  PUSH :
  {
    SWITCH(BUTTON.INPUT.CHANNEL)
    {
      CASE 1 : getTimeOffset (cThisTime, 900)   // Inc by 15 minutes
      CASE 2 : getTimeOffset (cThisTime,-900)   // Dec by 15 minutes
    }

    SEND_COMMAND dvPnls1a,"'TEXT1-',cThisTime"
  }
}

Share This Article

Comments

No Current Comments

Write a comment.

Please note…

  1. Please keep comments to a thoughtful discussion on the topic.
  2. If you just want to say hello, use the contact form instead.
  3. Textile is limited to *strong*, _italics_ and "link":url .
  4. I reserve the right to edit/remove any comments.