|
50 | 50 | import microsoft.exchange.webservices.data.core.exception.service.local.ServiceVersionException; |
51 | 51 | import microsoft.exchange.webservices.data.misc.TimeSpan; |
52 | 52 | import microsoft.exchange.webservices.data.property.complex.ItemAttachment; |
| 53 | + |
53 | 54 | import org.apache.commons.logging.Log; |
54 | 55 | import org.apache.commons.logging.LogFactory; |
| 56 | +import org.joda.time.Period; |
| 57 | +import org.joda.time.format.ISOPeriodFormat; |
55 | 58 |
|
56 | 59 | import javax.xml.stream.XMLOutputFactory; |
57 | 60 | import javax.xml.stream.XMLStreamException; |
@@ -875,170 +878,22 @@ public static TimeSpan getXSDurationToTimeSpan(String xsDuration) { |
875 | 878 | negative = true; |
876 | 879 | } |
877 | 880 |
|
878 | | - // Year |
879 | | - m = PATTERN_YEAR.matcher(xsDuration); |
880 | | - int year = 0; |
881 | | - if (m.find()) { |
882 | | - year = Integer.parseInt(m.group().substring(0, |
883 | | - m.group().indexOf("Y"))); |
884 | | - } |
885 | | - |
886 | | - // Month |
887 | | - m = PATTERN_MONTH.matcher(xsDuration); |
888 | | - int month = 0; |
889 | | - if (m.find()) { |
890 | | - month = Integer.parseInt(m.group().substring(0, |
891 | | - m.group().indexOf("M"))); |
892 | | - } |
893 | | - |
894 | | - // Day |
895 | | - m = PATTERN_DAY.matcher(xsDuration); |
896 | | - int day = 0; |
897 | | - if (m.find()) { |
898 | | - day = Integer.parseInt(m.group().substring(0, |
899 | | - m.group().indexOf("D"))); |
900 | | - } |
901 | | - |
902 | | - // Hour |
903 | | - m = PATTERN_HOUR.matcher(xsDuration); |
904 | | - int hour = 0; |
905 | | - if (m.find()) { |
906 | | - hour = Integer.parseInt(m.group().substring(0, |
907 | | - m.group().indexOf("H"))); |
908 | | - } |
909 | | - |
910 | | - // Minute |
911 | | - m = PATTERN_MINUTES.matcher(xsDuration); |
912 | | - int minute = 0; |
913 | | - if (m.find()) { |
914 | | - minute = Integer.parseInt(m.group().substring(0, |
915 | | - m.group().indexOf("M"))); |
916 | | - } |
917 | | - |
918 | | - // Seconds |
919 | | - m = PATTERN_SECONDS.matcher(xsDuration); |
920 | | - int seconds = 0; |
921 | | - if (m.find()) { |
922 | | - seconds = Integer.parseInt(m.group().substring(0, |
923 | | - m.group().indexOf("."))); |
924 | | - } |
925 | | - |
926 | | - int milliseconds = 0; |
927 | | - m = PATTERN_MILLISECONDS.matcher(xsDuration); |
928 | | - if (m.find()) { |
929 | | - // Only allowed 4 digits of precision |
930 | | - if (m.group().length() > 5) { |
931 | | - milliseconds = Integer.parseInt(m.group().substring(0, 4)); |
932 | | - } else { |
933 | | - seconds = Integer.parseInt(m.group().substring(0, |
934 | | - m.group().indexOf("S"))); |
935 | | - } |
936 | | - } |
937 | | - |
938 | | - // Apply conversions of year and months to days. |
939 | | - // Year = 365 days |
940 | | - // Month = 30 days |
941 | | - day = day + (year * 365) + (month * 30); |
942 | | - // TimeSpan retval = new TimeSpan(day, hour, minute, seconds, |
943 | | - // milliseconds); |
944 | | - long retval = (((((((day * 24) + hour) * 60) + minute) * 60) + |
945 | | - seconds) * 1000) + milliseconds; |
| 881 | + // Removing leading '-' |
946 | 882 | if (negative) { |
947 | | - retval = -retval; |
948 | | - } |
949 | | - return new TimeSpan(retval); |
950 | | - |
951 | | - } |
952 | | - |
953 | | - /** |
954 | | - * Takes an xs:duration string as defined by the W3 Consortiums |
955 | | - * Recommendation "XML Schema Part 2: Datatypes Second Edition", |
956 | | - * http://www.w3.org/TR/xmlschema-2/#duration, and converts it into a |
957 | | - * System.TimeSpan structure This method uses the following approximations: |
958 | | - * 1 year = 365 days 1 month = 30 days Additionally, it only allows for four |
959 | | - * decimal points of seconds precision. |
960 | | - * |
961 | | - * @param xsDuration xs:duration string to convert |
962 | | - * @return System.TimeSpan structure |
963 | | - */ |
964 | | - public static TimeSpan getXSDurationToTimeSpanValue(String xsDuration) { |
965 | | - // TODO: Need to check whether this should be the equivalent or not |
966 | | - Matcher m = PATTERN_TIME_SPAN.matcher(xsDuration); |
967 | | - boolean negative = false; |
968 | | - if (m.find()) { |
969 | | - negative = true; |
970 | | - } |
971 | | - |
972 | | - // Year |
973 | | - // m = Pattern.compile("(\\d+)Y").matcher(xsDuration); |
974 | | - // int year = 0; |
975 | | - // if (m.find()) { |
976 | | - // year = Integer.parseInt(m.group().substring(0, |
977 | | - // m.group().indexOf("Y"))); |
978 | | - // } |
979 | | - |
980 | | - // Month |
981 | | - // m = Pattern.compile("(\\d+)M").matcher(xsDuration); |
982 | | - // int month = 0; |
983 | | - // if (m.find()) { |
984 | | - // month = Integer.parseInt(m.group().substring(0, |
985 | | - // m.group().indexOf("M"))); |
986 | | - // } |
987 | | - |
988 | | - // Day |
989 | | - m = PATTERN_DAY.matcher(xsDuration); |
990 | | - long day = 0; |
991 | | - if (m.find()) { |
992 | | - day = Integer.parseInt(m.group().substring(0, |
993 | | - m.group().indexOf("D"))); |
994 | | - } |
995 | | - |
996 | | - // Hour |
997 | | - m = PATTERN_HOUR.matcher(xsDuration); |
998 | | - int hour = 0; |
999 | | - if (m.find()) { |
1000 | | - hour = Integer.parseInt(m.group().substring(0, |
1001 | | - m.group().indexOf("H"))); |
1002 | | - } |
1003 | | - |
1004 | | - // Minute |
1005 | | - m = PATTERN_MINUTES.matcher(xsDuration); |
1006 | | - int minute = 0; |
1007 | | - if (m.find()) { |
1008 | | - minute = Integer.parseInt(m.group().substring(0, |
1009 | | - m.group().indexOf("M"))); |
1010 | | - } |
1011 | | - |
1012 | | - // Seconds |
1013 | | - m = PATTERN_SECONDS.matcher(xsDuration); |
1014 | | - int seconds = 0; |
1015 | | - int milliseconds = 0; |
1016 | | - m = PATTERN_MILLISECONDS.matcher(xsDuration); |
1017 | | - if (m.find()) { |
1018 | | - // Only allowed 4 digits of precision |
1019 | | - if (m.group().length() > 5) { |
1020 | | - milliseconds = Integer.parseInt(m.group().substring(0, 4)); |
1021 | | - } else { |
1022 | | - seconds = Integer.parseInt(m.group().substring(0, m.group().indexOf("S"))); |
1023 | | - } |
| 883 | + xsDuration = xsDuration.replace("-P", "P"); |
1024 | 884 | } |
1025 | 885 |
|
1026 | | - // Apply conversions of year and months to days. |
1027 | | - // Year = 365 days |
1028 | | - // Month = 30 days |
1029 | | - // day = day + (year * 365) + (month * 30); |
1030 | | - //TimeSpan retval = new TimeSpan(day, hour, minute, seconds, |
1031 | | - // milliseconds); |
1032 | | - |
1033 | | - long retval = |
1034 | | - day * TimeSpan.DAYS + hour * TimeSpan.HOURS + minute * TimeSpan.MINUTES + seconds * TimeSpan.SECONDS |
1035 | | - + milliseconds * TimeSpan.MILLISECONDS; |
| 886 | + Period period = Period.parse(xsDuration, ISOPeriodFormat.standard()); |
| 887 | + |
| 888 | + long retval = period.toStandardDuration().getMillis(); |
| 889 | + |
1036 | 890 | if (negative) { |
1037 | 891 | retval = -retval; |
1038 | 892 | } |
| 893 | + |
1039 | 894 | return new TimeSpan(retval); |
1040 | | - } |
1041 | 895 |
|
| 896 | + } |
1042 | 897 |
|
1043 | 898 | /** |
1044 | 899 | * Time span to xs time. |
|
0 commit comments