class OSMTagParser {

    public static long parseDuration(String str) throws IllegalArgumentException {
        long seconds = 0;
        if (str == null)
            return 0;

        // Check for ISO_8601 format
        if (str.startsWith("P")) {
            // A common mistake is when the minutes format is intended but the month format is specified 
            // e.g. one month "P1M" is set, but on minute "PT1M" is meant.
            Duration dur;
            try {
                dur = DatatypeFactory.newInstance().newDuration(str);
                seconds = dur.getTimeInMillis(STATIC_DATE) / 1000;
            } catch (Exception ex) {
                throw new IllegalArgumentException("Cannot parse duration tag value: " + str, ex);
            }
            return seconds;
        }

        try {
            int index = str.indexOf(":");
            if (index > 0) {
                String hourStr = str.substring(0, index);
                String minStr = str.substring(index + 1);
                String secondsStr = "0";
                index = minStr.indexOf(":");
                if (index > 0) {
                    secondsStr = minStr.substring(index + 1, index + 3);
                    minStr = minStr.substring(0, index);
                }

                seconds += Integer.parseInt(hourStr) * 60L * 60;
                seconds += Integer.parseInt(minStr) * 60L;
                seconds += Integer.parseInt(secondsStr);
                return seconds;
            } else {
                // value contains minutes
                seconds = Integer.parseInt(str) * 60;
            }
        } catch (Exception ex) {
            throw new IllegalArgumentException("Cannot parse duration tag value: " + str, ex);
        }
        return seconds;
    }

}