text: I am trying to figure out to convert a timestamp (using the getTimestamp) command to a string value with the format of YYYYMMDD_HHMMSS. This seems like a simple task but the only way I have found to do it thus far was by using IF statements and that seems very inefficient. Is there an easier way?
  Answer by Natasha DSilva (634) | Sep 20, 2018 at 12:53 PM
  Hi Jim, I looked in the documentation in the spl.time namespace and the only function for converting a timestamp to string is ctime][1], but it has a fixed format.
  To match the format you're looking for, I used the time function which separates a timestamp into its components and then built a string out of that:
  //Convert an integer to a string and pad with 0 if needed rstring pad (int32 val) { //val = val +1; if (val < 10) { return "0" + (rstring)val; } return (rstring)val; } composite printTime { graph () as sink = Custom() { logic onProcess: { timestamp curTime = getTimestamp(); mutable Sys.tm asTuple = {}; time(curTime, asTuple); //split into a tuple rstring yearString = (rstring)(asTuple.year + 1900); printStringLn( yearString + pad(asTuple.mon+1) + pad(asTuple.mday) + "_" + pad(asTuple.hour) + pad(asTuple.min) + pad(asTuple.sec )); } } }
  [1]:
  Answer by Rene Kroell (213) | Sep 22, 2018 at 06:56 AM
  Hi Jim,
  to convert a timestamp into a custom string format I normally use the following two functions depending if I need gmt or local time. So in your case the call getGmtString(getTimestamp(), "%Y%m%d_%H%M%S") or getLocalString(getTimestamp(), "%Y%m%d_%H%M%S") should work.
  For details about the format string see the documentation of the strftime function.
  From a performance point of view it may not be very efficient but you can convert to each format you like.
  /** * This function converts the given timestamp into a gmt string. * * @param timeStamp The timestamp to convert into string. * @param format The strftime like format string for the conversion. * * @return The timestamp converted into gmt string. */ public rstring getGmtString(timestamp timeStamp, rstring format) { mutable Sys.tm tm = {}; gmtime(timeStamp, tm); return strftime(tm, format); } /** * This function converts the given timestamp into a local string. * * @param timeStamp The timestamp to convert into string. * @param format The strftime like format string for the conversion. * * @return The timestamp converted into local string. */ public rstring getLocalString(timestamp timeStamp, rstring format) { mutable Sys.tm tm = {}; time(timeStamp, tm); return strftime(tm, format); }
  @Rene Kroell Thank you for sharing, this is actually a cleaner solution that I was not aware of!
  Answer by Jim@CHOP (32) | Sep 21, 2018 at 12:46 PM
  Thanks @Natasha DSilva
  147 people are following this question.
  How to parse tuple rstring using Parse operator ? (also conversion to blob) 1 Answer
  Is there a tool to convert an XSD into an SPL Schema? 1 Answer
  how using findLast for get a string 1 Answer
  how to convert string to timestamp in infosphere stream ? 3 Answers
  How do I convert from hex string (0x3f9d70a4) to float (1.23) in SPL? 0 Answers