Jump to content
UBot Underground

Recommended Posts

This might be useful for many people - the idea came from a real request by uniquebot on this thread here, as a matter of fact.
 
Here is a Custom Function, called $TimeStill that would return various bits of info extracted from a string of the same format that $date natively returns (or from that function directly) and alternatively, returns the specified bits even w/o a defined parameter passed, based on current $date output from the System.
 
The following code starts with a loop that only showcases the various types of inputs and the results the $TimeStill custom function will return.
 
Basically, you can call it, specifying what you want to extract from the time/date string, separate bits such as:

  • year
  • month
  • day
  • hour
  • minute
  • seconds

or even a list of them all, when

  • list is the specified type,

or when

  • none of the above is specified (by default)

Also, the $TimeStill custom command/function will use $date return string, if no time string input is passed to it.
 
In extremis, if you call the function with both parameters empty, it will return a list of the current $date output elements (bits of info) such as year, month, etc... in the exact order the $date function gives them: 
 

MM/DD/YYYY HH:MM:SS

 
Here is the code:
 

loop(1) {
    set(#TIME, $date, "Global")
    set(#TIME_YEAR, $TimeStill(#TIME, "Year"), "Global")
    set(#TIME_MONTH, $TimeStill(#TIME, "monTH"), "Global")
    set(#TIME_DAY, $TimeStill(#TIME, " daY "), "Global")
    set(#TIME_HOUR, $TimeStill(#TIME, "hour "), "Global")
    set(#TIME_MIN, $TimeStill(#TIME, " Minute"), "Global")
    set(#TIME_SEC, $TimeStill(#TIME, "second"), "Global")
    set(#TIME_LIST, $TimeStill(#TIME, "LIST"), "Global")
    set(#TIME_NOType, $TimeStill(#TIME, ""), "Global")
    set(#TIME_NOTimeList, $TimeStill("", "list"), "Global")
    set(#TIME_NOTimeYear, $TimeStill("", "year"), "Global")
    set(#TIME_NOTimeNoType, $TimeStill("", ""), "Global")
}
divider
define $TimeStill(#var_INP_DATE, #var_INP_OUTPUT) {
    set(#var_LOC_DATE, $trim(#var_INP_DATE), "Global")
    if($comparison($text length(#var_LOC_DATE), "=", 0)) {
        then {
            set(#var_LOC_DATE, $date, "Global")
        }
        else {
            set(#var_LOC_DATE, $change text casing(#var_LOC_DATE, "Lower Case"), "Global")
        }
    }
    set(#var_LOC_OUTPUT_Type, $trim(#var_INP_OUTPUT), "Global")
    divider
    if($comparison($text length(#var_LOC_OUTPUT_Type), "=", 0)) {
        then {
            set(#var_LOC_OUTPUT_Type, "list", "Global")
        }
        else {
            set(#var_LOC_OUTPUT_Type, $change text casing($trim(#var_INP_OUTPUT), "Lower Case"), "Global")
        }
    }
    set(#var_LOC_DATE_LST, $replace regular expression(#var_LOC_DATE, "[^\\d]", $new line), "Global")
    clear list(%lst_LOC_TimeStill)
    add list to list(%lst_LOC_TimeStill, $list from text(#var_LOC_DATE_LST, $new line), "Don\'t Delete", "Global")
    divider
    if($contains(#var_LOC_OUTPUT_Type, "year")) {
        then {
            set(#var_OUT_TimeStill, $list item(%lst_LOC_TimeStill, 2), "Global")
        }
        else if($contains(#var_LOC_OUTPUT_Type, "month")) {
            set(#var_OUT_TimeStill, $list item(%lst_LOC_TimeStill, 0), "Global")
        }
        else if($contains(#var_LOC_OUTPUT_Type, "day")) {
            set(#var_OUT_TimeStill, $list item(%lst_LOC_TimeStill, 1), "Global")
        }
        else if($contains(#var_LOC_OUTPUT_Type, "hour")) {
            set(#var_OUT_TimeStill, $list item(%lst_LOC_TimeStill, 3), "Global")
        }
        else if($contains(#var_LOC_OUTPUT_Type, "min")) {
            set(#var_OUT_TimeStill, $list item(%lst_LOC_TimeStill, 4), "Global")
        }
        else if($contains(#var_LOC_OUTPUT_Type, "sec")) {
            set(#var_OUT_TimeStill, $list item(%lst_LOC_TimeStill, 5), "Global")
        }
        else if($contains(#var_LOC_OUTPUT_Type, "list")) {
            set(#var_OUT_TimeStill, #var_LOC_DATE_LST, "Global")
        }
    }
    divider
    return(#var_OUT_TimeStill)
}


and the .ubot file for download:
 
TIME-TimeStill-Return-Date-Bits-Function.ubot
 
Enjoy!
 
 
P.S.

 

For reasons of testing, so that you can see the variable values in the DEBUGGER, the code shows the define function's variables set as "Global".
 
However, in a working environment, these should all be set to "Local" in my opinion, so keep in mind that I would personally switch so, but feel free to do whatever you like, of course - the code will still work, but in multithreading environments might be prone to errors.
 
Caveat:
Also, if you want to use it repeatedly to extract each piece of data in a loop or something, there is a slight danger of errors if you would be using the $date for each function call repeatedly (as by the nature of the beast which is time, the values might change from the beginning of the loop till the end)
 
Imagine you start by returning the YEAR of the current date with it, exactly at 12/31/2013 23:59:59 and while you're looping, the year passes by and you will then get the corresponding results derived from the next day, of the next month of the next year... etc... I think you got the point.
 
In such a case, to make sure you won't ever be faced with such disastrous 'time-shifts' LOL ... you should pin down the starting date string by adding it to a time variable that calls $date only once and then use the $TimeStill command only with that variable as input parameter in the loop.

  • Like 3
Link to post
Share on other sites

hey VB i did the same thing 3 weeks ago called date tutorial

 

not as elegant as your i must say but function the same

 

good post

 

i wont put the link in don't want to trash ya thread

 

pftg4

  • Like 1
Link to post
Share on other sites

Sorry mate, haven't seen that... but as a proof of honesty, please do post the link to your thread too. 

 

I'd be happy to read it.  Thanks!

  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...