Permalink
Browse files

Added BigInteger to %d printf format, added DateTime printf formattin…

…g. Closes #5
  • Loading branch information...
1 parent 129d861 commit 0eaaf188ac8ca1bd0eb67cd51d3abe14590bac28 David Miller committed Jul 21, 2009
View
@@ -5,3 +5,5 @@ obj
*.suo
# Emacs artifacts
*~
+# Java artifacts
+*.class
@@ -1066,6 +1066,76 @@ public void TextSpecBasics()
// TODO: Datetime tests
+ [Test]
+ public void DateTimeBasics()
+ {
+ Test("01","%tH", new DateTime(2009, 7, 1, 1, 10, 20));
+ Test("14", "%tH", new DateTime(2009, 7, 1, 14, 10, 20));
+ Test("01", "%tI", new DateTime(2009, 7, 1, 1, 10, 20));
+ Test("02", "%tI", new DateTime(2009, 7, 1, 14, 10, 20));
+ Test("1", "%tk", new DateTime(2009, 7, 1, 1, 10, 20));
+ Test("14", "%tk", new DateTime(2009, 7, 1, 14, 10, 20));
+ Test("1", "%tl", new DateTime(2009, 7, 1, 1, 10, 20));
+ Test("2", "%tl", new DateTime(2009, 7, 1, 14, 10, 20));
+ Test("02", "%tM", new DateTime(2009, 7, 1, 14, 02, 03));
+ Test("44", "%tM", new DateTime(2009, 7, 1, 14, 44, 50));
+ Test("03", "%tS", new DateTime(2009, 7, 1, 14, 02, 03));
+ Test("50", "%tS", new DateTime(2009, 7, 1, 14, 44, 50));
+ Test("050", "%tL", new DateTime(2009, 7, 1, 14, 02, 03).AddMilliseconds(50));
+ Test("000027300", "%tN", new DateTime(2009, 7, 1, 14, 20, 03).AddTicks(273));
+ Test("AM", "%tp", new DateTime(2009, 7, 1, 1, 10, 20)); // java has lowercase here.
+ Test("PM", "%tp", new DateTime(2009, 7, 1, 14, 10, 20)); // java has lowercase here.
+ Test("93784010", "%tQ", new DateTime(1970, 1, 1) + new TimeSpan(1, 2, 3, 4, 10));
+ Test("93784", "%ts", new DateTime(1970, 1, 1) + new TimeSpan(1, 2, 3, 4, 10));
+ Test("02:03:04", "%tT", new DateTime(2009, 7, 1, 2, 3, 4));
+ Test("14:52:43", "%tT", new DateTime(2009, 7, 1, 14, 52, 43));
+
+ Test("", "%tz", new DateTime(2009, 7, 1, 1, 10, 20));
+ Test("", "%tZ", new DateTime(2009, 7, 1, 1, 10, 20));
+
+ Test("July", "%tB", new DateTime(2009, 7, 1, 1, 10, 20));
+ Test("Jul", "%tb", new DateTime(2009, 7, 1, 1, 10, 20));
+ Test("Jul", "%th", new DateTime(2009, 7, 1, 1, 10, 20));
+ Test("Wednesday", "%tA", new DateTime(2009, 7, 1, 1, 10, 20));
+ Test("Wed", "%ta", new DateTime(2009, 7, 1, 1, 10, 20));
+
+ Test("19", "%tC", new DateTime(1998, 7, 1, 1, 10, 20));
+ Test("20", "%tC", new DateTime(2009, 7, 1, 1, 10, 20));
+ Test("09", "%tC", new DateTime(998, 7, 1, 1, 10, 20));
+
+ Test("1998", "%tY", new DateTime(1998, 7, 1, 1, 10, 20));
+ Test("2009", "%tY", new DateTime(2009, 7, 1, 1, 10, 20));
+ Test("0998", "%tY", new DateTime(998, 7, 1, 1, 10, 20));
+ Test("98", "%ty", new DateTime(1998, 7, 1, 1, 10, 20));
+ Test("09", "%ty", new DateTime(2009, 7, 1, 1, 10, 20));
+
+ Test("001", "%tj", new DateTime(2009, 1, 1));
+ Test("004", "%tj", new DateTime(2009, 1, 1).AddDays(3));
+ Test("201", "%tj", new DateTime(2009, 1, 1).AddDays(200));
+
+ Test("01", "%tm", new DateTime(2009, 1, 1));
+ Test("11", "%tm", new DateTime(2009, 11, 25));
+ Test("01", "%td", new DateTime(2009, 1, 1));
+ Test("25", "%td", new DateTime(2009, 11, 25));
+ Test("1", "%te", new DateTime(2009, 1, 1));
+ Test("25", "%te", new DateTime(2009, 11, 25));
+
+ Test("01:02:03 AM", "%tr", new DateTime(2009, 7, 1, 1, 2, 3));
+ Test("02:10:20 PM", "%tr", new DateTime(2009, 7, 1, 14, 10, 20));
+
+ Test("01:02", "%tR", new DateTime(2009, 7, 1, 1, 2, 3));
+ Test("14:10", "%tR", new DateTime(2009, 7, 1, 14, 10, 20));
+
+ Test("Wed, 01 Jul 2009 01:02:03 GMT", "%tc", new DateTime(2009, 7, 1, 1, 2, 3));
+ Test("Wed, 01 Jul 2009 14:10:20 GMT", "%tc", new DateTime(2009, 7, 1, 14, 10, 20));
+
+ Test("07/01/09", "%tD", new DateTime(2009, 7, 1, 1, 2, 3));
+ Test("11/25/98", "%tD", new DateTime(1998, 11, 25, 1, 2, 3));
+ Test("2009-07-01", "%tF", new DateTime(2009, 7, 1, 1, 2, 3));
+ Test("1998-11-25", "%tF", new DateTime(1998, 11, 25, 1, 2, 3));
+
+ }
+
#endregion
}
@@ -27,12 +27,12 @@ namespace clojure.lang
/// </remarks>
public static class Printf
{
- // TODO: extend Printf to BigInteger
// TODO: extend Printf to BigDecimal
// TODO: extend Printf to decimal
// TODO: implement DateTime
// TODO: implement HexFloat
- // TODO: implmement grouping for GeneralFloat
+ // TODO: implement grouping for GeneralFloat
+ // TODO: implement grouping for %d on BigInteger
#region Data
@@ -952,9 +952,164 @@ void PrintMagnitude(StringBuilder sb, bool isNeg, string val)
private void PrintDateTime(StringBuilder sb, object arg)
{
- throw new NotImplementedException();
+ if (arg == null)
+ {
+ PrintString(sb, "null");
+ return;
+ }
+
+ DateTime dt = DateTime.Now;
+
+ if (arg is long)
+ dt = DateTime.FromBinary((long)arg);
+ else if (arg is DateTime)
+ dt = (DateTime)arg;
+ else
+ FailConversion(_conversion, arg);
+
+ PrintDateTime(sb, dt);
}
+ static readonly DateTime Epoch = new DateTime(1970,1,1);
+
+ private void PrintDateTime(StringBuilder sb, DateTime dt)
+ {
+ string format = "";
+
+ switch (_conversion)
+ {
+
+ case DateTimeConv.HOUR_OF_DAY_0: // = 'H' (00 - 23)
+ format = "HH";
+ break;
+
+ case DateTimeConv.HOUR_0: // = 'I' (01 - 12)
+ format = "hh";
+ break;
+
+ case DateTimeConv.HOUR_OF_DAY: // = 'k' (0 - 23) -- like H
+ format = "%H";
+ break;
+
+ case DateTimeConv.HOUR: // = 'l' (1 - 12) -- like I
+ format = "%h";
+ break;
+
+ case DateTimeConv.MINUTE: // = 'M' (00 - 59)
+ format = "mm";
+ break;
+
+ case DateTimeConv.NANOSECOND: // = 'N' (000000000 - 999999999)
+ sb.Append(((dt - new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond)).Ticks * 100).ToString("D9"));
+ return;
+
+ case DateTimeConv.MILLISECOND: // = 'L' jdk, not in gnu (000 - 999)
+ format = "fff";
+ break;
+
+ case DateTimeConv.MILLISECOND_SINCE_EPOCH: // = 'Q' (0 - 99...?)
+ TimeSpan ts = dt - Epoch;
+ sb.Append((long)ts.TotalMilliseconds);
+ return;
+
+ case DateTimeConv.AM_PM: // = 'p' (am or pm)
+ format = "tt";
+ break;
+
+ case DateTimeConv.SECONDS_SINCE_EPOCH: // = 's' (0 - 99...?)
+ ts = dt - Epoch;
+ sb.Append((long)ts.TotalSeconds);
+ return;
+
+ case DateTimeConv.SECOND: // = 'S' (00 - 60 - leap second)
+ format = "ss";
+ break;
+
+ case DateTimeConv.TIME: // = 'T' (24 hour hh:mm:ss)
+ format = "HH:mm:ss";
+ break;
+
+ case DateTimeConv.ZONE_NUMERIC: // = 'z' (-1200 - +1200) - ls minus
+ // TODO: This is not exactly the same as the Java version
+ format = "%K";
+ break;
+
+ case DateTimeConv.ZONE: // = 'Z' (symbol)
+ // Can't do this directly from DateTime.
+ return;
+
+ // Date
+ case DateTimeConv.NAME_OF_DAY_ABBREV: // = 'a' 'a'
+ format = "ddd";
+ break;
+
+ case DateTimeConv.NAME_OF_DAY: // = 'A' 'A'
+ format = "dddd";
+ break;
+
+ case DateTimeConv.NAME_OF_MONTH_ABBREV: // = 'b' 'b'
+ case DateTimeConv.NAME_OF_MONTH_ABBREV_X: // = 'h' -- same b
+ format = "MMM";
+ break;
+
+ case DateTimeConv.NAME_OF_MONTH: // = 'B' 'B'
+ format = "MMMM";
+ break;
+
+ case DateTimeConv.CENTURY: // = 'C' (00 - 99)
+ sb.Append((dt.Year / 100).ToString("D2"));
+ return;
+
+ case DateTimeConv.DAY_OF_MONTH_0: // = 'd' (01 - 31)
+ format = "dd";
+ break;
+
+ case DateTimeConv.DAY_OF_MONTH: // = 'e' (1 - 31) -- like d
+ format = "%d";
+ break;
+
+ case DateTimeConv.DAY_OF_YEAR: // = 'j' (001 - 366)
+ sb.Append(dt.DayOfYear.ToString("D3"));
+ return;
+
+ case DateTimeConv.MONTH: // = 'm' (01 - 12)
+ format = "MM";
+ break;
+
+ case DateTimeConv.YEAR_2: // = 'y' (00 - 99)
+ format = "yy";
+ break;
+
+ case DateTimeConv.YEAR_4: // = 'Y' (0000 - 9999)
+ format = "yyyy";
+ break;
+
+ // Composites
+ case DateTimeConv.TIME_12_HOUR: // = 'r' (hh:mm:ss [AP]M)
+ format = "hh:mm:ss tt";
+ break;
+
+ case DateTimeConv.TIME_24_HOUR: // = 'R' (hh:mm same as %H:%M)
+ format = "HH:mm";
+ break;
+
+ case DateTimeConv.DATE_TIME: // 'c' (Sat Nov 04 12:02:33 EST 1999)
+ format = "r";
+ break;
+
+ case DateTimeConv.DATE: // 'D' (mm/dd/yy)
+ format = "MM/dd/yy";
+ break;
+
+ case DateTimeConv.ISO_STANDARD_DATE: // 'F' (%Y-%m-%d)
+ format = "yyyy-MM-dd";
+ break;
+ }
+ sb.Append(dt.ToString(format));
+ }
+
+
+
#endregion
#region Printing support
@@ -1217,11 +1372,83 @@ public static bool IsValid(char c)
public static class DateTimeConv
{
+ public const char HOUR_OF_DAY_0 = 'H'; // (00 - 23)
+ public const char HOUR_0 = 'I'; // (01 - 12)
+ public const char HOUR_OF_DAY = 'k'; // (0 - 23) -- like H
+ public const char HOUR = 'l'; // (1 - 12) -- like I
+ public const char MINUTE = 'M'; // (00 - 59)
+ public const char NANOSECOND = 'N'; // (000000000 - 999999999)
+ public const char MILLISECOND = 'L'; // jdk, not in gnu (000 - 999)
+ public const char MILLISECOND_SINCE_EPOCH = 'Q'; // (0 - 99...?)
+ public const char AM_PM = 'p'; // (am or pm)
+ public const char SECONDS_SINCE_EPOCH = 's'; // (0 - 99...?)
+ public const char SECOND = 'S'; // (00 - 60 - leap second)
+ public const char TIME = 'T'; // (24 hour hh:mm:ss)
+ public const char ZONE_NUMERIC = 'z'; // (-1200 - +1200) - ls minus?
+ public const char ZONE = 'Z'; // (symbol)
+
+ // Date
+ public const char NAME_OF_DAY_ABBREV = 'a'; // 'a'
+ public const char NAME_OF_DAY = 'A'; // 'A'
+ public const char NAME_OF_MONTH_ABBREV = 'b'; // 'b'
+ public const char NAME_OF_MONTH = 'B'; // 'B'
+ public const char CENTURY = 'C'; // (00 - 99)
+ public const char DAY_OF_MONTH_0 = 'd'; // (01 - 31)
+ public const char DAY_OF_MONTH = 'e'; // (1 - 31) -- like d
+ public const char NAME_OF_MONTH_ABBREV_X = 'h'; // -- same b
+ public const char DAY_OF_YEAR = 'j'; // (001 - 366)
+ public const char MONTH = 'm'; // (01 - 12)
+ public const char YEAR_2 = 'y'; // (00 - 99)
+ public const char YEAR_4 = 'Y'; // (0000 - 9999)
+
+ // Composites
+ public const char TIME_12_HOUR = 'r'; // (hh:mm:ss [AP]M)
+ public const char TIME_24_HOUR = 'R'; // (hh:mm same as %H:%M)
+ public const char DATE_TIME = 'c'; // (Sat Nov 04 12:02:33 EST 1999)
+ public const char DATE = 'D'; // (mm/dd/yy)
+ public const char ISO_STANDARD_DATE = 'F'; // (%Y-%m-%d)
+
+
public static bool IsValid(char c)
{
- throw new NotImplementedException("Feeling lazy");
+ switch (c)
+ {
+ case HOUR_OF_DAY_0:
+ case HOUR_0:
+ case HOUR_OF_DAY:
+ case HOUR:
+ case MINUTE:
+ case NANOSECOND:
+ case MILLISECOND:
+ case MILLISECOND_SINCE_EPOCH:
+ case AM_PM:
+ case SECONDS_SINCE_EPOCH:
+ case SECOND:
+ case TIME:
+ case ZONE_NUMERIC:
+ case ZONE:
+ case NAME_OF_DAY_ABBREV:
+ case NAME_OF_DAY:
+ case NAME_OF_MONTH_ABBREV:
+ case NAME_OF_MONTH:
+ case CENTURY:
+ case DAY_OF_MONTH_0:
+ case DAY_OF_MONTH:
+ case NAME_OF_MONTH_ABBREV_X:
+ case DAY_OF_YEAR:
+ case MONTH:
+ case YEAR_2:
+ case YEAR_4:
+ case TIME_12_HOUR:
+ case TIME_24_HOUR:
+ case DATE_TIME:
+ case DATE:
+ case ISO_STANDARD_DATE:
+ return true;
+ default:
+ return false;
+ }
}
-
}
#endregion
@@ -6,6 +6,8 @@
package testjavaformat;
import java.io.IOException;
+import java.util.Calendar;
+import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -179,6 +181,29 @@ private static void RunFormatTests() {
//System.out.println("char1 =" + String.format("%x", (int) s.charAt(1)));
+ Test("%,20d",new java.math.BigInteger("123456789"));
+
+ System.out.println("-------------");
+
+ Test("%tH", new Date(2009, 7, 1, 1, 10, 20));
+ Test("%tH", new Date(2009, 7, 1, 14, 10, 20));
+ Test("%tI", new Date(2009, 7, 1, 1, 10, 20));
+ Test("%tI", new Date(2009, 7, 1, 14, 10, 20));
+ Test("%tk", new Date(2009, 7, 1, 1, 10, 20));
+ Test("%tk", new Date(2009, 7, 1, 14, 10, 20));
+ Test("%tl", new Date(2009, 7, 1, 1, 10, 20));
+ Test("%tl", new Date(2009, 7, 1, 14, 10, 20));
+
+ Test("%tM", new Date(2009, 7, 1, 14, 02, 03));
+ Test("%tM", new Date(2009, 7, 1, 14, 44, 50));
+ Test("%tS", new Date(2009, 7, 1, 14, 02, 03));
+ Test("%tS", new Date(2009, 7, 1, 14, 44, 50));
+
+
+
+
+
+
}
private static void Test(String fmt, Object ... args) {

0 comments on commit 0eaaf18

Please sign in to comment.