1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.calendar; 18 19 import com.android.calendarcommon.DateException; 20 import java.util.Calendar; 21 22 /** 23 * 24 * Dup of packages/providers/CalendarProvider/src/com/android/providers/calendar/Duration.java 25 * 26 * According to RFC2445, durations are like this: 27 * WEEKS 28 * | DAYS [ HOURS [ MINUTES [ SECONDS ] ] ] 29 * | HOURS [ MINUTES [ SECONDS ] ] 30 * it doesn't specifically, say, but this sort of implies that you can't have 31 * 70 seconds. 32 */ 33 public class Duration 34 { 35 public int sign; // 1 or -1 36 public int weeks; 37 public int days; 38 public int hours; 39 public int minutes; 40 public int seconds; 41 Duration()42 public Duration() 43 { 44 sign = 1; 45 } 46 47 /** 48 * Parse according to RFC2445 ss4.3.6. (It's actually a little loose with 49 * its parsing, for better or for worse) 50 */ parse(String str)51 public void parse(String str) throws DateException 52 { 53 sign = 1; 54 weeks = 0; 55 days = 0; 56 hours = 0; 57 minutes = 0; 58 seconds = 0; 59 60 int len = str.length(); 61 int index = 0; 62 char c; 63 64 if (len < 1) { 65 return ; 66 } 67 68 c = str.charAt(0); 69 if (c == '-') { 70 sign = -1; 71 index++; 72 } 73 else if (c == '+') { 74 index++; 75 } 76 77 if (len < index) { 78 return ; 79 } 80 81 c = str.charAt(index); 82 if (c != 'P') { 83 throw new DateException ( 84 "Duration.parse(str='" + str + "') expected 'P' at index=" 85 + index); 86 } 87 index++; 88 89 int n = 0; 90 for (; index < len; index++) { 91 c = str.charAt(index); 92 if (c >= '0' && c <= '9') { 93 n *= 10; 94 n += ((c-'0')); 95 } 96 else if (c == 'W') { 97 weeks = n; 98 n = 0; 99 } 100 else if (c == 'H') { 101 hours = n; 102 n = 0; 103 } 104 else if (c == 'M') { 105 minutes = n; 106 n = 0; 107 } 108 else if (c == 'S') { 109 seconds = n; 110 n = 0; 111 } 112 else if (c == 'D') { 113 days = n; 114 n = 0; 115 } 116 else if (c == 'T') { 117 } 118 else { 119 throw new DateException ( 120 "Duration.parse(str='" + str + "') unexpected char '" 121 + c + "' at index=" + index); 122 } 123 } 124 } 125 126 /** 127 * Add this to the calendar provided, in place, in the calendar. 128 */ addTo(Calendar cal)129 public void addTo(Calendar cal) 130 { 131 cal.add(Calendar.DAY_OF_MONTH, sign*weeks*7); 132 cal.add(Calendar.DAY_OF_MONTH, sign*days); 133 cal.add(Calendar.HOUR, sign*hours); 134 cal.add(Calendar.MINUTE, sign*minutes); 135 cal.add(Calendar.SECOND, sign*seconds); 136 } 137 addTo(long dt)138 public long addTo(long dt) { 139 return dt + getMillis(); 140 } 141 getMillis()142 public long getMillis() { 143 long factor = 1000 * sign; 144 return factor * ((7*24*60*60*weeks) 145 + (24*60*60*days) 146 + (60*60*hours) 147 + (60*minutes) 148 + seconds); 149 } 150 } 151