01: /*
02: * This file is part of DrFTPD, Distributed FTP Daemon.
03: *
04: * DrFTPD is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * DrFTPD is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License
15: * along with DrFTPD; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18: package org.drftpd;
19:
20: /**
21: * @author zubov
22: * @version $Id: Time.java 852 2004-12-04 17:46:58Z mog $
23: */
24: public class Time {
25: /**
26: * @return human readable string for time.
27: */
28: public static String formatTime(long time) {
29: if (time == 0) {
30: return "0m 0s";
31: }
32:
33: // Less than an hour...
34: if (time < (60 * 60 * 1000)) {
35: long min = time / 60000;
36: long s = (time - (min * 60000)) / 1000;
37:
38: return min + "m " + ((s > 9) ? ("" + s) : (" " + s)) + "s";
39: }
40:
41: // Less than a day...
42: if (time < (24 * 60 * 60 * 1000)) {
43: long h = time / (60 * 60000);
44: long min = (time - (h * 60 * 60000)) / 60000;
45:
46: return h + "h " + ((min > 9) ? ("" + min) : (" " + min))
47: + "m";
48: }
49:
50: // Over a day...
51: long d = time / (24 * 60 * 60000);
52: long h = (time - (d * 24 * 60 * 60000)) / (60 * 60000);
53:
54: return d + "d " + ((h > 9) ? ("" + h) : (" " + h)) + "h";
55: }
56:
57: public static long parseTime(String s) {
58: s = s.toLowerCase();
59:
60: if (s.endsWith("ms")) {
61: return Long.parseLong(s.substring(0, s.length() - 2));
62: }
63:
64: if (s.endsWith("s")) {
65: return Long.parseLong(s.substring(0, s.length() - 1)) * 1000;
66: }
67:
68: if (s.endsWith("m")) {
69: return Long.parseLong(s.substring(0, s.length() - 1)) * 60000;
70: }
71:
72: return Long.parseLong(s);
73: }
74: }
|