comparison libpurple/util.c @ 21667:919338d399ae

merge of '2fcb5d2cb35c5ba692592e22bc442c3d62000f85' and '61c8d242483046a3b3b43cd1afbb794af35a08c9'
author Ethan Blanton <elb@pidgin.im>
date Tue, 27 Nov 2007 15:57:08 +0000
parents b2aa68cdc8b9
children 8f82dc5e0b76
comparison
equal deleted inserted replaced
21438:4fe9acb6b514 21667:919338d399ae
751 time_t 751 time_t
752 purple_str_to_time(const char *timestamp, gboolean utc, 752 purple_str_to_time(const char *timestamp, gboolean utc,
753 struct tm *tm, long *tz_off, const char **rest) 753 struct tm *tm, long *tz_off, const char **rest)
754 { 754 {
755 time_t retval = 0; 755 time_t retval = 0;
756 struct tm *t; 756 static struct tm t;
757 const char *c = timestamp; 757 const char *c = timestamp;
758 int year = 0; 758 int year = 0;
759 long tzoff = PURPLE_NO_TZ_OFF; 759 long tzoff = PURPLE_NO_TZ_OFF;
760 760
761 time(&retval); 761 time(&retval);
762 t = localtime(&retval); 762 localtime_r(&retval, &t);
763 763
764 /* 4 digit year */ 764 /* 4 digit year */
765 if (sscanf(c, "%04d", &year) && year > 1900) 765 if (sscanf(c, "%04d", &year) && year > 1900)
766 { 766 {
767 c += 4; 767 c += 4;
768 if (*c == '-') 768 if (*c == '-')
769 c++; 769 c++;
770 t->tm_year = year - 1900; 770 t.tm_year = year - 1900;
771 } 771 }
772 772
773 /* 2 digit month */ 773 /* 2 digit month */
774 if (!sscanf(c, "%02d", &t->tm_mon)) 774 if (!sscanf(c, "%02d", &t.tm_mon))
775 { 775 {
776 if (rest != NULL && *c != '\0') 776 if (rest != NULL && *c != '\0')
777 *rest = c; 777 *rest = c;
778 return 0; 778 return 0;
779 } 779 }
780 c += 2; 780 c += 2;
781 if (*c == '-' || *c == '/') 781 if (*c == '-' || *c == '/')
782 c++; 782 c++;
783 t->tm_mon -= 1; 783 t.tm_mon -= 1;
784 784
785 /* 2 digit day */ 785 /* 2 digit day */
786 if (!sscanf(c, "%02d", &t->tm_mday)) 786 if (!sscanf(c, "%02d", &t.tm_mday))
787 { 787 {
788 if (rest != NULL && *c != '\0') 788 if (rest != NULL && *c != '\0')
789 *rest = c; 789 *rest = c;
790 return 0; 790 return 0;
791 } 791 }
792 c += 2; 792 c += 2;
793 if (*c == '/') 793 if (*c == '/')
794 { 794 {
795 c++; 795 c++;
796 796
797 if (!sscanf(c, "%04d", &t->tm_year)) 797 if (!sscanf(c, "%04d", &t.tm_year))
798 { 798 {
799 if (rest != NULL && *c != '\0') 799 if (rest != NULL && *c != '\0')
800 *rest = c; 800 *rest = c;
801 return 0; 801 return 0;
802 } 802 }
803 t->tm_year -= 1900; 803 t.tm_year -= 1900;
804 } 804 }
805 else if (*c == 'T' || *c == '.') 805 else if (*c == 'T' || *c == '.')
806 { 806 {
807 c++; 807 c++;
808 /* we have more than a date, keep going */ 808 /* we have more than a date, keep going */
809 809
810 /* 2 digit hour */ 810 /* 2 digit hour */
811 if ((sscanf(c, "%02d:%02d:%02d", &t->tm_hour, &t->tm_min, &t->tm_sec) == 3 && (c = c + 8)) || 811 if ((sscanf(c, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 && (c = c + 8)) ||
812 (sscanf(c, "%02d%02d%02d", &t->tm_hour, &t->tm_min, &t->tm_sec) == 3 && (c = c + 6))) 812 (sscanf(c, "%02d%02d%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 && (c = c + 6)))
813 { 813 {
814 gboolean offset_positive = FALSE; 814 gboolean offset_positive = FALSE;
815 int tzhrs; 815 int tzhrs;
816 int tzmins; 816 int tzmins;
817 817
818 t->tm_isdst = -1; 818 t.tm_isdst = -1;
819 819
820 if (*c == '.' && *(c+1) >= '0' && *(c+1) <= '9') /* dealing with precision we don't care about */ 820 if (*c == '.') {
821 c += 4; 821 do {
822 c++;
823 } while (*c >= '0' && *c <= '9'); /* dealing with precision we don't care about */
824 }
822 if (*c == '+') 825 if (*c == '+')
823 offset_positive = TRUE; 826 offset_positive = TRUE;
824 if (((*c == '+' || *c == '-') && (c = c + 1)) && 827 if (((*c == '+' || *c == '-') && (c = c + 1)) &&
825 ((sscanf(c, "%02d:%02d", &tzhrs, &tzmins) == 2 && (c = c + 5)) || 828 ((sscanf(c, "%02d:%02d", &tzhrs, &tzmins) == 2 && (c = c + 5)) ||
826 (sscanf(c, "%02d%02d", &tzhrs, &tzmins) == 2 && (c = c + 4)))) 829 (sscanf(c, "%02d%02d", &tzhrs, &tzmins) == 2 && (c = c + 4))))
828 tzoff = tzhrs*60*60 + tzmins*60; 831 tzoff = tzhrs*60*60 + tzmins*60;
829 if (offset_positive) 832 if (offset_positive)
830 tzoff *= -1; 833 tzoff *= -1;
831 /* We don't want the C library doing DST calculations 834 /* We don't want the C library doing DST calculations
832 * if we know the UTC offset already. */ 835 * if we know the UTC offset already. */
833 t->tm_isdst = 0; 836 t.tm_isdst = 0;
834 } 837 }
835 else if (utc) 838 else if (utc)
836 { 839 {
837 t->tm_isdst = -1; 840 static struct tm tmptm;
841 time_t tmp;
842 tmp = mktime(&t);
843 /* we care about whether it *was* dst, and the offset, here on this
844 * date, not whether we are currently observing dst locally *now*.
845 * This isn't perfect, because we would need to know in advance the
846 * offset we are trying to work out in advance to be sure this
847 * works for times around dst transitions but it'll have to do. */
848 localtime_r(&tmp, &tmptm);
849 t.tm_isdst = tmptm.tm_isdst;
850 #ifdef HAVE_TM_GMTOFF
851 t.tm_gmtoff = tmptm.tm_gmtoff;
852 #endif
838 } 853 }
839 854
840 if (rest != NULL && *c != '\0') 855 if (rest != NULL && *c != '\0')
841 { 856 {
842 if (*c == ' ') 857 if (*c == ' ')
861 tzoff = PURPLE_NO_TZ_OFF; 876 tzoff = PURPLE_NO_TZ_OFF;
862 else 877 else
863 tzoff += sys_tzoff; 878 tzoff += sys_tzoff;
864 #else 879 #else
865 #ifdef HAVE_TM_GMTOFF 880 #ifdef HAVE_TM_GMTOFF
866 tzoff += t->tm_gmtoff; 881 tzoff += t.tm_gmtoff;
867 #else 882 #else
868 # ifdef HAVE_TIMEZONE 883 # ifdef HAVE_TIMEZONE
869 tzset(); /* making sure */ 884 tzset(); /* making sure */
870 tzoff -= timezone; 885 tzoff -= timezone;
871 # endif 886 # endif
880 } 895 }
881 } 896 }
882 897
883 if (tm != NULL) 898 if (tm != NULL)
884 { 899 {
885 *tm = *t; 900 *tm = t;
886 tm->tm_isdst = -1; 901 tm->tm_isdst = -1;
887 mktime(tm); 902 mktime(tm);
888 } 903 }
889 904
890 retval = mktime(t); 905 retval = mktime(&t);
891 if (tzoff != PURPLE_NO_TZ_OFF) 906 if (tzoff != PURPLE_NO_TZ_OFF)
892 retval += tzoff; 907 retval += tzoff;
893 908
894 if (tz_off != NULL) 909 if (tz_off != NULL)
895 *tz_off = tzoff; 910 *tz_off = tzoff;
2935 2950
2936 if (tmp == NULL) 2951 if (tmp == NULL)
2937 return FALSE; 2952 return FALSE;
2938 g_free(tmp); 2953 g_free(tmp);
2939 2954
2940 return (g_getenv("GNOME_DESKTOP_SESSION_ID") != NULL); 2955 tmp = (gchar *)g_getenv("GNOME_DESKTOP_SESSION_ID");
2956
2957 return ((tmp != NULL) && (*tmp != '\0'));
2941 #else 2958 #else
2942 return FALSE; 2959 return FALSE;
2943 #endif 2960 #endif
2944 } 2961 }
2945 2962