blob: ef381d4d9eaf973eb36b2b49dd9e439178bc4599 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package de.thedevstack.conversationsplus.xmpp.time;
import java.util.Locale;
import java.util.TimeZone;
import de.thedevstack.conversationsplus.generator.AbstractGenerator;
import de.thedevstack.conversationsplus.xml.Element;
import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket;
import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacketGenerator;
/**
* Generates the IQ Packets for Entity Time
* as defined in XEP-0202.
* @see <a href="http://xmpp.org/extensions/xep-0202.html">http://xmpp.org/extensions/xep-0202.html</a>
*/
public final class TimePacketGenerator {
/**
* Generates the IqPacket to reply the entity time.
* <pre>
* <iq type='result'
* from='juliet@capulet.com/balcony'
* to='romeo@montague.net/orchard'
* id='time_1'>
* <time xmlns='urn:xmpp:time'>
* <tzo>-06:00</tzo>
* <utc>2006-12-19T17:58:35Z</utc>
* </time>
* </iq>
* </pre>
* @param packet
* @return
*/
public static IqPacket generateResponse(IqPacket packet) {
IqPacket responsePacket = IqPacketGenerator.generateIqResultResponse(packet);
Element time = responsePacket.addChild(EntityTime.ELEMENT, EntityTime.NAMESPACE);
final long now = System.currentTimeMillis();
time.addChild("utc").setContent(AbstractGenerator.getTimestamp(now));
TimeZone ourTimezone = TimeZone.getDefault();
long offsetSeconds = ourTimezone.getOffset(now) / 1000;
long offsetMinutes = Math.abs((offsetSeconds % 3600) / 60);
long offsetHours = offsetSeconds / 3600;
String hours;
if (offsetHours < 0) {
hours = String.format(Locale.US, "%03d", offsetHours);
} else {
hours = String.format(Locale.US, "%02d", offsetHours);
}
String minutes = String.format(Locale.US, "%02d", offsetMinutes);
time.addChild("tzo").setContent(hours + ":" + minutes);
return responsePacket;
}
private TimePacketGenerator() {}
}
|