public static Duration fromIso(string s, bool check) { try { long ticks = 0; bool neg = false; IsoParser p = new IsoParser(s); // check for negative if (p.cur == '-') { neg = true; p.consume(); } else if (p.cur == '+') { p.consume(); } // next char must be P p.consume('P'); if (p.cur == -1) { throw new System.Exception(); } // D int num = 0; if (p.cur != 'T') { num = p.num(); p.consume('D'); ticks += num * nsPerDay; if (p.cur == -1) { return(new Duration(ticks)); } } // next char must be T p.consume('T'); if (p.cur == -1) { throw new System.Exception(); } num = p.num(); // H if (num >= 0 && p.cur == 'H') { p.consume(); ticks += num * nsPerHr; num = p.num(); } // M if (num >= 0 && p.cur == 'M') { p.consume(); ticks += num * nsPerMin; num = p.num(); } // S if (num >= 0 && p.cur == 'S' || p.cur == '.') { ticks += num * nsPerSec; if (p.cur == '.') { p.consume(); ticks += p.frac(); } p.consume('S'); } // verify we parsed everything if (p.cur != -1) { throw new System.Exception(); } // negate if necessary and return result if (neg) { ticks = -ticks; } return(new Duration(ticks)); } catch (System.Exception) { if (!check) { return(null); } throw ParseErr.make("ISO 8601 Duration", s).val; } }
public static Duration fromIso(string s, bool check) { try { long ticks = 0; bool neg = false; IsoParser p = new IsoParser(s); // check for negative if (p.cur == '-') { neg = true; p.consume(); } else if (p.cur == '+') { p.consume(); } // next char must be P p.consume('P'); if (p.cur == -1) throw new System.Exception(); // D int num = 0; if (p.cur != 'T') { num = p.num(); p.consume('D'); ticks += num * nsPerDay; if (p.cur == -1) return new Duration(ticks); } // next char must be T p.consume('T'); if (p.cur == -1) throw new System.Exception(); num = p.num(); // H if (num >= 0 && p.cur == 'H') { p.consume(); ticks += num * nsPerHr; num = p.num(); } // M if (num >= 0 && p.cur == 'M') { p.consume(); ticks += num * nsPerMin; num = p.num(); } // S if (num >= 0 && p.cur == 'S' || p.cur == '.') { ticks += num * nsPerSec; if (p.cur == '.') { p.consume(); ticks += p.frac(); } p.consume('S'); } // verify we parsed everything if (p.cur != -1) throw new System.Exception(); // negate if necessary and return result if (neg) ticks = -ticks; return new Duration(ticks); } catch(System.Exception) { if (!check) return null; throw ParseErr.make("ISO 8601 Duration", s).val; } }