001/** 002 * 003 * Copyright 2003-2007 Jive Software, 2014 Florian Schmaus 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.time.packet; 018 019import org.jivesoftware.smack.packet.IQ; 020import org.jxmpp.util.XmppDateTime; 021 022import java.util.Calendar; 023import java.util.Date; 024import java.util.logging.Level; 025import java.util.logging.Logger; 026 027/** 028 * A Time IQ packet, which is used by XMPP clients to exchange their respective local 029 * times. Clients that wish to fully support the entity time protocol should register 030 * a PacketListener for incoming time requests that then respond with the local time. 031 * 032 * @see <a href="http://www.xmpp.org/extensions/xep-0202.html">XEP-202</a> 033 * @author Florian Schmaus 034 */ 035public class Time extends IQ { 036 public static final String NAMESPACE = "urn:xmpp:time"; 037 public static final String ELEMENT = "time"; 038 039 private static final Logger LOGGER = Logger.getLogger(Time.class.getName()); 040 041 private String utc; 042 private String tzo; 043 044 public Time() { 045 super(ELEMENT, NAMESPACE); 046 setType(Type.get); 047 } 048 049 /** 050 * Creates a new Time instance using the specified calendar instance as 051 * the time value to send. 052 * 053 * @param cal the time value. 054 */ 055 public Time(Calendar cal) { 056 super(ELEMENT, NAMESPACE); 057 tzo = XmppDateTime.asString(cal.getTimeZone()); 058 // Convert local time to the UTC time. 059 utc = XmppDateTime.formatXEP0082Date(cal.getTime()); 060 } 061 062 /** 063 * Returns the local time or <tt>null</tt> if the time hasn't been set. 064 * 065 * @return the local time. 066 */ 067 public Date getTime() { 068 if (utc == null) { 069 return null; 070 } 071 Date date = null; 072 try { 073 date = XmppDateTime.parseDate(utc); 074 } 075 catch (Exception e) { 076 LOGGER.log(Level.SEVERE, "Error getting local time", e); 077 } 078 return date; 079 } 080 081 /** 082 * Sets the time using the local time. 083 * 084 * @param time the current local time. 085 */ 086 public void setTime(Date time) { 087 } 088 089 /** 090 * Returns the time as a UTC formatted String using the format CCYY-MM-DDThh:mm:ssZ. 091 * 092 * @return the time as a UTC formatted String. 093 */ 094 public String getUtc() { 095 return utc; 096 } 097 098 /** 099 * Sets the time using UTC formatted String in the format CCYY-MM-DDThh:mm:ssZ. 100 * 101 * @param utc the time using a formatted String. 102 */ 103 public void setUtc(String utc) { 104 this.utc = utc; 105 } 106 107 /** 108 * Returns the time zone. 109 * 110 * @return the time zone. 111 */ 112 public String getTzo() { 113 return tzo; 114 } 115 116 /** 117 * Sets the time zone offset. 118 * 119 * @param tzo the time zone offset. 120 */ 121 public void setTzo(String tzo) { 122 this.tzo = tzo; 123 } 124 125 public static Time createResponse(IQ request) { 126 Time time = new Time(Calendar.getInstance()); 127 time.setType(Type.result); 128 time.setTo(request.getFrom()); 129 return time; 130 } 131 132 @Override 133 protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) { 134 if (utc != null) { 135 buf.rightAngleBracket(); 136 buf.append("<utc>").append(utc).append("</utc>"); 137 buf.append("<tzo>").append(tzo).append("</tzo>"); 138 } else { 139 buf.setEmptyElement(); 140 } 141 142 return buf; 143 } 144}