Using systemfunc and Unix date utility: problems with compound strings

From: Jonathan Vigh <vigh_at_nyahnyahspammersnyahnyah>
Date: Wed, 08 Mar 2006 21:57:37 -0700

    I'm trying to use systemfunc with the Unix utility 'date' to return
a formatted date (seconds since 1970) from a compound string that
contains the date. The string has the form: "2/26/2006 9:14:37 AM". At
the command line, the date command is invoked:

prompt> date -d "2/26/2006 9:14:37 AM" +%s

which returns:
as desired.

I would like to get NCL's systemfunc to do the same thing using the
string "2/26/2006 9:14:37 AM". Because the string is compound, Unix date
interprets the second part 9:14:37 as the format specifier. I can get
around this by putting the compound string in single quotes:

in NCL> time = systemfunc("date -d '2/26/2006 9:14:37 AM' '+%s' ")

This works, but if I instead use an NCL-concatenated variable
representation of the same string, say Sdatetime:

in NCL> Sdatetime = "2/26/2006 9:14:37 AM"

and then use systemfunc:

in NCL> time = systemfunc("date -d " + Stimedate + " '+%s'")

date complains that it has too many non-option arguments -- it is only
seeing the first part of the string that is passed to it. Putting
Sdatetime in single quotes doesn't work, because then NCL doesn't pass
the string variable's value to date.

I'm guessing that I need to somehow put the double or single quotation
marks into my string before it gets passed to systemfunc, or do some
sort of escape sequence in the systemfunc command itself. I'm a bit
confused as to how to do this. Any help is appreciated.
An example program is attached to easily replicate this issue.


Jonathan Vigh, Ph.D. Candidate              work phone: 970.491.8988                         
Department of Atmospheric Science 
Colorado State University

load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"


Sdate = "2/26/2006"
Stime = "9:14:37"
SAMorPM = "AM"

Stimedate = Sdate+" "+Stime +" "+SAMorPM ; a combined string of date and time


time = systemfunc("date -d '2/26/2006 9:14:37 AM' '+%s' ") ; attempt without using the string representation
print(time) ; produces output: (0) 1140970477
time = systemfunc("date -d " + Stimedate + " '+%s'") ; attempt using the compound string


ncl-talk mailing list
Received on Wed Mar 08 2006 - 21:57:37 MST

This archive was generated by hypermail 2.2.0 : Thu Mar 09 2006 - 08:06:36 MST