Time.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software, 2014 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.time.packet;

  18. import org.jivesoftware.smack.packet.IQ;
  19. import org.jxmpp.util.XmppDateTime;

  20. import java.util.Calendar;
  21. import java.util.Date;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;

  24. /**
  25.  * A Time IQ packet, which is used by XMPP clients to exchange their respective local
  26.  * times. Clients that wish to fully support the entity time protocol should register
  27.  * a PacketListener for incoming time requests that then respond with the local time.
  28.  *
  29.  * @see <a href="http://www.xmpp.org/extensions/xep-0202.html">XEP-202</a>
  30.  * @author Florian Schmaus
  31.  */
  32. public class Time extends IQ {
  33.     public static final String NAMESPACE = "urn:xmpp:time";
  34.     public static final String ELEMENT = "time";

  35.     private static final Logger LOGGER = Logger.getLogger(Time.class.getName());

  36.     private String utc;
  37.     private String tzo;

  38.     public Time() {
  39.         super(ELEMENT, NAMESPACE);
  40.         setType(Type.get);
  41.     }

  42.     /**
  43.      * Creates a new Time instance using the specified calendar instance as
  44.      * the time value to send.
  45.      *
  46.      * @param cal the time value.
  47.      */
  48.     public Time(Calendar cal) {
  49.         super(ELEMENT, NAMESPACE);
  50.         tzo = XmppDateTime.asString(cal.getTimeZone());
  51.         // Convert local time to the UTC time.
  52.         utc = XmppDateTime.formatXEP0082Date(cal.getTime());
  53.     }

  54.     /**
  55.      * Returns the local time or <tt>null</tt> if the time hasn't been set.
  56.      *
  57.      * @return the local time.
  58.      */
  59.     public Date getTime() {
  60.         if (utc == null) {
  61.             return null;
  62.         }
  63.         Date date = null;
  64.         try {
  65.             date = XmppDateTime.parseDate(utc);
  66.         }
  67.         catch (Exception e) {
  68.             LOGGER.log(Level.SEVERE, "Error getting local time", e);
  69.         }
  70.         return date;
  71.     }

  72.     /**
  73.      * Sets the time using the local time.
  74.      *
  75.      * @param time the current local time.
  76.      */
  77.     public void setTime(Date time) {
  78.     }

  79.     /**
  80.      * Returns the time as a UTC formatted String using the format CCYY-MM-DDThh:mm:ssZ.
  81.      *
  82.      * @return the time as a UTC formatted String.
  83.      */
  84.     public String getUtc() {
  85.         return utc;
  86.     }

  87.     /**
  88.      * Sets the time using UTC formatted String in the format CCYY-MM-DDThh:mm:ssZ.
  89.      *
  90.      * @param utc the time using a formatted String.
  91.      */
  92.     public void setUtc(String utc) {
  93.         this.utc = utc;
  94.     }

  95.     /**
  96.      * Returns the time zone.
  97.      *
  98.      * @return the time zone.
  99.      */
  100.     public String getTzo() {
  101.         return tzo;
  102.     }

  103.     /**
  104.      * Sets the time zone offset.
  105.      *
  106.      * @param tzo the time zone offset.
  107.      */
  108.     public void setTzo(String tzo) {
  109.         this.tzo = tzo;
  110.     }

  111.     public static Time createResponse(IQ request) {
  112.         Time time = new Time(Calendar.getInstance());
  113.         time.setType(Type.result);
  114.         time.setTo(request.getFrom());
  115.         return time;
  116.     }

  117.     @Override
  118.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  119.         buf.rightAngleBracket();

  120.         if (utc != null) {
  121.             buf.append("<utc>").append(utc).append("</utc>");
  122.             buf.append("<tzo>").append(tzo).append("</tzo>");
  123.         } else {
  124.             buf.setEmptyElement();
  125.         }

  126.         return buf;
  127.     }
  128. }