Updated virt-top.txt.
[virt-top.git] / virt-top / virt_top_calendar.ml
1 (* 'top'-like tool for libvirt domains.
2    (C) Copyright 2007 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.
20 *)
21
22 open Printf
23 open ExtString ;;
24
25 Virt_top.parse_date_time :=
26 fun time ->
27   let cal : Calendar.t =
28     if String.starts_with time "+" then ( (* +something *)
29       let period = String.sub time 1 (String.length time - 1) in
30       let period =
31         if String.contains period ':' then ( (* +HH:MM:SS *)
32           let t = Printer.TimePrinter.from_string period in
33           let hh = Time.hour t and mm = Time.minute t and ss = Time.second t in
34           Calendar.Period.make 0 0 0 hh mm ss
35         ) else                          (* +seconds *)
36           Calendar.Period.second (int_of_string period) in
37       (* Add it as an offset from the current time. *)
38       Calendar.add (Calendar.now ()) period
39     ) else (
40       if String.contains time '-' then  (* YYYY-MM-DD HH:MM:SS *)
41         Printer.CalendarPrinter.from_string time
42       else (                            (* HH:MM:SS *)
43         let time = Printer.TimePrinter.from_string time in
44         Calendar.create (Date.today ()) time
45       )
46     ) in
47
48   eprintf "end time: %s\n" (Printer.CalendarPrinter.to_string cal);
49
50   (* Convert to a time_t, adjusting for the current timezone. *)
51   fst (Unix.mktime (Calendar.to_unixtm cal))