Arcade Calculation Expression - Local Date/Time Field - Offline - What to use?

1579
9
04-10-2023 09:39 AM
kmsmikrud
Occasional Contributor III

Good Morning,

We publish our AGOL hosted feature layers with a UTC date/time knowing then the CreationDate and EditDates will be in UTC. In addition, we have been adding a local DateTime field that gets calculated based on the CreationDate field usually in a post-survey script process.

Now with arcade expressions, I wanted to use this functionality to auto-populate these fields, but I'm seeing discrepancies for what displays on the iPads then what writes to the AGOL feature layer tables. Also if the calculations recalculate when offline data is synced, then that is another consideration as posted  here:

Below is one version I have started out with, which displays accurately in Field Maps in the layer form on the tablet, but when the data is synced from offline the date writes to the feature layer table in UTC.

 

 

 

//var dt_now_utc = Timestamp()
var akdt = Now()
//var akdt = ToLocal(dt_now_utc)
if (IsEmpty($feature.AKCreationDate)){
    return akdt;
}else{
    return akdt;
}

 

 

 

I have also tried to this which writes to the feature layer tables better (although if it recalculates when the offline edits sync, then this no longer works). This also isn't great because it displays in the tablet 8 hours behind the current time which doesn't look accurate even though it is writing to the tables accurately.

 

 

var dt_now_utc = Timestamp()
var akdt = DateAdd(dt_now_utc, -8, 'Hours')
if (IsEmpty($feature.AKCreationDate)){
    return akdt;
}else{
    return akdt;
}

 

What is the recommended arcade functions to get a local data time field to calculate using arcade expressions with offline data collection and for the data to be accurate when offline edits sync, do these values update then to the local time I think they do change.

Thanks in advance!,

Kathy

 

0 Kudos
9 Replies
DougBrowning
MVP Esteemed Contributor

Why did you comment out ToLocal?  Have you tried

var akdt = ToLocal(Now())

0 Kudos
kmsmikrud
Occasional Contributor III

Thanks for the reply.

I believe I commented it out, because I felt like 'ToLocal' wasn't working 'offline'.  If Now provides the value in the local system of the client, why add the ToLocal(Now())?  In reviewing the expressions, for one of the layers I have the following expression. Ideally all I want is the local time of the CreationDate that will work offline and not change when syncing offline edits.

The screenshot below shows the arcade expression and resulting datetime in Alaska or that was my goal. You can see the location Fix Time in UTC. The first few records in AKCreationDate look good, but we have a boat using Starlink so they are basically connected. Then the other records match the fix time and those were collected offline and those values were not converted to local datetime which leads me to believe ToLocal does not work offline.

datetime_table.jpg

var dt_now_utc = Timestamp()
var akdt = ToLocal(dt_now_utc)
if (IsEmpty($feature.AKCreationDate)){
    return akdt;
}else{
    return akdt;
}

 

0 Kudos
DougBrowning
MVP Esteemed Contributor

I have used it offline I think.  Am testing that now.  Now may show the current time for display but when it uploads to AGOL all times are converted to UTC for storage.  So it makes sense to see that in the table.  You cannot store in a time zone really its all UTC.  At Dev Summit they did say a new field type for local time is coming soon.  The time zone comes from the tablet so always good to make sure that is correct.  Also the web map will sometimes show local time converted back.  Timezones are really hard to get right.  You may need to have a Arcade field ToLocal in the popup to convert it at the time of display.

0 Kudos
kmsmikrud
Occasional Contributor III

Thanks so much and appreciate it on the testing. I'm a bit of an Island in our organization.

The table screenshot above was from ArcGIS Pro so the first few values in the AKCreationDate wrote with the 8 hour offset from UTC datetime which was perfect. I found that out on AGOL/web maps with the data displaying with local browser and its just easier on my part for processing data and dashboards to also have an AKCreationDate field.

Usually I would have python script tool that updates the fields (using the code below), but thought I would try to make use of the arcade calculated expressions this go around. 

So one step forward but also backward if the data isn't accurate for what I intended.

 

 with arcpy.da.UpdateCursor(value, ["CreationDate", "AKCreationDate", "AKDate","ManagementArea"], 'OBJECTID = ' + str(oid)) as tbl:
                        for r in tbl:
                            utc = r[0]
                            arcpy.AddMessage(str(utc))
                            ak = utc.replace(tzinfo=pytz.UTC)
                            r[1] = ak.astimezone(pytz.timezone(AKzone))
                            r[2] = r[1].strftime('%m/%d/%y')

 

 

0 Kudos
kmsmikrud
Occasional Contributor III

Hi @DougBrowning ,

I was curious how your offline datetime testing went with Field Map arcade calculations like using ToLocal? Thanks!

var akdt = ToLocal(Now())
0 Kudos
DougBrowning
MVP Esteemed Contributor

Seemed to work ok and we are using it this year.  Should use the timezone of the tablet.

0 Kudos
kmsmikrud
Occasional Contributor III

Good to know, I initially was using an expression similar to below and it looked great on the tablet, but then when I looked at the values in the attribute table in Pro they were the exact same as CreationDate so it wasn't writing the values with the conversion. But glad to know its working for you. I still feel like there was something strange going on because sometimes they would be the same as CreationDate and other times it seemed like the values updated to when the data was synced?

 

var akdt = ToLocal(Now())
var akdt = ToLocal(Timestamp())

 

I haven't had time to go back and take a look or test.....its like development and production all at the same time but for those values something wasn't right/reliable. We also had a couple of times when folks would sync their data the values would change and I couldn't help but wonder if it was recalculating when it syncs? We altered the code to accommodate a max value offline versus connected, that I still want to ask about that too.

0 Kudos
DougBrowning
MVP Esteemed Contributor

Well Aracde is calcualted as you look at it.  So a diff device would change it.  I think you would need to use this in a calculated value in Field Maps to get it to be permenant to that date.  I think there may also be a once()?

0 Kudos
kmsmikrud
Occasional Contributor III

Yup it was all in Field Maps same device and data was synced after surveys.  Fields had calculated expressions added in Field Designer. I had a user twice basically have a value change when it synced the data and this wasn't a datetime field but queried the layer for a max value so offline this is different then when it was connected. Anyway first time it happened its like that's weird and then the 2nd time its like oh its querying the whole dataset and the entire dataset wasn't taken offline in the offline area. Its a little hard to explain, but it was a good thing to catch.

I'll have to revisit both issues at some point.

0 Kudos