From: Dmitry Antipov Date: Wed, 14 Jan 2015 09:43:43 +0000 (+0300) Subject: Consistently handle time zone specification. X-Git-Tag: emacs-25.0.90~2603^2~8 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=da396beb6e0b9ef9a45afb849eeda26da14038af;p=emacs.git Consistently handle time zone specification. * editfns.c (decode_time_zone): New function, refactored out from ... (Fencode_time): ... adjusted user. (Fset_time_zone_rule): Use decode_time_zone. --- diff --git a/src/ChangeLog b/src/ChangeLog index 3523ea41570..4f7ef6ef03e 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -16,6 +16,11 @@ * callproc.c (encode_current_directory, call_process): * process.c (Fstart_process): Use it. + Consistently handle time zone specification. + * editfns.c (decode_time_zone): New function, refactored out from ... + (Fencode_time): ... adjusted user. + (Fset_time_zone_rule): Use decode_time_zone. + 2015-01-14 Paul Eggert Use bool for boolean in xmenu.c, xml.c diff --git a/src/editfns.c b/src/editfns.c index cd15f6569aa..e097893a6f9 100644 --- a/src/editfns.c +++ b/src/editfns.c @@ -2063,6 +2063,33 @@ check_tm_member (Lisp_Object obj, int offset) return n - offset; } +/* Decode ZONE as a time zone specification. */ + +static const char * +decode_time_zone (Lisp_Object zone) +{ + const char *tzstring = NULL; + + if (EQ (zone, Qt)) + tzstring = "UTC0"; + else if (STRINGP (zone)) + tzstring = SSDATA (zone); + else if (INTEGERP (zone)) + { + static char const tzbuf_format[] = "XXX%s%"pI"d:%02d:%02d"; + char tzbuf[sizeof tzbuf_format + INT_STRLEN_BOUND (EMACS_INT)]; + EMACS_INT abszone = eabs (XINT (zone)), zone_hr = abszone / (60 * 60); + int zone_min = (abszone / 60) % 60, zone_sec = abszone % 60; + + sprintf (tzbuf, tzbuf_format, &"-"[XINT (zone) < 0], + zone_hr, zone_min, zone_sec); + tzstring = tzbuf; + } + else + xsignal2 (Qerror, build_string ("Invalid time zone specification"), zone); + return tzstring; +} + DEFUN ("encode-time", Fencode_time, Sencode_time, 6, MANY, 0, doc: /* Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time. This is the reverse operation of `decode-time', which see. @@ -2105,30 +2132,7 @@ usage: (encode-time SECOND MINUTE HOUR DAY MONTH YEAR &optional ZONE) */) value = mktime (&tm); else { - static char const tzbuf_format[] = "XXX%s%"pI"d:%02d:%02d"; - char tzbuf[sizeof tzbuf_format + INT_STRLEN_BOUND (EMACS_INT)]; - const char *tzstring; - - if (EQ (zone, Qt)) - tzstring = "UTC0"; - else if (STRINGP (zone)) - tzstring = SSDATA (zone); - else if (INTEGERP (zone)) - { - EMACS_INT abszone = eabs (XINT (zone)); - EMACS_INT zone_hr = abszone / (60*60); - int zone_min = (abszone/60) % 60; - int zone_sec = abszone % 60; - sprintf (tzbuf, tzbuf_format, &"-"[XINT (zone) < 0], - zone_hr, zone_min, zone_sec); - tzstring = tzbuf; - } - else - tzstring = 0; - - timezone_t tz = tzstring ? tzalloc (tzstring) : 0; - if (! tz) - error ("Invalid time zone specification"); + timezone_t tz = tzalloc (decode_time_zone (zone)); value = mktime_z (tz, &tm); tzfree (tz); } @@ -2265,7 +2269,8 @@ the data it can't find. */) DEFUN ("set-time-zone-rule", Fset_time_zone_rule, Sset_time_zone_rule, 1, 1, 0, doc: /* Set the local time zone using TZ, a string specifying a time zone rule. If TZ is nil, use implementation-defined default time zone information. -If TZ is t, use Universal Time. +If TZ is t, use Universal Time. If TZ is an integer, it is treated as in +`encode-time'. Instead of calling this function, you typically want (setenv "TZ" TZ). That changes both the environment of the Emacs process and the @@ -2273,17 +2278,7 @@ variable `process-environment', whereas `set-time-zone-rule' affects only the former. */) (Lisp_Object tz) { - const char *tzstring; - - if (! (NILP (tz) || EQ (tz, Qt))) - CHECK_STRING (tz); - - if (NILP (tz)) - tzstring = initial_tz; - else if (EQ (tz, Qt)) - tzstring = "UTC0"; - else - tzstring = SSDATA (tz); + const char *tzstring = NILP (tz) ? initial_tz : decode_time_zone (tz); block_input (); set_time_zone_rule (tzstring);