1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Quoting arguments to a user defined function

Discussion in 'Support' started by p.f.moore, Jul 5, 2008.

  1. p.f.moore

    Joined:
    May 30, 2008
    Messages:
    122
    Likes Received:
    1
    I'm having trouble with levels of quoting. What I'm trying to do is
    write a function which returns the output of the Gnu date command.
    Annoyingly, the command takes a format string parameter which makes
    heavy use of % characters. So I need to quote the argument:


    05/07/2008

    If I try to make this into a user defined function:

    function fmtdate=`%@EXECSTR[C:\Utils\Gnuwin32\date +^`%$^`]`

    it does not work as expected:


    fmtdate=%@EXECSTR[C:\Utils\Gnuwin32\bin\date +`%$`]


    ECHO is OFF

    I don't know of a good way to debug this, but it looks to me like the
    quoting is wrong somehow. Can anybody offer me any suggestions (either
    on how to see what's going on, or on how to fix the quoting up so that
    I can call the function in a reasonably natural way)?

    (Actually, if someone can point me to a built in function that will
    allow me to format the current date *and time* in an arbitrary way,
    rather than just the fixed formats described in "Date Display Formats"
    that would do me fine, as well!)

    Paul.
     
  2. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,730
    Likes Received:
    80
    p.f.moore wrote:

    The back quotes are processed and removed in the @fmtdate function; they
    aren't passed through to be processed by the next function or command.

    The easiest workaround would be to use SETDOS /X to prevent processing
    of the % characters in your function (i.e., SETDOS /X-3 to turn it off
    and SETDOS /X+3 to reenable it).

    Rex Conn
    JP Software
     
  3. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    p.f.moore wrote:
    | (Actually, if someone can point me to a built in function that will
    | allow me to format the current date *and time* in an arbitrary way,
    | rather than just the fixed formats described in "Date Display Formats"
    | that would do me fine, as well!)

    Look at the topic "variablecats.htm" - Variables by categories - in the
    section on date and time variables:

    _year _month _day _hour _minute _second

    which give you each field (also _monthf, _imonth, _imonthf if month names
    are relevant). From these you can define your own function(s) to combine
    them in any manner you want. I have no doubt the UDF will be executed faster
    than calling an external program, and capturing its output via @execstr[].

    WARNING! When you execute any such function just before a higher unit
    changes (e.g., near the hour), it is possible that you would get
    inconsistency - some fields retrieved before the change, some after the
    change. For example, if you are near midnight on New Year's eve, and you
    retrieve time elements before, date elements after midnight, you can get
    2009-01-01 23:59:59. A safer method is to use one of the variables _datetime
    / _isodatetime / _utcdatetime and use @instr to locate the subfields from
    the yyyyMMddhhmmss format.
    --
    HTH, Steve
     
  4. p.f.moore

    Joined:
    May 30, 2008
    Messages:
    122
    Likes Received:
    1
    2008/7/5 Steve Fábián <>:

    Yes, I was aware of these. Generally, I find messing round with manual
    formatting is a real nuisance, which is why I prefer approaches which
    let me use format codes. (For example, the month as I want it is
    %@FORMAT[02,%_MONTH] to get leading zeroes - add a few of those
    together and you very rapidly exceed any sane line length.

    I could use ^ for line continuations, but the intent of the code still
    gets obscured by the formatting...

    Paul.
     
  5. p.f.moore

    Joined:
    May 30, 2008
    Messages:
    122
    Likes Received:
    1
    2008/7/5 rconn <>:

    Aha! Something like

    function fmtdate=`%@EXECSTR[setdos /X-3 & C:\Utils\Gnuwin32\bin\date
    +%$ & setdos /X+3]`
    echo %@fmtdate[`%d/%m/%Y`]

    seems to work.

    I'd never thought of using SETDOS like this before - its effects on
    parsing seemed to imply to me that it had to be used by the caller,
    not the callee, which is almost never what I want.

    Thanks for the hint.
    Paul.
    Thanks,
    Paul.
     

Share This Page