karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject svn commit: r1443560 - in /karaf/branches/karaf-2.3.x/shell/commands/src/main: java/org/apache/karaf/shell/commands/ resources/META-INF/services/org/apache/karaf/shell/ resources/OSGI-INF/ resources/OSGI-INF/blueprint/
Date Thu, 07 Feb 2013 15:36:27 GMT
Author: jbonofre
Date: Thu Feb  7 15:36:26 2013
New Revision: 1443560

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

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

Added: karaf/branches/karaf-2.3.x/shell/commands/src/main/java/org/apache/karaf/shell/commands/DateAction.java
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.3.x/shell/commands/src/main/java/org/apache/karaf/shell/commands/DateAction.java?rev=1443560&view=auto
==============================================================================
--- karaf/branches/karaf-2.3.x/shell/commands/src/main/java/org/apache/karaf/shell/commands/DateAction.java
(added)
+++ karaf/branches/karaf-2.3.x/shell/commands/src/main/java/org/apache/karaf/shell/commands/DateAction.java
Thu Feb  7 15:36:26 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;
+
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+import org.apache.felix.gogo.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/branches/karaf-2.3.x/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.3.x/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands?rev=1443560&r1=1443559&r2=1443560&view=diff
==============================================================================
--- karaf/branches/karaf-2.3.x/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands
(original)
+++ karaf/branches/karaf-2.3.x/shell/commands/src/main/resources/META-INF/services/org/apache/karaf/shell/commands
Thu Feb  7 15:36:26 2013
@@ -16,6 +16,7 @@
 ##---------------------------------------------------------------------------
 org.apache.karaf.shell.commands.CatAction
 org.apache.karaf.shell.commands.ClearAction
+org.apache.karaf.shell.commands.DateAction
 org.apache.karaf.shell.commands.EachAction
 org.apache.karaf.shell.commands.EchoAction
 org.apache.karaf.shell.commands.ExecuteAction

Modified: karaf/branches/karaf-2.3.x/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.3.x/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml?rev=1443560&r1=1443559&r2=1443560&view=diff
==============================================================================
--- karaf/branches/karaf-2.3.x/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
(original)
+++ karaf/branches/karaf-2.3.x/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
Thu Feb  7 15:36:26 2013
@@ -26,6 +26,9 @@
         <command name="shell/clear">
             <action class="org.apache.karaf.shell.commands.ClearAction"/>
         </command>
+        <command name="shell/date">
+            <action class="org.apache.karaf.shell.commands.DateAction"/>
+        </command>
         <command name="shell/each">
             <action class="org.apache.karaf.shell.commands.EachAction"/>
         </command>

Modified: karaf/branches/karaf-2.3.x/shell/commands/src/main/resources/OSGI-INF/bundle.info
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.3.x/shell/commands/src/main/resources/OSGI-INF/bundle.info?rev=1443560&r1=1443559&r2=1443560&view=diff
==============================================================================
--- karaf/branches/karaf-2.3.x/shell/commands/src/main/resources/OSGI-INF/bundle.info (original)
+++ karaf/branches/karaf-2.3.x/shell/commands/src/main/resources/OSGI-INF/bundle.info Thu
Feb  7 15:36:26 2013
@@ -12,6 +12,7 @@
     The following commands are available:
     \u001B[36mshell:cat\u001B[0m Displays the content of a file or url.
     \u001B[36mshell:clear\u001B[0m Clears the console buffer.
+    \u001B[36mshell:date\u001B[0m Display the current time in the given FORMAT
     \u001B[36mshell:each\u001B[0m Execute a closure on a list of arguments.
     \u001B[36mshell:echo\u001B[0m Echoes or prints arguments to STDOUT.
     \u001B[36mshell:exec\u001B[0m Executes system processes.



Mime
View raw message