/*
* Time.java
*
* Created on 20.02.2005, 13:03
*
* Copyright (c) 2005-2007, Eugene Stahov (evgs), http://bombus-im.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* You can also redistribute and/or modify this program under the
* terms of the Psi License, specified in the accompanied COPYING
* file, as published by the Psi Project; either dated January 1st,
* 2005, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package ui;
//
import Client.Config;
import locale.SR;
//
import java.util.*;
/**
*
* @author Eugene Stahov
*/
public class Time {
private static Calendar c=Calendar.getInstance( TimeZone.getTimeZone("GMT") );
private static long utcToLocalOffsetMillis=0;
private static long fixupLocalOffsetMills=0;
private static int tzo=0;
/** Creates a new instance of Time */
private Time() { }
public static void setOffset(int tzOffset, int locOffset){
utcToLocalOffsetMillis=((long)tzOffset)*60*60*1000;
fixupLocalOffsetMills=((long)locOffset)*60*60*1000;
tzo=tzOffset;
}
public static String lz2(int i){
if (i<10) return "0"+i; else return String.valueOf(i);
}
public static String timeLocalString(long date){
Calendar c=calDate(date);
return lz2(c.get(Calendar.HOUR_OF_DAY))+':'+lz2(c.get(Calendar.MINUTE));
}
private static Calendar calDate(long date){
c.setTime(new Date(date+utcToLocalOffsetMillis));
return c;
}
public static String dayLocalString(long date){
Calendar c=calDate(date);
return util.strconv.stringReplace((lz2(c.get(Calendar.DAY_OF_MONTH))+'.'+
lz2(c.get(Calendar.MONTH)+1)+'.'+
lz2(c.get(Calendar.YEAR) % 100)+" "), "01.04", "32.03");
}
public static long utcTimeMillis(){
return System.currentTimeMillis()+fixupLocalOffsetMills;
}
public static String Xep0082UtcTime(){
long date=utcTimeMillis();
c.setTime(new Date(date));
return util.strconv.stringReplace((String.valueOf(c.get(Calendar.YEAR))+
lz2(c.get(Calendar.MONTH)+1)+
lz2(c.get(Calendar.DAY_OF_MONTH))+
'T' + lz2(c.get(Calendar.HOUR_OF_DAY))+':'+lz2(c.get(Calendar.MINUTE))+':'+lz2(c.get(Calendar.SECOND))), "0401", "0332");
}
public static String utcTime() {
long date=utcTimeMillis();
c.setTime(new Date(date));
return util.strconv.stringReplace((String.valueOf(c.get(Calendar.YEAR)) +
'-' + lz2(c.get(Calendar.MONTH)+1) +
'-' + lz2(c.get(Calendar.DAY_OF_MONTH)) +
'T' + lz2(c.get(Calendar.HOUR_OF_DAY))+':'+lz2(c.get(Calendar.MINUTE))+':'+lz2(c.get(Calendar.SECOND)) +
'Z'), "04-01", "03-32");
}
public static String tzOffset(){
StringBuffer tz=new StringBuffer();
int tzi=tzo;
char sign='+';
if (tzo<0) { sign='-'; tzi=-tzo; }
tz.append(sign).append(lz2(tzi)).append(":00");
return tz.toString();
}
public static String dispLocalTime(){
long utcDate=utcTimeMillis();
//Calendar c=calDate(date);
return dayLocalString(utcDate)+timeLocalString(utcDate);
}
private final static int[] calFields=
{Calendar.YEAR, Calendar.MONTH, Calendar.DATE,
Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND};
private final static int[] ofsFieldsA=
{ 0, 4, 6, 9, 12, 15 } ; //XEP-0091 - DEPRECATED
private final static int[] ofsFieldsB=
{ 0, 5, 8, 11, 14, 17 } ;//XEP-0203
private final static int[] BombusDateTimeFields= //<+voffk: parsing strings like "dd.mm.yy hh:mm">
{ 6, 3, 0, 9, 12, 14 };
public static long dateIso8601(String sdate){ //<*voffk>
int[] ofs=ofsFieldsA;
int l=4; // yearlen
if (sdate.endsWith("Z")) {
ofs=ofsFieldsB;
}
if (sdate.length()==14) {
ofs=BombusDateTimeFields;
l=2; // yearlen
}
try {
for (int i=0; i
public static String localTime(){
return timeLocalString(utcTimeMillis());
}
public static String localDate(){
return dayLocalString(utcTimeMillis()).trim();
}
public static String secDiffToDate(int seconds){
String result ="";
int d = 0,h = 0,m = 0,s = 0;
if (seconds>86400){
d=(seconds/86400);
seconds=seconds-(d*86400);
}
if (seconds>3600){
h=(seconds/3600);
seconds=seconds-(h*3600);
}
if (seconds>60){
m=(seconds/60);
seconds=seconds-(m*60);
}
s=seconds;
if (d>0) {
result+= d + " " + goodWordForm (d,3);
}
if (h>0) {
if (d>0) result+=", ";
result+= h + " " + goodWordForm (h, 2);
}
if (m>0) {
if ((d>0) || (h>0)) result+=", ";
result+= m + " " + goodWordForm (m, 1);
}
if (s>0) {
if ((d>0) || (h>0) || (m>0)) result+=", ";
result+= s + " " + goodWordForm (s, 0);
}
if (result=="" && s==0)
result=s + " " + goodWordForm (s, 0);
return result;
}
public static String goodWordForm (int d, int field) {
String [][] suf = {
{SR.MS_SEC1, SR.MS_SEC2, SR.MS_SEC3},
{SR.MS_MIN1, SR.MS_MIN2, SR.MS_MIN3},
{SR.MS_HOUR1, SR.MS_HOUR2, SR.MS_HOUR3},
{SR.MS_DAY1, SR.MS_DAY2, SR.MS_DAY3},
};
int index;
if ((d%100>10) && (d%100<20) || (d%10==0) || (d%10>4))
index=2;
else if ((d%10>1) && (d%10<5))
index=1;
else
index=0;
return suf[field][index];
}
//
}