package com.paritytrading.philadelphia;

import org.joda.time.ReadableDateTime;

/**
 * Utilities for working with timestamps.
 */
public class FIXTimestamps {

    private FIXTimestamps() {
    }

    /**
     * Append a timestamp to a string builder.
     *
     * @param t a timestamp
     * @param s a string builder
     */
    public static void append(ReadableDateTime t, StringBuilder s) {
        s.append(t.getYear());
        appendTwoDigits(t.getMonthOfYear(), s);
        appendTwoDigits(t.getDayOfMonth(), s);
        s.append('-');
        appendTwoDigits(t.getHourOfDay(), s);
        s.append(':');
        appendTwoDigits(t.getMinuteOfHour(), s);
        s.append(':');
        appendTwoDigits(t.getSecondOfMinute(), s);
        s.append('.');
        appendThreeDigits(t.getMillisOfSecond(), s);
    }

    private static void appendThreeDigits(int x, StringBuilder s) {
        if (x < 100)
            s.append('0');

        if (x < 10)
            s.append('0');

        s.append(x);
    }

    private static void appendTwoDigits(int x, StringBuilder s) {
        if (x < 10)
            s.append('0');

        s.append(x);
    }

}

@@ -154,16 +154,116 @@
 amps %7B%0A%0A
+    private static final ThreadLocal%3Cchar%5B%5D%3E BUFFER = ThreadLocal.withInitial(() -%3E new char%5B21%5D);%0A%0A
     priv
@@ -499,25 +499,73 @@
         
-s.append(
+char%5B%5D buffer = BUFFER.get();%0A%0A        setDigits(buffer, 
 t.getYea
@@ -567,16 +567,22 @@
 etYear()
+, 0, 4
 );%0A     
@@ -584,32 +584,34 @@
         
-appendTwoDigits(
+setDigits(buffer, 
 t.getMon
@@ -618,25 +618,28 @@
 thOfYear(), 
-s
+4, 2
 );%0A        a
@@ -637,32 +637,34 @@
         
-appendTwoDigits(
+setDigits(buffer, 
 t.getDay
@@ -670,25 +670,28 @@
 yOfMonth(), 
-s
+6, 2
 );%0A        s
@@ -693,21 +693,23 @@
     
-s.append(
+buffer%5B8%5D = 
 '-'
-)
 ;%0A  
@@ -714,32 +714,34 @@
         
-appendTwoDigits(
+setDigits(buffer, 
 t.getHou
@@ -746,25 +746,28 @@
 ourOfDay(), 
-s
+9, 2
 );%0A        s
@@ -761,37 +761,40 @@
 2);%0A        
-s.append(
+buffer%5B11%5D = 
 ':'
-)
 ;%0A        ap
@@ -791,32 +791,34 @@
         
-appendTwoDigits(
+setDigits(buffer, 
 t.getMin
@@ -826,25 +826,29 @@
 teOfHour(), 
-s
+12, 2
 );%0A        s
@@ -850,21 +850,24 @@
     
-s.append(
+buffer%5B14%5D = 
 ':'
-)
 ;%0A  
@@ -872,32 +872,34 @@
         
-appendTwoDigits(
+setDigits(buffer, 
 t.getSec
@@ -909,25 +909,29 @@
 OfMinute(), 
-s
+15, 2
 );%0A        s
@@ -933,21 +933,24 @@
     
-s.append(
+buffer%5B17%5D = 
 '.'
-)
 ;%0A  
@@ -955,34 +955,34 @@
         
-appendThreeDigits(
+setDigits(buffer, 
 t.getMil
@@ -996,17 +996,48 @@
 cond(), 
-s
+18, 3);%0A%0A        s.append(buffer
 );%0A    %7D
@@ -1066,262 +1066,174 @@
 oid 
-appendThreeDigits(
+setDigits(char%5B%5D buffer, 
 int 
-x, StringBuilder s) %7B%0A        if (x %3C 100)%0A            s.append('0');%0A%0A        if (x %3C 10)%0A            s.append('0');%0A%0A        s.append(x);
+i, int offset, int digits) %7B%0A        for (int j = offset + digits - 1; j %3E= offset; j--) %7B
 %0A    
-%7D%0A%0A
     
-private static void appendTwoDigits(int x, StringBuilder s) %7B%0A        if (x %3C
+    buffer%5Bj%5D = (char)('0' + i %25
  10)
+;%0A
 %0A   
@@ -1245,44 +1245,26 @@
     
-s.append('0');%0A%0A        s.append(x);
+i /= 10;%0A        %7D
 %0A   

Optimize 'FIXTimestamps.append'