Search in sources :

Example 1 with MyLocale

use of com.actiontech.dble.plan.common.locale.MyLocale in project dble by actiontech.

the class MyTime method makeDateTime.

/**
 * Create a formated date/time value in a string.
 *
 * @return
 */
public static boolean makeDateTime(DateTimeFormat format, MySQLTime lTime, MySQLTimestampType type, StringPtr strPtr) {
    int hoursI;
    int weekday;
    int ptr = 0, end;
    char[] formatChars = format.getFormat().toCharArray();
    StringBuilder str = new StringBuilder();
    MyLocale locale = MyLocales.MY_LOCALE_EN_US;
    if (lTime.isNeg())
        str.append('-');
    end = format.getFormat().length();
    for (; ptr != end; ptr++) {
        if (formatChars[ptr] != '%' || ptr + 1 == end)
            str.append(formatChars[ptr]);
        else {
            switch(formatChars[++ptr]) {
                case 'M':
                    if (lTime.getMonth() == 0) {
                        strPtr.set(str.toString());
                        return true;
                    }
                    str.append(locale.getMonthNames().getTypeNames()[(int) (lTime.getMonth() - 1)]);
                    break;
                case 'b':
                    if (lTime.getMonth() == 0) {
                        strPtr.set(str.toString());
                        return true;
                    }
                    str.append(locale.getAbMonthNames().getTypeNames()[(int) (lTime.getMonth() - 1)]);
                    break;
                case 'W':
                    if (type == MySQLTimestampType.MYSQL_TIMESTAMP_TIME || (lTime.getMonth() == 0 && lTime.getYear() == 0)) {
                        strPtr.set(str.toString());
                        return true;
                    }
                    weekday = MyTime.calcWeekday(MyTime.calcDaynr(lTime.getYear(), lTime.getMonth(), lTime.getDay()), false);
                    str.append(locale.getDayNames().getTypeNames()[weekday]);
                    break;
                case 'a':
                    if (type == MySQLTimestampType.MYSQL_TIMESTAMP_TIME || (lTime.getMonth() == 0 && lTime.getYear() == 0)) {
                        strPtr.set(str.toString());
                        return true;
                    }
                    weekday = MyTime.calcWeekday(MyTime.calcDaynr(lTime.getYear(), lTime.getMonth(), lTime.getDay()), false);
                    str.append(locale.getAbDayNames().getTypeNames()[weekday]);
                    break;
                case 'D':
                    if (type == MySQLTimestampType.MYSQL_TIMESTAMP_TIME) {
                        strPtr.set(str.toString());
                        return true;
                    }
                    str.append(String.format("%01d", lTime.getDay()));
                    if (lTime.getDay() >= 10 && lTime.getDay() <= 19)
                        str.append("th");
                    else {
                        int tmp = (int) (lTime.getDay() % 10);
                        switch(tmp) {
                            case 1:
                                str.append("st");
                                break;
                            case 2:
                                str.append("nd");
                                break;
                            case 3:
                                str.append("rd");
                                break;
                            default:
                                str.append("th");
                                break;
                        }
                    }
                    break;
                case 'Y':
                    str.append(String.format("%04d", lTime.getYear()));
                    break;
                case 'y':
                    str.append(String.format("%02d", lTime.getYear()));
                    break;
                case 'm':
                    str.append(String.format("%02d", lTime.getMonth()));
                    break;
                case 'c':
                    str.append(String.format("%01d", lTime.getMonth()));
                    break;
                case 'd':
                    str.append(String.format("%02d", lTime.getDay()));
                    break;
                case 'e':
                    str.append(String.format("%01d", lTime.getDay()));
                    break;
                case 'f':
                    str.append(String.format("%06d", lTime.getSecondPart()));
                    break;
                case 'H':
                    str.append(String.format("%02d", lTime.getHour()));
                    break;
                case 'h':
                case 'I':
                    hoursI = (int) ((lTime.getHour() % 24 + 11) % 12 + 1);
                    str.append(String.format("%02d", hoursI));
                    break;
                case 'i':
                    /* minutes */
                    str.append(String.format("%02d", lTime.getMinute()));
                    break;
                case 'j':
                    if (type == MySQLTimestampType.MYSQL_TIMESTAMP_TIME) {
                        strPtr.set(str.toString());
                        return true;
                    }
                    str.append(String.format("%03d", MyTime.calcDaynr(lTime.getYear(), lTime.getMonth(), lTime.getDay()) - MyTime.calcDaynr(lTime.getYear(), 1, 1) + 1));
                    break;
                case 'k':
                    str.append(String.format("%01d", lTime.getHour()));
                    break;
                case 'l':
                    hoursI = (int) ((lTime.getHour() % 24 + 11) % 12 + 1);
                    str.append(String.format("%01d", lTime.getHour()));
                    break;
                case 'p':
                    hoursI = (int) (lTime.getHour() % 24);
                    str.append(hoursI < 12 ? "AM" : "PM");
                    break;
                case 'r':
                    String tmpFmt = ((lTime.getHour() % 24) < 12) ? "%02d:%02d:%02d AM" : "%02d:%02d:%02d PM";
                    str.append(String.format(tmpFmt, (lTime.getHour() + 11) % 12 + 1, lTime.getMinute(), lTime.getSecond()));
                    break;
                case 'S':
                case 's':
                    str.append(String.format("%02d", lTime.getSecond()));
                    break;
                case 'T':
                    str.append(String.format("%02d:%02d:%02d", lTime.getHour(), lTime.getMinute(), lTime.getSecond()));
                    break;
                case 'U':
                case 'u':
                    {
                        LongPtr year = new LongPtr(0);
                        if (type == MySQLTimestampType.MYSQL_TIMESTAMP_TIME) {
                            strPtr.set(str.toString());
                            return true;
                        }
                        str.append(String.format("%02d", MyTime.calcWeek(lTime, formatChars[ptr] == 'U' ? MyTime.WEEK_FIRST_WEEKDAY : MyTime.WEEK_MONDAY_FIRST, year)));
                    }
                    break;
                case 'v':
                case 'V':
                    {
                        LongPtr year = new LongPtr(0);
                        if (type == MySQLTimestampType.MYSQL_TIMESTAMP_TIME) {
                            strPtr.set(str.toString());
                            return true;
                        }
                        str.append(String.format("%02d", MyTime.calcWeek(lTime, formatChars[ptr] == 'V' ? (MyTime.WEEK_YEAR | MyTime.WEEK_FIRST_WEEKDAY) : (MyTime.WEEK_YEAR | MyTime.WEEK_MONDAY_FIRST), year)));
                    }
                    break;
                case 'x':
                case 'X':
                    {
                        LongPtr year = new LongPtr(0);
                        if (type == MySQLTimestampType.MYSQL_TIMESTAMP_TIME) {
                            strPtr.set(str.toString());
                            return true;
                        }
                        MyTime.calcWeek(lTime, formatChars[ptr] == 'X' ? MyTime.WEEK_YEAR | MyTime.WEEK_FIRST_WEEKDAY : MyTime.WEEK_YEAR | MyTime.WEEK_MONDAY_FIRST, year);
                        str.append(String.format("%04d", year.get()));
                    }
                    break;
                case 'w':
                    if (type == MySQLTimestampType.MYSQL_TIMESTAMP_TIME || (lTime.getMonth() == 0 && lTime.getYear() == 0)) {
                        strPtr.set(str.toString());
                        return true;
                    }
                    weekday = MyTime.calcWeekday(MyTime.calcDaynr(lTime.getYear(), lTime.getMonth(), lTime.getDay()), true);
                    str.append(String.format("%01d", weekday));
                    break;
                default:
                    str.append(format.getFormat().substring(ptr));
                    break;
            }
        }
    }
    strPtr.set(str.toString());
    return false;
}
Also used : MyLocale(com.actiontech.dble.plan.common.locale.MyLocale) LongPtr(com.actiontech.dble.plan.common.ptr.LongPtr)

Aggregations

MyLocale (com.actiontech.dble.plan.common.locale.MyLocale)1 LongPtr (com.actiontech.dble.plan.common.ptr.LongPtr)1