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