PageRenderTime 30ms CodeModel.GetById 13ms app.highlight 14ms RepoModel.GetById 2ms app.codeStats 0ms

/locales/en_us.d

http://github.com/wilkie/djehuty
D | 197 lines | 162 code | 30 blank | 5 comment | 27 complexity | 3511a713476d170e0537b4271e0db4da MD5 | raw file
  1module locales.en_us;
  2
  3import core.locale;
  4
  5import core.time;
  6import core.date;
  7import core.definitions;
  8import core.string;
  9
 10class LocaleEnglish_US : LocaleInterface {
 11	string formatTime(Time time) {
 12		long hour = time.hours;
 13
 14		bool pm = false;
 15
 16		if (hour >= 12) {
 17			hour -= 12;
 18			pm = true;
 19		}
 20
 21		string ret;
 22
 23		ret = toStr(hour);
 24		ret ~= ":";
 25
 26		long min = time.minutes % 60;
 27		if (min < 10) {
 28			ret ~= "0";
 29		}
 30		ret ~= toStr(min);
 31		ret ~= ":";
 32
 33		long sec = time.seconds % 60;
 34		if (sec < 10) {
 35			ret ~= "0";
 36		}
 37		ret ~= toStr(sec);
 38
 39		if (pm) {
 40			ret ~= "pm";
 41		}
 42		else {
 43			ret ~= "PM";
 44		}
 45
 46		return ret;
 47	}
 48
 49	string formatDate(Date date) {
 50		string ret;
 51		switch(date.month) {
 52			case Month.January:
 53				ret = "January ";
 54				break;
 55			case Month.February:
 56				ret = "February ";
 57				break;
 58			case Month.March:
 59				ret = "March ";
 60				break;
 61			case Month.April:
 62				ret = "April ";
 63				break;
 64			case Month.May:
 65				ret = "May ";
 66				break;
 67			case Month.June:
 68				ret = "June ";
 69				break;
 70			case Month.July:
 71				ret = "July ";
 72				break;
 73			case Month.August:
 74				ret = "August ";
 75				break;
 76			case Month.September:
 77				ret = "September ";
 78				break;
 79			case Month.October:
 80				ret = "October ";
 81				break;
 82			case Month.November:
 83				ret = "November ";
 84				break;
 85			case Month.December:
 86				ret = "December ";
 87				break;
 88			default:
 89				ret = "??? ";
 90				break;
 91		}
 92
 93		string day = toStr(date.day);
 94		ret ~= day;
 95
 96		ret ~= ", " ~ toStr(date.year);
 97
 98		return ret;
 99	}
100
101	string formatCurrency(long whole, long scale) {
102		return "$" ~ formatNumber(whole, scale, 2);
103	}
104
105	string formatCurrency(double amount) {
106		return "$" ~ formatNumber(amount);
107	}
108
109	string formatNumber(long whole, long scale, long round = -1) {
110		long intPart;
111		long baseScale;
112		long fracPart;
113
114		// Get integer part of decimal
115		intPart = whole;
116		baseScale = 1;
117		int precision;
118		for (long i; i < scale; i++) {
119			intPart /= 10;
120			baseScale *= 10;
121			precision++;
122		}
123
124		// Get fraction as an integer
125		fracPart = whole % baseScale;
126
127		// Round down 
128		for ( ; precision > round ; precision-- ) {
129			fracPart /= 10;
130		}
131
132		return formatNumber(intPart) ~ "." ~ formatNumber(fracPart);
133	}
134
135	string formatNumber(long value) {
136		if (value == 0) {
137			return "0";
138		}
139
140		string ret;
141		while (value > 0) {
142			long part = value % 1000;
143			value /= 1000;
144			if (ret !is null) {
145				ret = toStr(part) ~ "," ~ ret;
146			}
147			else {
148				ret = toStr(part);
149			}
150		}
151		return ret;
152	}
153
154	string formatNumber(double value) {
155		string ret;
156		long intPart = cast(long)value;
157
158		while (intPart > 0) {
159			long part = intPart % 1000;
160			intPart /= 1000;
161			if (ret !is null) {
162				ret = toStr(part) ~ "," ~ ret;
163			}
164			else {
165				ret = toStr(part);
166			}
167		}
168		ret ~= ".";
169		ret ~= toStr(value);
170	
171		// round last digit
172		bool roundUp = (ret[$-1] >= '5');
173		ret = ret[0..$-1];
174
175		while (roundUp) {
176			if (ret.length == 0) {
177				return "0";
178			}
179			else if (ret[$-1] == '.' || ret[$-1] == '9') {
180				ret = ret[0..$-1];
181				continue;
182			}
183			ret[$-1]++;
184			break;
185		}
186
187		// get rid of useless zeroes (and point if necessary)
188		foreach_reverse(uint i, chr; ret) {
189			if (chr != '0' && chr != '.') {
190				ret = ret[0..i+1];
191				break;
192			}
193		}
194
195		return ret;
196	}
197}