Home | History | Annotate | Download | only in utility
      1 /* Copyright 2010, The Android Open Source Project
      2  **
      3  ** Licensed under the Apache License, Version 2.0 (the "License");
      4  ** you may not use this file except in compliance with the License.
      5  ** You may obtain a copy of the License at
      6  **
      7  **     http://www.apache.org/licenses/LICENSE-2.0
      8  **
      9  ** Unless required by applicable law or agreed to in writing, software
     10  ** distributed under the License is distributed on an "AS IS" BASIS,
     11  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12  ** See the License for the specific language governing permissions and
     13  ** limitations under the License.
     14  */
     15 
     16 package com.android.exchange.utility;
     17 
     18 import java.text.ParseException;
     19 import java.util.Calendar;
     20 
     21 /**
     22  * Note: This class was simply copied from the class in CalendarProvider, since we don't have access
     23  * to it from the Email app.  I reformated some lines, but otherwise haven't altered the code.
     24  */
     25 public class Duration {
     26     public int sign; // 1 or -1
     27     public int weeks;
     28     public int days;
     29     public int hours;
     30     public int minutes;
     31     public int seconds;
     32 
     33     public Duration() {
     34         sign = 1;
     35     }
     36 
     37     /**
     38      * Parse according to RFC2445 ss4.3.6.  (It's actually a little loose with
     39      * its parsing, for better or for worse)
     40      */
     41     public void parse(String str) throws ParseException {
     42         sign = 1;
     43         weeks = 0;
     44         days = 0;
     45         hours = 0;
     46         minutes = 0;
     47         seconds = 0;
     48 
     49         int len = str.length();
     50         int index = 0;
     51         char c;
     52 
     53         if (len < 1) {
     54             return;
     55         }
     56 
     57         c = str.charAt(0);
     58         if (c == '-') {
     59             sign = -1;
     60             index++;
     61         } else if (c == '+') {
     62             index++;
     63         }
     64 
     65         if (len < index) {
     66             return;
     67         }
     68 
     69         c = str.charAt(index);
     70         if (c != 'P') {
     71             throw new ParseException (
     72                     "Duration.parse(str='" + str + "') expected 'P' at index="
     73                     + index, index);
     74         }
     75         index++;
     76 
     77         int n = 0;
     78         for (; index < len; index++) {
     79             c = str.charAt(index);
     80             if (c >= '0' && c <= '9') {
     81                 n *= 10;
     82                 n += (c - '0');
     83             } else if (c == 'W') {
     84                 weeks = n;
     85                 n = 0;
     86             } else if (c == 'H') {
     87                 hours = n;
     88                 n = 0;
     89             } else if (c == 'M') {
     90                 minutes = n;
     91                 n = 0;
     92             } else if (c == 'S') {
     93                 seconds = n;
     94                 n = 0;
     95             } else if (c == 'D') {
     96                 days = n;
     97                 n = 0;
     98             } else if (c == 'T') {
     99             } else {
    100                 throw new ParseException (
    101                         "Duration.parse(str='" + str + "') unexpected char '"
    102                         + c + "' at index=" + index, index);
    103             }
    104         }
    105     }
    106 
    107     /**
    108      * Add this to the calendar provided, in place, in the calendar.
    109      */
    110     public void addTo(Calendar cal) {
    111         cal.add(Calendar.DAY_OF_MONTH, sign*weeks*7);
    112         cal.add(Calendar.DAY_OF_MONTH, sign*days);
    113         cal.add(Calendar.HOUR, sign*hours);
    114         cal.add(Calendar.MINUTE, sign*minutes);
    115         cal.add(Calendar.SECOND, sign*seconds);
    116     }
    117 
    118     public long addTo(long dt) {
    119         return dt + getMillis();
    120     }
    121 
    122     public long getMillis() {
    123         long factor = 1000 * sign;
    124         return factor * ((7*24*60*60*weeks) + (24*60*60*days) + (60*60*hours) + (60*minutes) +
    125                 seconds);
    126     }
    127 }
    128