Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: CPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Common Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/cpl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2002, 2009 Jan Arne Petersen <jpetersen@uni-bonn.de> Copyright (C) 2004 Charles O Nutter <headius@headius.com> Copyright (C) 2004 Anders Bengtsson <ndrsbngtssn@yahoo.se> Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de> Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the CPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the CPL, the GPL or the LGPL. END LICENSE BLOCK ***
 
 package org.jruby.util;
 
 import java.util.Date;
 import java.util.List;
 
 
 public class RubyDateFormat extends DateFormat {
     private static final long serialVersionUID = -250429218019023997L;
 
     private boolean ruby_1_9;
     private List<TokencompiledPattern;
 
     private final DateFormatSymbols formatSymbols;
 
     private static final int FORMAT_STRING = 0;
     private static final int FORMAT_WEEK_LONG = 1;
     private static final int FORMAT_WEEK_SHORT = 2;
     private static final int FORMAT_MONTH_LONG = 3;
     private static final int FORMAT_MONTH_SHORT = 4;
     private static final int FORMAT_DAY = 5;
     private static final int FORMAT_DAY_S = 6;
     private static final int FORMAT_HOUR = 7;
     private static final int FORMAT_HOUR_M = 8;
     private static final int FORMAT_HOUR_S = 9;
     private static final int FORMAT_DAY_YEAR = 10;
     private static final int FORMAT_MINUTES = 11;
     private static final int FORMAT_MONTH = 12;
     private static final int FORMAT_MERIDIAN = 13;
     private static final int FORMAT_MERIDIAN_LOWER_CASE = 14;
     private static final int FORMAT_SECONDS = 15;
     private static final int FORMAT_WEEK_YEAR_S = 16;
     private static final int FORMAT_WEEK_YEAR_M = 17;
     private static final int FORMAT_DAY_WEEK = 18;
     private static final int FORMAT_YEAR_LONG = 19;
     private static final int FORMAT_YEAR_SHORT = 20;
     private static final int FORMAT_ZONE_OFF = 21;
     private static final int FORMAT_ZONE_ID = 22;
     private static final int FORMAT_CENTURY = 23;
     private static final int FORMAT_HOUR_BLANK = 24;
     private static final int FORMAT_MILLISEC = 25;
     private static final int FORMAT_EPOCH = 26;
     private static final int FORMAT_DAY_WEEK2 = 27;
     private static final int FORMAT_WEEK_WEEKYEAR = 28;
     private static final int FORMAT_NANOSEC = 29;
     private static final int FORMAT_PRECISION = 30;
     private static final int FORMAT_WEEKYEAR = 31;
     private static final int FORMAT_OUTPUT = 32;
     private static final int FORMAT_COLON_ZONE_OFF = 33;
     private static final int FORMAT_COLON_COLON_ZONE_OFF = 34;
     private static final int FORMAT_COLON_COLON_COLON_ZONE_OFF = 35;
 
 
     private static class Token {
         private int format;
         private Object data;
         private TimeOutputFormatter outputFormatter;
         
         public Token(int format) {
             this(formatnull);
         }
        public Token(int formatObject data) {
            this. = format;
            this. = data;
        }
        
        
Gets the data.

Returns:
Returns a Object
        public Object getData() {
            return ;
        }

        
Gets the format.

Returns:
Returns a int
        public int getFormat() {
            return ;
        }
    }

    
Constructor for RubyDateFormat.
    public RubyDateFormat() {
        this(""new DateFormatSymbols());
    }
    public RubyDateFormat(String patternLocale aLocale) {
        this(patternnew DateFormatSymbols(aLocale));
    }
    public RubyDateFormat(String patternLocale aLocaleboolean ruby_1_9) {
        this(patternaLocale);
        this. = ruby_1_9;
    }
    
    public RubyDateFormat(String patternDateFormatSymbols formatSymbols) {
        super();
        this. = formatSymbols;
        applyPattern(pattern);
    }
    
    public void applyPattern(String pattern) {
        compilePattern(pattern);
    }
    
    private void compilePattern(String pattern) {
         = new LinkedList<Token>();
        
        int len = pattern.length();
        for (int i = 0; i < len;) {
            if (pattern.charAt(i) == '%') {
                i++;
                if(i == len) {
                    .add(new Token("%"));
                } else {
                    i = addOutputFormatter(patterni);
                    switch (pattern.charAt(i)) {
                    case 'A' :
                        .add(new Token());
                        break;
                    case 'a' :
                        .add(new Token());
                        break;
                    case 'B' :
                        .add(new Token());
                        break;
                    case 'b' :
                    case 'h' :
                        .add(new Token());
                        break;
                    case 'C' :
                        .add(new Token());
                        break;
                    case 'c' :
                        .add(new Token());
                        .add(new Token(" "));
                        .add(new Token());
                        .add(new Token(" "));
                        .add(new Token());
                        .add(new Token(" "));
                        .add(new Token());
                        .add(new Token(":"));
                        .add(new Token());
                        .add(new Token(":"));
                        .add(new Token());
                        .add(new Token(" "));
                        .add(new Token());
                        break;
                    case 'D':
                        .add(new Token());
                        .add(new Token("/"));
                        .add(new Token());
                        .add(new Token("/"));
                        .add(new Token());
                        break;
                    case 'd':
                        .add(new Token());
                        break;
                    case 'e':
                        .add(new Token());
                        break;
                    case 'F':
                        .add(new Token());
                        .add(new Token("-"));
                        .add(new Token());
                        .add(new Token("-"));
                        .add(new Token());
                        break;
                    case 'G':
                        .add(new Token());
                        break;
                    case 'H':
                        .add(new Token());
                        break;
                    case 'I':
                        .add(new Token());
                        break;
                    case 'j':
                        .add(new Token());
                        break;
                    case 'k':
                        .add(new Token());
                        break;
                    case 'L':
                        .add(new Token());
                        break;
                    case 'l':
                        .add(new Token());
                        break;
                    case 'M':
                        .add(new Token());
                        break;
                    case 'm':
                        .add(new Token());
                        break;
                    case 'N':
                        .add(new Token());
                        break;
                    case 'n':
                        .add(new Token("\n"));
                        break;
                    case 'p':
                        .add(new Token());
                        break;
                    case 'P':
                        .add(new Token());
                        break;
                    case 'R':
                        .add(new Token());
                        .add(new Token(":"));
                        .add(new Token());
                        break;
                    case 'r':
                        .add(new Token());
                        .add(new Token(":"));
                        .add(new Token());
                        .add(new Token(":"));
                        .add(new Token());
                        .add(new Token(" "));
                        .add(new Token());
                        break;
                    case 's':
                        .add(new Token());
                        break;
                    case 'S':
                        .add(new Token());
                        break;
                    case 'T':
                        .add(new Token());
                        .add(new Token(":"));
                        .add(new Token());
                        .add(new Token(":"));
                        .add(new Token());
                        break;
                    case 't':
                        .add(new Token(,"\t"));
                        break;
                    case 'u':
                        .add(new Token());
                        break;
                    case 'U':
                        .add(new Token());
                        break;
                    case 'v':
                        .add(new Token());
                        .add(new Token("-"));
                        .add(new Token());
                        .add(new Token("-"));
                        .add(new Token());
                        break;
                    case 'V':
                        .add(new Token());
                        break;
                    case 'W':
                        .add(new Token());
                        break;
                    case 'w':
                        .add(new Token());
                        break;
                    case 'X':
                        .add(new Token());
                        .add(new Token(":"));
                        .add(new Token());
                        .add(new Token(":"));
                        .add(new Token());
                        break;
                    case 'x':
                        .add(new Token());
                        .add(new Token("/"));
                        .add(new Token());
                        .add(new Token("/"));
                        .add(new Token());
                        break;
                    case 'Y':
                        .add(new Token());
                        break;
                    case 'y':
                        .add(new Token());
                        break;
                    case 'Z':
                        .add(new Token());
                        break;
                    case 'z':
                        .add(new Token());
                        break;
                    case '%':
                        .add(new Token("%"));
                        break;
                    case ':':
                        i++;
                        if(i == len) {
                            .add(new Token("%:"));
                        } else {
                            switch (pattern.charAt(i)) {
                                case 'z':
                                    .add(new Token());
                                    break;
                                case ':':
                                    i++;
                                    if(i == len) {
                                        .add(new Token("%::"));
                                    } else {
                                        switch (pattern.charAt(i)) {
                                            case 'z':
                                                .add(new Token());
                                                break;
                                            case ':':
                                                i++;
                                                if(i == len) {
                                                    .add(new Token("%:::"));
                                                } else {
                                                    switch (pattern.charAt(i)) {
                                                        case 'z':
                                                            .add(new Token());
                                                            break;
                                                        case ':':
                                                        default:
                                                            .add(new Token("%:::" + pattern.charAt(i)));
                                                    }
                                                }
                                                break;
                                            default:
                                                .add(new Token("%::" + pattern.charAt(i)));
                                        }
                                    }
                                    break;
                                default:
                                    .add(new Token("%:" + pattern.charAt(i)));
                            }
                        }
                        break;
                    default:
                        .add(new Token("%" + pattern.charAt(i)));
                    }
                    i++;
                }
            } else {
                StringBuilder sb = new StringBuilder();
                for (;i < len && pattern.charAt(i) != '%'i++) {
                    sb.append(pattern.charAt(i));
                }
                .add(new Token(sb.toString()));
            }
        }
    }
    private int addOutputFormatter(String patternint index) {
        if () {
            TimeOutputFormatter outputFormatter = TimeOutputFormatter.getFormatter(pattern.substring(index - 1));
            if (outputFormatter != null) {
                index += outputFormatter.getFormatter().length();
                .add(new Token(outputFormatter));
            }
        }
        return index;
    }
    private String formatOutput(TimeOutputFormatter formatterString output) {
        if (formatter == nullreturn output;
        output = formatter.format(output);
        formatter = null;
        return output;
    }
    private DateTime dt;
    private long nsec;
    public void setDateTime(final DateTime dt) {
        this. = dt;
    }
    public void setNSec(long nsec) {
        this. = nsec;
    }
    
    // Much faster then generic String.format()
    private String twoCharDigit(long value) {
        if (value == 0) return "00";
        if (value < 10) return "0"+value;
        return ""+value;        
    }
    
    // Much faster then generic String.format()
    private String threeCharDigit(long value) {
        if (value == 0) return "000";
        if (value < 10) return "00"+value;
        if (value < 100) return "0"+value;
        return ""+value;
    }
    // Much faster then generic String.format()
    private String fourCharDigit(long value) {
        if (value == 0) return "0000";
        if (value < 10) return "000"+value;
        if (value < 100) return "00"+value;
        if (value < 1000) return "0"+value;
        return ""+value;
    }

    
    public StringBuffer format(Date ignoredStringBuffer toAppendToFieldPosition fieldPosition) {
        TimeOutputFormatter formatter = null;
        for (Token token) {
            String output = null;
            long value = 0;
            boolean format = true;
            switch (token.getFormat()) {
                case :
                    formatter = (TimeOutputFormattertoken.getData();
                    break;
                case :
                    output = token.getData().toString();
                    format = false;
                    break;
                case :
                    // This is GROSS, but Java API's aren't ISO 8601 compliant at all
                    int v = (.getDayOfWeek()+1)%8;
                    if(v == 0) {
                        v++;
                    }
                    output = .getWeekdays()[v];
                    break;
                case :
                    // This is GROSS, but Java API's aren't ISO 8601 compliant at all
                    v = (.getDayOfWeek()+1)%8;
                    if(v == 0) {
                        v++;
                    }
                    output = .getShortWeekdays()[v];
                    break;
                case :
                    output = .getMonths()[.getMonthOfYear()-1];
                    break;
                case :
                    output = .getShortMonths()[.getMonthOfYear()-1];
                    break;
                case :
                    output = twoCharDigit(.getDayOfMonth());
                    break;
                case 
                    value = .getDayOfMonth();
                    output = (value < 10 ? " " : "") + Long.toString(value);
                    break;
                case :
                case :
                    value = .getHourOfDay();
                    output = "";
                    if (value < 10) {
                        output += token.getFormat() ==  ? "0" : " ";
                    }
                    output += value;
                    break;
                case :
                case :
                    value = .getHourOfDay();
                    if(value > 12) {
                        value-=12;
                    }
                    if(value == 0) {
                        output = "12";
                    } else {
                        output = "";
                        if (value < 10) {
                            output += token.getFormat() ==  ? "0" : " ";
                        }
                        output += value;
                    }
                    break;
                case :
                    output = threeCharDigit(.getDayOfYear());
                    break;
                case :
                    output = twoCharDigit(.getMinuteOfHour());
                    break;
                case :
                    output = twoCharDigit(.getMonthOfYear());
                    break;
                case :
                case :
                    if (.getHourOfDay() < 12) {
                        output = token.getFormat() ==  ? "AM" : "am";
                    } else {
                        output = token.getFormat() ==  ? "PM" : "pm";
                    }
                    break;
                case :
                    value = .getSecondOfMinute();
                    output = (value < 10 ? "0" : "") + Long.toString(value);
                    break;
                case :
                    output = formatWeekYear(...);
                    break;
                case :
                    output = formatWeekYear(...);
                    break;
                case :
                case :
                    value = .getDayOfWeek() ;
                    if (token.getFormat() == ) {
                        value = value % 7;
                    }
                    output = Long.toString(value);
                    break;
                case :
                    output = fourCharDigit(.getYear());
                    break;
                case :
                    output = twoCharDigit(.getYear() % 100);
                    break;
                case :
                case :
                case :
                case :
                    value = .getZone().getOffset(.getMillis());
                    output = value < 0 ? "-" : "+";
                    value = Math.abs(value);
                    // hours
                    if (value / 3600000 < 10) {
                        output += "0";
                    }
                    output += (value / 3600000);
                    // :::z just shows hour
                    if (token.getFormat() == break;
                    // :z and ::z have colon after hour
                    if (token.getFormat() ==  ||
                            token.getFormat() == output += ':';
                    // minutes
                    if ((value % 3600000 / 60000) < 10) {
                        output += "0";
                    }
                    output += value % 3600000 / 60000;
                    // ::z includes colon and seconds
                    if (token.getFormat() == ) {
                        // seconds
                        if ((value % 60000) < 10) {
                            output += "0";
                        }
                        output += value % 60000;
                    }
                    break;
                case :
                    toAppendTo.append(.getZone().getShortName(.getMillis()));
                    break;
                case :
                    toAppendTo.append(.getCenturyOfEra());
                    break;
                case :
                    output = threeCharDigit(.getMillisOfSecond());
                    break;
                case :
                    output = Long.toString(.getMillis()/1000);
                    break;
                case :
                    output = twoCharDigit(.getWeekOfWeekyear());
                    break;
                case :
                    value = .getMillisOfSecond() * 1000000;
                    if (value += ;
                    String width =  ? "9" : "3";
                    if (formatter != nullwidth = formatter.getFormatter();
                    output = String.format("%0" + width + "d"value);
                    formatter = null// we are done with this formatter
                    break;
                case :
                    output = Integer.toString(.getWeekyear());
                    break;
            }
            if (output != null) {
                toAppendTo.append(format ? formatOutput(formatteroutput) : output);
            }
        }
        return toAppendTo;
    }
	private String formatWeekYear(int firstDayOfWeek) {
            java.util.Calendar dtCalendar = .toGregorianCalendar();
            dtCalendar.setFirstDayOfWeek(firstDayOfWeek);
            dtCalendar.setMinimalDaysInFirstWeek(7);
            int value = dtCalendar.get(...);
            if ((value == 52 || value == 53) &&
                    (dtCalendar.get(.) == . )) {
                // MRI behavior: Week values are monotonous.
                // So, weeks that effectively belong to previous year,
                // will get the value of 0, not 52 or 53, as in Java.
                value = 0;
            }
            return twoCharDigit(value);
	}

    
    public Date parse(String sourceParsePosition pos) {
        throw new UnsupportedOperationException();
    }
New to GrepCode? Check out our FAQ X