aboutsummaryrefslogtreecommitdiffstats
path: root/src/editfns.c
diff options
context:
space:
mode:
authorPaul Eggert1996-09-21 18:42:26 +0000
committerPaul Eggert1996-09-21 18:42:26 +0000
commit9475166699a6413fe5ea2dadfb2a20d33397cbdc (patch)
treedc0191e10ad875fc41ac641301cd9c748ee879b2 /src/editfns.c
parent36fe2f9a101cfd0ce42e68509c30f5200adb9b91 (diff)
downloademacs-9475166699a6413fe5ea2dadfb2a20d33397cbdc.tar.gz
emacs-9475166699a6413fe5ea2dadfb2a20d33397cbdc.zip
(tm_diff): Renamed from difftm. Yield int, not long.
This now uses the same code as the GNU C Library. All callers changed. (TM_YEAR_BASE): Renamed from TM_YEAR_ORIGIN.
Diffstat (limited to 'src/editfns.c')
-rw-r--r--src/editfns.c50
1 files changed, 24 insertions, 26 deletions
diff --git a/src/editfns.c b/src/editfns.c
index 92daf882d86..ee126f06e16 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -42,7 +42,7 @@ Boston, MA 02111-1307, USA. */
42extern char **environ; 42extern char **environ;
43extern Lisp_Object make_time (); 43extern Lisp_Object make_time ();
44extern void insert_from_buffer (); 44extern void insert_from_buffer ();
45static long difftm (); 45static int tm_diff ();
46static void update_buffer_properties (); 46static void update_buffer_properties ();
47void set_time_zone_rule (); 47void set_time_zone_rule ();
48 48
@@ -703,7 +703,7 @@ ZONE is an integer indicating the number of seconds east of Greenwich.\n\
703 if (decoded_time == 0) 703 if (decoded_time == 0)
704 list_args[8] = Qnil; 704 list_args[8] = Qnil;
705 else 705 else
706 XSETINT (list_args[8], difftm (&save_tm, decoded_time)); 706 XSETINT (list_args[8], tm_diff (&save_tm, decoded_time));
707 return Flist (9, list_args); 707 return Flist (9, list_args);
708} 708}
709 709
@@ -821,31 +821,29 @@ and from `file-attributes'.")
821 return build_string (buf); 821 return build_string (buf);
822} 822}
823 823
824#define TM_YEAR_ORIGIN 1900 824#define TM_YEAR_BASE 1900
825 825
826/* Yield A - B, measured in seconds. */ 826/* Yield A - B, measured in seconds.
827static long 827 This function is copied from the GNU C Library. */
828difftm (a, b) 828static int
829tm_diff (a, b)
829 struct tm *a, *b; 830 struct tm *a, *b;
830{ 831{
831 int ay = a->tm_year + (TM_YEAR_ORIGIN - 1); 832 /* Compute intervening leap days correctly even if year is negative.
832 int by = b->tm_year + (TM_YEAR_ORIGIN - 1); 833 Take care to avoid int overflow in leap day calculations,
833 /* Divide years by 100, rounding towards minus infinity. */ 834 but it's OK to assume that A and B are close to each other. */
834 int ac = ay / 100 - (ay % 100 < 0); 835 int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3);
835 int bc = by / 100 - (by % 100 < 0); 836 int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3);
836 /* Some compilers can't handle this as a single return statement. */ 837 int a100 = a4 / 25 - (a4 % 25 < 0);
837 long days = ( 838 int b100 = b4 / 25 - (b4 % 25 < 0);
838 /* difference in day of year */ 839 int a400 = a100 >> 2;
839 a->tm_yday - b->tm_yday 840 int b400 = b100 >> 2;
840 /* + intervening leap days */ 841 int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
841 + ((ay >> 2) - (by >> 2)) 842 int years = a->tm_year - b->tm_year;
842 - (ac - bc) 843 int days = (365 * years + intervening_leap_days
843 + ((ac >> 2) - (bc >> 2)) 844 + (a->tm_yday - b->tm_yday));
844 /* + difference in years * 365 */ 845 return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
845 + (long)(ay-by) * 365 846 + (a->tm_min - b->tm_min))
846 );
847 return (60*(60*(24*days + (a->tm_hour - b->tm_hour))
848 + (a->tm_min - b->tm_min))
849 + (a->tm_sec - b->tm_sec)); 847 + (a->tm_sec - b->tm_sec));
850} 848}
851 849
@@ -876,12 +874,12 @@ the data it can't find.")
876 && (t = gmtime (&value)) != 0) 874 && (t = gmtime (&value)) != 0)
877 { 875 {
878 struct tm gmt; 876 struct tm gmt;
879 long offset; 877 int offset;
880 char *s, buf[6]; 878 char *s, buf[6];
881 879
882 gmt = *t; /* Make a copy, in case localtime modifies *t. */ 880 gmt = *t; /* Make a copy, in case localtime modifies *t. */
883 t = localtime (&value); 881 t = localtime (&value);
884 offset = difftm (t, &gmt); 882 offset = tm_diff (t, &gmt);
885 s = 0; 883 s = 0;
886#ifdef HAVE_TM_ZONE 884#ifdef HAVE_TM_ZONE
887 if (t->tm_zone) 885 if (t->tm_zone)