Turning off the timezone (and more) in AutoDateFormatter

Hi all,

I'm passing in "None" for the timezone in AutoDateFormatter.

What I am getting is "UTC", but what I want is nothing.

Looking at the code, I see lines like:

     self._formatter = DateFormatter("%H:%M:%S %Z", self._tz)

so my None is getting passed through. Then in DateFormatter, I see:

         if tz is None: tz = _get_rc_timezone()

Which looks like it's getting a timezone from an rc parameter. This
strikes me as "not such a good idea". Far too easy to get the timezone
wrong -- I'm not sure there should be such a thing as a default time zone.

However, the issue at hand is that I want to not show the timezone at
all. I would have thought that None would be a good way to spell that,
but it's taken already to mean "default".

So that means editing/overriding AutoDateFormatter. The way it's
written, I pretty much have to re-write the entire thing, which isn't so
bad, but it might be nicer if we could make it easier to override just
part of it.

One idea:

Some say that the "right" way to spell "case/switch" in Python is a dict, rather than a collection of if--elifs. So in this case, we could make a dict of format strings, something like below.

I've tried this, and I can now easily override the formatting for a particular time range, like so:

     Formatter = mpl.dates.AutoDateFormatter(locator, tz=None)
     # edit the format strings to remove the TZ spec
     # this only works with my custom version of AutoDateLocator
     Formatter.format_strings[1.0/24.0] = "%H:%M:%S"

I do agree with the comments that there are other ways to improve this,
but maybe this is a start.

-Chris

class AutoDateFormatter(ticker.Formatter):
     """
     This class attempts to figure out the best format to use. This is
     most useful when used with the :class:`AutoDateLocator`.
     """

     # This can be improved by providing some user-level direction on
     # how to choose the best format (precedence, etc...)

     # Perhaps a 'struct' that has a field for each time-type where a
     # zero would indicate "don't show" and a number would indicate
     # "show" with some sort of priority. Same priorities could mean
     # show all with the same priority.

     # Or more simply, perhaps just a format string for each
     # possibility...

     def __init__(self, locator, tz=None):
         self._locator = locator
         self._formatter = DateFormatter("%b %d %Y %H:%M:%S %Z", tz)
         self._tz = tz

         self.format_strings = { 365.0 : "%Y",
                                  30.0 : "%b %Y",
                                   1.0 : "%b %d %Y",
                                   7.0 : "%b %d %Y",
                                1.0/24.0 : "%H:%M:%S %Z",
                                1.0/(24*60) : "%H:%M:%S %Z",
                                1.0/(24*3600) : "%H:%M:%S %Z",
                                }
         self.fallback_format = "%b %d %Y %H:%M:%S %Z"

     def __call__(self, x, pos=0):
         scale = float( self._locator._get_unit() )

         f_string = self.format_strings.get(scale, self.fallback_format)

         self._formatter = DateFormatter(f_string, self._tz)

         return self._formatter(x, pos)

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@...259...

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@...259...