|
| 1 | +(*---------------------------------------------------------------------------- |
| 2 | + Copyright (c) 2015 Inhabited Type LLC. |
| 3 | +
|
| 4 | + All rights reserved. |
| 5 | +
|
| 6 | + Redistribution and use in source and binary forms, with or without |
| 7 | + modification, are permitted provided that the following conditions |
| 8 | + are met: |
| 9 | +
|
| 10 | + 1. Redistributions of source code must retain the above copyright |
| 11 | + notice, this list of conditions and the following disclaimer. |
| 12 | +
|
| 13 | + 2. Redistributions in binary form must reproduce the above copyright |
| 14 | + notice, this list of conditions and the following disclaimer in the |
| 15 | + documentation and/or other materials provided with the distribution. |
| 16 | +
|
| 17 | + 3. Neither the name of the author nor the names of his contributors |
| 18 | + may be used to endorse or promote products derived from this software |
| 19 | + without specific prior written permission. |
| 20 | +
|
| 21 | + THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS |
| 22 | + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 23 | + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | + DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR |
| 25 | + ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 29 | + STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 30 | + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | + POSSIBILITY OF SUCH DAMAGE. |
| 32 | + ----------------------------------------------------------------------------*) |
| 33 | + |
| 34 | +let parse_date_exn s = |
| 35 | + try Scanf.sscanf s "%3s, %d %s %4d %d:%d:%d %s" ( |
| 36 | + fun _wday mday mon year hour min sec tz -> |
| 37 | + let months = [ |
| 38 | + "Jan", 1; "Feb", 2; "Mar", 3; "Apr", 4; "May", 5; "Jun", 6; |
| 39 | + "Jul", 7; "Aug", 8; "Sep", 9; "Oct", 10; "Nov", 11; "Dec", 12 |
| 40 | + ] in |
| 41 | + let parse_tz = function |
| 42 | + | "" | "Z" | "GMT" | "UTC" | "UT" -> 0 |
| 43 | + | "PST" -> -480 |
| 44 | + | "MST" | "PDT" -> -420 |
| 45 | + | "CST" | "MDT" -> -360 |
| 46 | + | "EST" | "CDT" -> -300 |
| 47 | + | "EDT" -> -240 |
| 48 | + | s -> Scanf.sscanf s "%c%02d%_[:]%02d" (fun sign hour min -> |
| 49 | + min + hour * (if sign = '-' then -60 else 60)) |
| 50 | + in |
| 51 | + let mon = List.assoc mon months in |
| 52 | + let year = |
| 53 | + if year < 50 then year + 2000 |
| 54 | + else if year < 1000 then year + 1900 |
| 55 | + else year |
| 56 | + in |
| 57 | + let date = (year, mon, mday) in |
| 58 | + let time = ((hour, min, sec), (parse_tz tz)*60) in |
| 59 | + let ptime = Ptime.of_date_time (date, time) in |
| 60 | + match ptime with |
| 61 | + | None -> raise (Invalid_argument "Invalid date string") |
| 62 | + | Some date -> |
| 63 | + match Ptime.(Span.to_int_s (to_span date)) with |
| 64 | + | None -> raise (Invalid_argument "Invalid date string") |
| 65 | + | Some t -> t |
| 66 | + ) |
| 67 | + with |
| 68 | + | Scanf.Scan_failure e -> raise (Invalid_argument e) |
| 69 | + | Not_found -> raise (Invalid_argument "Invalid date string") |
| 70 | + |
| 71 | +let parse_date s = |
| 72 | + try (Some (parse_date_exn s)) with |
| 73 | + | Invalid_argument _ -> None |
0 commit comments