karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject svn commit: r1443506 - in /karaf/trunk/shell/commands/src/main: java/org/apache/karaf/shell/commands/impl/ resources/META-INF/services/org/apache/karaf/shell/ resources/OSGI-INF/ resources/OSGI-INF/blueprint/
Date Thu, 07 Feb 2013 14:37:07 GMT
Author: jbonofre
Date: Thu Feb  7 14:37:06 2013
New Revision: 1443506

URL: http://svn.apache.org/viewvc?rev=1443506&view=rev
Log:
[KARAF-2078] Create the shell:date command

Added:
    karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/DateAction.java
Modified:
    karaf/trunk/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands
    karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
    karaf/trunk/shell/commands/src/main/resources/OSGI-INF/bundle.info

Added: karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/DateAction.java
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/DateAction.java?rev=1443506&view=auto
==============================================================================
--- karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/DateAction.java
(added)
+++ karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/DateAction.java
Thu Feb  7 14:37:06 2013
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.karaf.shell.commands.impl;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
+import org.apache.karaf.shell.console.AbstractAction;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+@Command(scope = "shell", name = "date", description = "Display the current time in the given
FORMAT")
+public class DateAction extends AbstractAction {
+
+    @Option(name = "-d", aliases = { "--date" }, description = "Display time described, not
now", multiValued = false, required = false)
+    private String date;
+
+    @Argument(index = 0, name = "format", description = "Output format", multiValued = false,
required = false)
+    private String format;
+
+    protected Object doExecute() throws Exception {
+        Date d;
+        if (date == null || date.equalsIgnoreCase("now")) {
+            d = new Date();
+        } else {
+            if (date.startsWith("@")) {
+                d = new Date(Long.parseLong(date.substring(1)));
+            } else {
+                d = new Date(date);
+            }
+        }
+        if (format == null) {
+            System.out.println(d);
+        } else {
+            // transform Unix format to Java SimpleDateFormat (if required)
+            format = format.replaceAll("%a","EEE");
+            format = format.replaceAll("%A", "EEEEEEE");
+            format = format.replaceAll("%b", "MMM");
+            format = format.replaceAll("%B", "MMMMMMM");
+            format = format.replaceAll("%c", "MMM EEE d HH:mm:ss yyyy");
+            format = format.replaceAll("%C","yy");
+            format = format.replaceAll("%d", "dd");
+            format = format.replaceAll("%D", "MM/dd/yy");
+            format = format.replaceAll("%e", "dd");
+            format = format.replaceAll("%F", "yyyy-MM-dd");
+            format = format.replaceAll("%g", "YY");
+            format = format.replaceAll("%G", "YYYY");
+            format = format.replaceAll("%h", "MMM");
+            format = format.replaceAll("%H", "HH");
+            format = format.replaceAll("%I", "hh");
+            format = format.replaceAll("%j", "DDD");
+            format = format.replaceAll("%k", "HH");
+            format = format.replaceAll("%l", "hh");
+            format = format.replaceAll("%m", "MM");
+            format = format.replaceAll("%M", "mm");
+            format = format.replaceAll("%n", "\n");
+            format = format.replaceAll("%N", "S");
+            format = format.replaceAll("%p", "aa");
+            format = format.replaceAll("%P", "aa");
+            format = format.replaceAll("%r", "hh:mm:ss aa");
+            format = format.replaceAll("%R", "HH:mm");
+            format = format.replaceAll("%s", "S");
+            format = format.replaceAll("%S", "ss");
+            format = format.replaceAll("%t", "\t");
+            format = format.replaceAll("%T", "HH:mm:ss");
+            format = format.replaceAll("%u", "u");
+            format = format.replaceAll("%U", "w");
+            format = format.replaceAll("%V", "W");
+            format = format.replaceAll("%w", "u");
+            format = format.replaceAll("%W", "w");
+            format = format.replaceAll("%x", "MM/dd/yy");
+            format = format.replaceAll("%X", "HH:mm:ss");
+            format = format.replaceAll("%y", "yy");
+            format = format.replaceAll("%Y", "yyyy");
+            format = format.replaceAll("%z", "X");
+            format = format.replaceAll("%:z", "XXX");
+            format = format.replaceAll("%::z", "XXXX");
+            format = format.replaceAll("%:::z", "XXXX");
+            format = format.replaceAll("%Z", "z");
+            DateFormat df = new SimpleDateFormat(format);
+            System.out.println(df.format(d));
+        }
+        return null;
+    }
+
+}

Modified: karaf/trunk/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands?rev=1443506&r1=1443505&r2=1443506&view=diff
==============================================================================
--- karaf/trunk/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands
(original)
+++ karaf/trunk/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands
Thu Feb  7 14:37:06 2013
@@ -16,6 +16,7 @@
 ##---------------------------------------------------------------------------
 org.apache.karaf.shell.commands.impl.CatAction
 org.apache.karaf.shell.commands.impl.ClearAction
+org.apache.karaf.shell.commands.impl.DateAction
 org.apache.karaf.shell.commands.impl.EachAction
 org.apache.karaf.shell.commands.impl.EchoAction
 org.apache.karaf.shell.commands.impl.ExecuteAction

Modified: karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml?rev=1443506&r1=1443505&r2=1443506&view=diff
==============================================================================
--- karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml (original)
+++ karaf/trunk/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml Thu
Feb  7 14:37:06 2013
@@ -27,6 +27,9 @@
             <action class="org.apache.karaf.shell.commands.impl.ClearAction"/>
         </command>
         <command>
+            <action class="org.apache.karaf.shell.commands.impl.DateAction"/>
+        </command>
+        <command>
             <action class="org.apache.karaf.shell.commands.impl.EachAction"/>
         </command>
         <command>

Modified: karaf/trunk/shell/commands/src/main/resources/OSGI-INF/bundle.info
URL: http://svn.apache.org/viewvc/karaf/trunk/shell/commands/src/main/resources/OSGI-INF/bundle.info?rev=1443506&r1=1443505&r2=1443506&view=diff
==============================================================================
--- karaf/trunk/shell/commands/src/main/resources/OSGI-INF/bundle.info (original)
+++ karaf/trunk/shell/commands/src/main/resources/OSGI-INF/bundle.info Thu Feb  7 14:37:06
2013
@@ -14,6 +14,7 @@ This bundle provides various shell comma
 The following commands are available:
 * shell:cat - Displays the content of a file or url.
 * shell:clear - Clears the console buffer.
+* shell:date - Display the current time in the given FORMAT
 * shell:each - Execute a closure on a list of arguments.
 * shell:echo - Echoes or prints arguments to STDOUT.
 * shell:exec - Executes system processes.



Mime
View raw message