Nominal to Date e.g.'01:40PM' to 13:40

artdijk
artdijk New Altair Community Member
edited November 5 in Community Q&A
LS
How can I convert a set of nominal fields that are meant to be date/time field to time.
e.g. I have 01:40PM and this needs to be 13:40
e.g. 08:20AM and this needs to be 08:20
How can this be done ?
What processes should I use and with what kind of settings ?
Thanks
Arthur
Tagged:

Answers

  • steffen
    steffen New Altair Community Member
    Hello Arthur

    you can use the operator "Nominal to Date". I suggest to read the extensive documentation of this operator available in the "help"-tab.

    hope this was helpful,

    steffen
  • artdijk
    artdijk New Altair Community Member
    Hi,
    But I tried to use nominal to date. This process did not include the AM or PM in the conversion. All AM times were converted correctly but the PM were 12 hours wrong.
    Any suggestion on how to account for PM ?
    Thanks
    Arthur
  • steffen
    steffen New Altair Community Member
    Hello Arthur

    Here is an example. Feel free to ignore all operators except "Nominal to Date", the others are for creating the demonstration data.

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <process version="5.0">
     <context>
       <input/>
       <output/>
       <macros/>
     </context>
     <operator activated="true" class="process" expanded="true" name="Process">
       <process expanded="true" height="431" width="547">
         <operator activated="true" class="retrieve" expanded="true" height="60" name="Retrieve" width="90" x="31" y="65">
           <parameter key="repository_entry" value="//Samples/data/Golf"/>
         </operator>
         <operator activated="true" class="generate_empty_attribute" expanded="true" height="76" name="create_time" width="90" x="45" y="210">
           <parameter key="name" value="time"/>
           <parameter key="value_type" value="polynominal"/>
         </operator>
         <operator activated="true" class="execute_script" expanded="true" height="76" name="set_time_values" width="90" x="179" y="210">
           <parameter key="script" value="ExampleSet exampleSet = operator.getInput(ExampleSet.class);&#10;&#13;int counter = 1;&#13;&#10;for (Example example : exampleSet) {&#13;&#13;&#10;&#9;if(Math.pow(-1,counter) == 1){&#10;       example[&quot;time&quot;] = &quot;08:10AM&quot;;&#13;&#13;&#10;  }else{&#13;&#10;  &#9;&#13;   example[&quot;time&quot;] = &quot;08:10PM&quot;;&#13;&#10;  }&#10;   counter++;&#10;}&#10;return exampleSet;"/>
         </operator>
         <operator activated="true" class="generate_copy" expanded="true" height="76" name="Generate Copy" width="90" x="313" y="210">
           <parameter key="attribute_name" value="time"/>
           <parameter key="new_name" value="transformed_time"/>
         </operator>
         <operator activated="true" class="nominal_to_date" expanded="true" height="76" name="Nominal to Date" width="90" x="447" y="75">
           <parameter key="attribute_name" value="transformed_time"/>
           <parameter key="date_type" value="time"/>
           <parameter key="date_format" value="h:mma"/>
         </operator>
         <connect from_op="Retrieve" from_port="output" to_op="create_time" to_port="example set input"/>
         <connect from_op="create_time" from_port="example set output" to_op="set_time_values" to_port="input 1"/>
         <connect from_op="set_time_values" from_port="output 1" to_op="Generate Copy" to_port="example set input"/>
         <connect from_op="Generate Copy" from_port="example set output" to_op="Nominal to Date" to_port="example set input"/>
         <connect from_op="Nominal to Date" from_port="example set output" to_port="result 1"/>
         <portSpacing port="source_input 1" spacing="0"/>
         <portSpacing port="sink_result 1" spacing="0"/>
         <portSpacing port="sink_result 2" spacing="0"/>
       </process>
     </operator>
    </process>
    Please reread the documentation of this operator available in help tab. In one line it says:

    a: am/pm marker; Text; example: PM
    The "trick" is to construct a parsing statement instead of using only those available.

    hope this was helpful,

    steffen