(* Utility functions. *) open CalendarLib open Printf let rec find s sub = let len = String.length s in let sublen = String.length sub in let rec loop i = if i <= len-sublen then ( let rec loop2 j = if j < sublen then ( if s.[i+j] = sub.[j] then loop2 (j+1) else -1 ) else i (* found *) in let r = loop2 0 in if r = -1 then loop (i+1) else r ) else -1 (* not found *) in loop 0 let rec nsplit sep str = let len = String.length str in let seplen = String.length sep in let i = find str sep in if i = -1 then [str] else ( let s' = String.sub str 0 i in let s'' = String.sub str (i+seplen) (len-i-seplen) in s' :: nsplit sep s'' ) let error fs = let display str = prerr_string "todo: error: "; prerr_endline str; exit 1 in ksprintf display fs let string_of_estimate period = (* We ignore the seconds, and assume every estimate is >= 1 day. *) let (years, months, days, _) = Calendar.Period.ymds period in let buf = Buffer.create 13 in let append str = if Buffer.length buf > 0 then Buffer.add_string buf ", "; Buffer.add_string buf str in if years >= 2 then append (sprintf "%d years" years) else if years = 1 then append "1 year"; if months >= 2 then append (sprintf "%d months" months) else if months = 1 then append "1 month"; if days >= 2 then append (sprintf "%d days" days) else if days = 1 then append "1 day"; let str = Buffer.contents buf in if str <> "" then str else "-"