Go to main content

help desk

QUICK TIP: Timezone field handling in ::set of model packages

How to handle properly timezone fields when recording data in the table

Posé le 2022-12-09 08:15:00

RÉPONSE OFFICIELLE

We know we can automatically shift fields that are declared as timezone_fields in our Model package when using the ::get function. See details here: https://extenso.live/en/quick-tip--how-to-handle-fields-with-a-timezone-shift-with-model-packagesWe can now also automate this process in the ::set, here's how...

[...]
%include_once "/site/ps_core/package/model/p_ps_health_plan.sn";
[...]

function main(cgidata, extranet_user)

 ps_health_plan::timezone = extranet_user.state.ps.selected_location.location.timezone; // Enable timezone shifting
 ps_health_plan::timezone_fields = ps_health_plan::timezone_fields.array_merge([ // List fields that will be shifted
 "estimated_start_date",
 "initial_treatment"
 ]);


	[...]
	switch operation
	[...]
 case "update_fields":
 fields = unstringify(cgidata.payload);
 	health_plan = ps_health_plan::set(
	cgidata.uid_health_plan, 	fields, 	load_details: false, 	auto_add_time: true, // Automatically add " 00:00:00" to datetime fields if missing
	shift_timezonable_fields: true // Automatically shift from pck::timezone to server_timezone fields that are in the list of pck::timezone_fields
	);
	[...]
	endc
	ends
endf

NOTE: If you use the ::set with parameter load_details: true, since it passes by a ::get internally, and your timezone and timezone_fields are already defined, your returned object will already contains the virtual shifted fields (with field names suffixed with "_tz");NOTE: You can also, instead of using the parameters auto_add_time and shift_timezonable_fields in your ::set fonction, you can set those at a global level, ex:

ps_health_plan::timezone = extranet_user.state.ps.selected_location.location.timezone;
ps_health_plan::timezone_fields = ps_health_plan::timezone_fields.array_merge([
 "estimated_start_date",
 "initial_treatment"
]);
ps_health_plan::DEFAULT_SET_SHIFT_TIMEZONABLE_FIELDS = true; ps_health_plan::DEFAULT_SET_AUTO_ADD_TIME = true;
[...]

health_plan = ps_health_plan::set(cgidata.uid_health_plan, fields);

WARNING: Remember that by default pck::timezone_fields contains sn_cdate and sn_mdate, so if you have enabled the zone shifting AND you provide the ::set with a value for those fields, they will get shifted like any others. This is usually not an issue since sn_cdate and sn_mdate are system handled fields, and if provided, it should be considered as a hack and special attention should be given to those values in that case.

Réponse de:
Etienne Carrier

Répondu le : 2022-12-09 10:24:00