Search in sources :

Example 1 with TZ

use of com.zimbra.common.calendar.TZIDMapper.TZ in project zm-mailbox by Zimbra.

the class WellKnownTimeZones method loadFromFile.

/**
     * Should be called once at server start
     * @param tzFile
     * @throws IOException
     * @throws ServiceException
     */
public static void loadFromFile(File tzFile) throws IOException, ServiceException {
    FileInputStream fis = null;
    ZVCalendar tzs = null;
    try {
        fis = new FileInputStream(tzFile);
        tzs = ZCalendar.ZCalendarBuilder.build(new FileInputStream(tzFile), MimeConstants.P_CHARSET_UTF8);
    } finally {
        if (fis != null)
            fis.close();
    }
    for (Iterator<ZComponent> compIter = tzs.getComponentIterator(); compIter.hasNext(); ) {
        ZComponent tzComp = compIter.next();
        if (!ICalTok.VTIMEZONE.equals(tzComp.getTok()))
            continue;
        ICalTimeZone tz = ICalTimeZone.fromVTimeZone(tzComp, true, TZID_NAME_ASSIGNMENT_BEHAVIOR.ALWAYS_KEEP);
        sTZIDMap.put(tz.getID(), tz);
    }
    // Add aliases from TZIDMapper.  Build map of TZID to match score.
    Map<String, Integer> matchScoreMap = new HashMap<String, Integer>();
    for (Iterator<TZIDMapper.TZ> tzIter = TZIDMapper.iterator(false); tzIter.hasNext(); ) {
        TZIDMapper.TZ tz = tzIter.next();
        String id = tz.getID();
        matchScoreMap.put(id, tz.getMatchScore());
        ICalTimeZone itz = getTimeZoneById(id);
        if (itz != null) {
            String[] aliases = tz.getAliases();
            if (aliases != null) {
                for (String alias : aliases) {
                    addAlias(itz, alias);
                }
            }
        }
    }
    // the highest match score is added to the map.
    for (Iterator<TZIDMapper.TZ> tzIter = TZIDMapper.iterator(false); tzIter.hasNext(); ) {
        TZIDMapper.TZ tz = tzIter.next();
        String id = tz.getID();
        ICalTimeZone itz = getTimeZoneById(id);
        if (itz != null) {
            ICalTimeZone current = sOffsetRuleMatches.get(itz);
            if (current == null) {
                sOffsetRuleMatches.put(itz, itz);
            } else {
                String currentId = current.getID();
                int currentMatchScore = matchScoreMap.containsKey(currentId) ? matchScoreMap.get(currentId) : 0;
                // Higher score wins.  In a tie, the TZID that comes earlier lexicographically wins.
                if (currentMatchScore < tz.getMatchScore() || (currentMatchScore == tz.getMatchScore() && currentId.compareTo(id) > 0)) {
                    sOffsetRuleMatches.remove(itz);
                    sOffsetRuleMatches.put(itz, itz);
                }
            }
            current = fuzzyOffsetRuleMatches.get(itz);
            if (current == null) {
                fuzzyOffsetRuleMatches.put(itz, itz);
            } else {
                String currentId = current.getID();
                int currentMatchScore = matchScoreMap.containsKey(currentId) ? matchScoreMap.get(currentId) : 0;
                // Higher score wins.  In a tie, the TZID that comes earlier lexicographically wins.
                if (currentMatchScore < tz.getMatchScore() || (currentMatchScore == tz.getMatchScore() && currentId.compareTo(id) > 0)) {
                    fuzzyOffsetRuleMatches.remove(itz);
                    fuzzyOffsetRuleMatches.put(itz, itz);
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) TZ(com.zimbra.common.calendar.TZIDMapper.TZ) FileInputStream(java.io.FileInputStream) ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) TZ(com.zimbra.common.calendar.TZIDMapper.TZ)

Example 2 with TZ

use of com.zimbra.common.calendar.TZIDMapper.TZ in project zm-mailbox by Zimbra.

the class WellKnownTimeZones method main.

public static void main(String[] args) throws Exception {
    String tzFilePath = LC.timezone_file.value();
    File tzFile = new File(tzFilePath);
    WellKnownTimeZones.loadFromFile(tzFile);
    System.out.println("OFFSET/RULE MATCH TIME ZONES");
    System.out.println("----------------------------");
    for (ICalTimeZone key : sOffsetRuleMatches.keySet()) {
        String id = key.getID();
        boolean inFuzzy = false;
        for (ICalTimeZone fuzzykey : fuzzyOffsetRuleMatches.keySet()) {
            if (id.equals(fuzzykey.getID())) {
                inFuzzy = true;
                break;
            }
        }
        if (inFuzzy) {
            System.out.println(key.getID());
        } else {
            System.out.println(key.getID() + " [missing from fuzzyOffsetRuleMatches]");
        }
    }
    System.out.println("(Total = " + sOffsetRuleMatches.size() + ") (fuzzyTotal = " + fuzzyOffsetRuleMatches.size() + ")");
    System.out.println();
    int nTotal = 0, nPrim = 0, nNonPrim = 0;
    for (Iterator<TZ> tziter = TZIDMapper.iterator(false); tziter.hasNext(); ) {
        nTotal++;
        TZ t = tziter.next();
        if (t.isPrimary()) {
            nPrim++;
            ICalTimeZone tz = getTimeZoneById(t.getID());
            ICalTimeZone match = sOffsetRuleMatches.get(tz);
            if (match == null)
                System.out.println("sOffsetRuleMatches map is missing primary TZ: " + tz.getID());
            else if (!match.getID().equals(tz.getID()))
                System.out.println("Mismatch for primary TZ: " + tz.getID() + " (map has " + match.getID() + ")");
        } else
            nNonPrim++;
    }
    System.out.println("num primary in TZIDMapper     = " + nPrim);
    System.out.println("num non-primary in TZIDMapper = " + nNonPrim);
    System.out.println("num total in TZIDMapper       = " + nTotal);
    System.out.println();
    ICalTimeZone iPhonePacific = ICalTimeZone.lookup("GMT-08.00/-07.00", -28800000, "16010101T020000", "FREQ=YEARLY;INTERVAL=1;BYMONTH=11;BYDAY=1SU;WKST=MO", "PST", -25200000, "16010101T020000", "FREQ=YEARLY;INTERVAL=1;BYMONTH=3;BYDAY=2SU;WKST=MO", "PDT");
    System.out.println("iPhone Pacific: " + iPhonePacific.getID());
    ICalTimeZone primaryPacific = sOffsetRuleMatches.get(iPhonePacific);
    System.out.println("Primary Pacific: " + primaryPacific.getID());
}
Also used : TZ(com.zimbra.common.calendar.TZIDMapper.TZ) File(java.io.File)

Aggregations

TZ (com.zimbra.common.calendar.TZIDMapper.TZ)2 ZComponent (com.zimbra.common.calendar.ZCalendar.ZComponent)1 ZVCalendar (com.zimbra.common.calendar.ZCalendar.ZVCalendar)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 HashMap (java.util.HashMap)1