Go to main content

help desk

QUICK TIP: How to handle fields with a timezone shift with Model packages

When getting data from a table through a Model package, you can make sure dates are shifted automatically to your target timezone. 

Asked on 2022-12-09 08:15:00

OFFICIAL ANSWER

When getting data from a table through a Model package, you can make sure dates are shifted automatically to your target timezone. To do this, you need to, once per runtime, setup the target timezone and list the fields you want to shift to apply. By default, only the sn_cdate and sn_mdate will be shifted. You can add more fields by altering the ::timezone_fields array of your Model package.

Example on how to set a target timezone

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

function main(cgidata, extranet_user)
	// Setup target timezone - This only needs to be done once per runtime
	ps_patient_statistic::timezone = extranet_user.state.ps.selected_location.location.timezone;
	// Add some fields to the list of timezonable fields - This only needs to be done once per runtime
	ps_patient_statistic::timezone_fields = ps_patient_statistic::timezone_fields.array_merge([
 "first_visit_date",  "last_visit_date",  "last_payment",
 "last_submission"
 ]);

 [...]

	// All ::get will have its "timezone_fields" shifted to the target timezone. 
	statistic = ps_patient_statistic::get(1234);
	[...]
endf

Your ::get result will now be extended with new virtual fields with suffix "_tz" for all fields given in the  ::timezone_fields. The original fields remains if you ever need them.sn_cdate -> original name, original version
sn_cdate_tz -> shifted name and version

Server timezone

By including the timezone package, you will automatically gain acces to the server timezone. As soon as the package is included, a init() will auto execute and set `timezone::server_timezone` with the server timezone.

Answer by:
Etienne Carrier

Replied on: 2022-12-09 10:24:00