Mercurial > emacs
comparison src/editfns.c @ 16269:79e6c47054c5
(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.
author | Paul Eggert <eggert@twinsun.com> |
---|---|
date | Sat, 21 Sep 1996 18:42:26 +0000 |
parents | 7558d82368f9 |
children | 17304eb73f97 |
comparison
equal
deleted
inserted
replaced
16268:eed1157f0587 | 16269:79e6c47054c5 |
---|---|
40 #define max(a, b) ((a) > (b) ? (a) : (b)) | 40 #define max(a, b) ((a) > (b) ? (a) : (b)) |
41 | 41 |
42 extern char **environ; | 42 extern char **environ; |
43 extern Lisp_Object make_time (); | 43 extern Lisp_Object make_time (); |
44 extern void insert_from_buffer (); | 44 extern void insert_from_buffer (); |
45 static long difftm (); | 45 static int tm_diff (); |
46 static void update_buffer_properties (); | 46 static void update_buffer_properties (); |
47 void set_time_zone_rule (); | 47 void set_time_zone_rule (); |
48 | 48 |
49 Lisp_Object Vbuffer_access_fontify_functions; | 49 Lisp_Object Vbuffer_access_fontify_functions; |
50 Lisp_Object Qbuffer_access_fontify_functions; | 50 Lisp_Object Qbuffer_access_fontify_functions; |
701 save_tm = *decoded_time; | 701 save_tm = *decoded_time; |
702 decoded_time = gmtime (&time_spec); | 702 decoded_time = gmtime (&time_spec); |
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 |
710 DEFUN ("encode-time", Fencode_time, Sencode_time, 6, MANY, 0, | 710 DEFUN ("encode-time", Fencode_time, Sencode_time, 6, MANY, 0, |
711 "Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.\n\ | 711 "Convert SECOND, MINUTE, HOUR, DAY, MONTH, YEAR and ZONE to internal time.\n\ |
819 buf[24] = 0; | 819 buf[24] = 0; |
820 | 820 |
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. |
827 static long | 827 This function is copied from the GNU C Library. */ |
828 difftm (a, b) | 828 static int |
829 tm_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 |
852 DEFUN ("current-time-zone", Fcurrent_time_zone, Scurrent_time_zone, 0, 1, 0, | 850 DEFUN ("current-time-zone", Fcurrent_time_zone, Scurrent_time_zone, 0, 1, 0, |
853 "Return the offset and name for the local time zone.\n\ | 851 "Return the offset and name for the local time zone.\n\ |
874 | 872 |
875 if (lisp_time_argument (specified_time, &value) | 873 if (lisp_time_argument (specified_time, &value) |
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) |
888 s = (char *)t->tm_zone; | 886 s = (char *)t->tm_zone; |
889 #else /* not HAVE_TM_ZONE */ | 887 #else /* not HAVE_TM_ZONE */ |