001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.format;
020
021import java.text.DateFormatSymbols;
022import java.util.Locale;
023
024public abstract class BaseFormat implements Format {
025
026        private Locale locale;
027
028        public BaseFormat(Locale locale) {
029                this.locale=locale;
030        }
031        
032        protected final String getMonthAsString(int month) {
033                if(getLocale().equals(Locale.US)) {
034                        switch(month) {
035                                case 1: return "January";
036                                case 2: return "February";
037                                case 3: return "March";
038                                case 4: return "April";
039                                case 5: return "May";
040                                case 6: return "June";
041                                case 7: return "July";
042                                case 8: return "August";
043                                case 9: return "September";
044                                case 10: return "October";
045                                case 11: return "November";
046                                case 12: return "December";
047                                default: return null;
048                        }
049                }
050                return new DateFormatSymbols(locale).getMonths()[month-1];
051                
052        }
053        
054        protected final String getMonthShortAsString(int month) {
055                if(getLocale().equals(Locale.US)) {
056                        switch(month) {
057                                case 1: return "Jan";
058                                case 2: return "Feb";
059                                case 3: return "Mar";
060                                case 4: return "Apr";
061                                case 5: return "May";
062                                case 6: return "Jun";
063                                case 7: return "Jul";
064                                case 8: return "Aug";
065                                case 9: return "Sep";
066                                case 10: return "Oct";
067                                case 11: return "Nov";
068                                case 12: return "Dec";
069                                default: return null;
070                        }
071                }
072                return new DateFormatSymbols(locale).getShortMonths()[month-1];
073                
074        }
075        
076        protected final String getDayOfWeekAsString(int dayOfWeek) {
077                if(getLocale().equals(Locale.US)) {
078                        switch(dayOfWeek) {
079                                case 1: return "Sunday";
080                                case 2: return "Monday";
081                                case 3: return "Tuesday";
082                                case 4: return "Wednesday";
083                                case 5: return "Thursday";
084                                case 6: return "Friday";
085                                case 7: return "Saturday";
086                                default:  return null;
087                        }
088                }
089                return new DateFormatSymbols(locale).getWeekdays()[dayOfWeek];
090                
091        }
092        
093        protected final String getDayOfWeekShortAsString(int dayOfWeek) {
094                if(getLocale().equals(Locale.US)) {
095                        switch(dayOfWeek) {
096                                case 1: return "Sun";
097                                case 2: return "Mon";
098                                case 3: return "Tue";
099                                case 4: return "Wed";
100                                case 5: return "Thu";
101                                case 6: return "Fri";
102                                case 7: return "Sat";
103                                default:  return null;
104                        }
105                }
106                return new DateFormatSymbols(locale).getShortWeekdays()[dayOfWeek];
107        }
108        
109        protected final Locale getLocale() {
110                return locale==null?Locale.US:locale;   
111        }
112}