src: Include <libxml/parser.h>
[virt-top.git] / src / opt_calendar.ml
1 (* 'top'-like tool for libvirt domains.
2    (C) Copyright 2007-2009 Richard W.M. Jones, Red Hat Inc.
3    http://libvirt.org/
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19    This file contains all code which requires ocaml-calendar >= 2.0
20 *)
21
22 open CalendarLib
23
24 open Printf
25
26 open Opt_gettext.Gettext ;;
27
28 Top.parse_date_time :=
29 fun time ->
30   let cal : Calendar.t =
31     (* time is "+something" *)
32     let is_plus =
33       let n = String.length time in
34       n >= 1 && time.[0] = '+'
35     in
36     if is_plus then (
37       let period = String.sub time 1 (String.length time - 1) in
38       let period =
39         if String.contains period ':' then ( (* +HH:MM:SS *)
40           let t = Printer.TimePrinter.from_string period in
41           let hh = Time.hour t and mm = Time.minute t and ss = Time.second t in
42           Calendar.Period.make 0 0 0 hh mm ss
43         ) else                          (* +seconds *)
44           Calendar.Period.second (int_of_string period) in
45       (* Add it as an offset from the current time.
46        *
47        * Note that the default for the Calendar library is to return
48        * Calendar.now in the UTC time zone, which is in fact what we
49        * need below.
50        *)
51       Calendar.add (Calendar.now ()) period
52     ) else (
53       let cal =
54         if String.contains time '-' then (* YYYY-MM-DD HH:MM:SS *)
55           Printer.CalendarPrinter.from_string time
56         else (                           (* HH:MM:SS *)
57           let time = Printer.TimePrinter.from_string time in
58           Calendar.create (Date.today ()) time
59         ) in
60       (* Assume the user has entered a local time.  Convert it to
61        * UTC time zone which is what we need below.  (RHBZ#680344)
62        *)
63       Calendar.convert cal Time_Zone.Local Time_Zone.UTC
64     ) in
65
66   eprintf "end time (UTC): %s\n" (Printer.CalendarPrinter.to_string cal);
67
68   (* Convert to a time_t.  Note that we compare this against
69    * Unix.gettimeofday in the main module, so this must be returned as
70    * plain seconds from 1970 with no timezone adjustment.  (RHBZ#637964)
71    *)
72   Calendar.to_unixfloat cal