How is message handling implemented in Mathematica when applying Set to unprotected global variables such as $TimeZone in the System context?


The documentation indicates that Set can be used with arguments such as Integer, Real, or a time zone String (e.g. "Asia/Hong_Kong"), and so if I set $TimeZone to something I know not to exist such as $TimeZone = "America/Agloe" or anything not supported for that matter, I receive the following message:

$TimeZone::tzset error message

I suspect this functionality has been implemented using a low-level or undocumented version of something similar to $Pre or its close cousin $PreRead. As these are both also global variables defined in the System context it appears this functionality is not implemented using them.

Problem Motivating the Question:

I would like to implement in one of my own packages a similar behavior for some exported global variables that will hold package settings as state variables but without having to define one or more setGlobalVariable[...] functions and avoiding protecting these global variables. This would more easily open up the possibilty to override these global variables with the Block[{$MyGlobalVariable = MySettingForGlobalVariable},...] construct.

  • 1
    $\begingroup$ Look up Message and MessageName in the Documentation Center. Also, see this answer for an example of how these functions are used. $\endgroup$
    – m_goldberg
    Nov 18, 2015 at 5:18

1 Answer 1



$tz::tzset = 
  "Unable to set $tz to `1`. Time zone should be 
       an integer, real number, a string, or an Entity object.";

$tz /: Set[$tz, x_] := 
 If[Internal`PossibleTimeZoneQ[x], OwnValues[$tz] = {HoldPattern[$tz] -> x}; x, 
  Message[$tz::tzset, x, False]]

$tz = 1

$\ $1


$tz = "America/Agloe"



$\ $1

  • $\begingroup$ How does $TimeZone avoid showing something other than {} for UpValues in the case of UpValues[$TimeZone]? $\endgroup$
    – rfrasier
    Nov 18, 2015 at 7:47
  • $\begingroup$ @rfrasier-mlp My answer only mimics the behavior of $TimeZone in a limited way. $TimeZone probably doesn't use UpVales. Try for example OwnValues[$TimeZone] = {HoldPattern[$TimeZone] -> "test"}; ?? $TimeZone and then $TimeZone; ?? $TimeZone. $\endgroup$
    – Karsten7
    Nov 18, 2015 at 8:20
  • $\begingroup$ @rfrasier-mlp You could use Attributes[$tz] = {ReadProtected} to hide the UpValues, when ?? $tz is used. $\endgroup$
    – Karsten7
    Nov 18, 2015 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.