Adding Two Hours to ODK Time

14 views
Skip to first unread message

Kayin Scholtz

unread,
Mar 25, 2019, 10:37:47 AM3/25/19
to OpenFn Community
Hi All,

We've recently noticed that all the data we're collecting with the "time" field in ODK is giving us back the information in GMT. Being in South Africa this means our time data is always recorded in Salesforce two hours earlier than what our data collectors entered. I'm looking to write a function to add two hours to entered value, but I'm struggling to  get it to work. Here's the basis of the code as is:

each(
    dataPath("data[*]"),
    combine(
      upsert("Reporting__c","Unique_ID__c", fields(
      field("Unique_ID__c", dataValue("instanceID")),
      field("Time_In__c", dataValue("time_in")),
      field("Time_Out__c", dataValue("time_out"))
    ))
  )
)

Taylor Downs

unread,
Mar 25, 2019, 11:42:30 AM3/25/19
to Kayin Scholtz, OpenFn Community
I can definitely help with this! Can you share some `state.data` so I can see what format `time_in` is initially?

You'll likely end up using the Date global in JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

--
You received this message because you are subscribed to the Google Groups "OpenFn Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openfn+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/openfn/7af7d738-1498-45dc-9ce0-025ce227beec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kayin Scholtz

unread,
Mar 27, 2019, 6:14:11 AM3/27/19
to Taylor Downs, OpenFn Community
Hi Taylor, 

Sorry for the delay, our time data comes into Open Fn in this format:

Time_In__c: '06:00:00.000Z',
Time_Out__c: '11:45:00.000Z',


Best,

Kayin

Taylor Downs

unread,
Mar 28, 2019, 6:47:32 AM3/28/19
to Kayin Scholtz, OpenFn Community
Ah, ok—there's no date. What's the desired output? (Is that a datetime field in some other system, and are you building that with a date from another input field?)
--
Taylor Downs | Founder & Core Contributor
Open Function | Integrated Systems for Development

Kayin Scholtz

unread,
Mar 28, 2019, 7:10:34 AM3/28/19
to Taylor Downs, OpenFn Community
Yes, sorry, the format is the output of the ODK Time field, so it just has time and time zone.

The data is being input into the new Salesforce "time" field with no date. The formats match fine except that the time zone gets confused.

Taylor Downs

unread,
Mar 29, 2019, 8:56:20 AM3/29/19
to Kayin Scholtz, OpenFn Community
OK, Javascipt isn't good with times (no date) and I'm hoping someone comes along with a more elegant solution, but how about this:

field('sf_time__c', function(state) => {
  const timeArray = state.data.odk_time.split(':');
  var hour = parseInt(timeArray[0]);
  if (hour < 21) {
    hour = hour + 2
  } else {
    hour = hour + 2 - 24;
  }
  const strHour = (hour < 10 ? '0' + hour : '' + hour);
  return [strHour, timeArray[1], timeArray[2]].join(':')
})


For more options, visit https://groups.google.com/d/optout.

Kayin Scholtz

unread,
Mar 29, 2019, 11:59:47 AM3/29/19
to Taylor Downs, OpenFn Community
Hi Taylor,

It works! The => was causing issues, but with the following it's working:

      field("Time_In__c", function(state) 
      { const timeArray = state.data.time_in.split(':');
      var hour = parseInt(timeArray[0]);
      if (hour < 21) 
      {hour = hour + 2} 
      else {
    hour = hour + 2 - 24;}
  const strHour = (hour < 10 ? '0' + hour : '' + hour);
  return [strHour, timeArray[1], timeArray[2]].join(':')
}),
      field("Time_Out__c", function(state) 
      { const timeArray = state.data.time_out.split(':');
      var hour = parseInt(timeArray[0]);
      if (hour < 21) 
      {hour = hour + 2} 
      else {
    hour = hour + 2 - 24;}
  const strHour = (hour < 10 ? '0' + hour : '' + hour);
  return [strHour, timeArray[1], timeArray[2]].join(':')
}),

Taylor Downs

unread,
Mar 29, 2019, 1:32:19 PM3/29/19
to Kayin Scholtz, OpenFn Community
Great! And oops, sorry... I was combining that fat arrow syntax with the standard function(arg) { ... } syntax. 
Reply all
Reply to author
Forward
0 new messages