diff --git a/convert/inttostr.h b/convert/inttostr.h index bb41912..c6a4dea 100644 --- a/convert/inttostr.h +++ b/convert/inttostr.h @@ -52,7 +52,7 @@ namespace PT // if the buffer is too small it will be terminated at the beginning (empty string) // and the function returns false template -bool Toa(unsigned long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0) +bool Toa(unsigned long long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0) { size_t i1, i2; long rest; @@ -105,7 +105,7 @@ return true; // if the buffer is too small it will be terminated at the beginning (empty string) // and the function returns false template -bool Toa(long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0) +bool Toa(long long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0) { if( len_out ) *len_out = 0; @@ -125,7 +125,7 @@ bool Toa(long value, CharType * buffer, size_t buf_len, int base = 10, size_t * is_sign = true; } - bool res = Toa(static_cast(value), buf, buf_len, base, len_out); + bool res = Toa(static_cast(value), buf, buf_len, base, len_out); if( res ) { @@ -144,13 +144,47 @@ return res; +template +bool Toa(unsigned long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0) +{ + return Toa(static_cast(value), buffer, buf_len, base, len_out); +} + +template +bool Toa(long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0) +{ + return Toa(static_cast(value), buffer, buf_len, base, len_out); +} +template +bool Toa(unsigned int value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0) +{ + return Toa(static_cast(value), buffer, buf_len, base, len_out); +} +template +bool Toa(int value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0) +{ + return Toa(static_cast(value), buffer, buf_len, base, len_out); +} +template +bool Toa(unsigned short value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0) +{ + return Toa(static_cast(value), buffer, buf_len, base, len_out); +} + + +template +bool Toa(short value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0) +{ + return Toa(static_cast(value), buffer, buf_len, base, len_out); +} + diff --git a/date/Makefile.dep b/date/Makefile.dep index 5f2e8c9..dd46ea6 100644 --- a/date/Makefile.dep +++ b/date/Makefile.dep @@ -1,3 +1,4 @@ # DO NOT DELETE -date.o: date.h +date.o: date.h ../convert/convert.h ../convert/inttostr.h +date.o: ../convert/strtoint.h ../convert/text.h ../convert/misc.h diff --git a/date/date.h b/date/date.h index f6a47b3..de08bab 100644 --- a/date/date.h +++ b/date/date.h @@ -40,7 +40,7 @@ #include #include - +#include "convert/convert.h" namespace PT @@ -493,6 +493,9 @@ private: void AssertRange(int & val, int val_min, int val_max); + template + void SerializeInt(Stream & out, int val, size_t min_width) const; + template void SerializeInt(Stream & out, int val) const; @@ -512,17 +515,29 @@ private: +template +void Date::SerializeInt(Stream & out, int val, size_t min_width) const +{ + char buf[64]; + size_t len; + if( Toa(val, buf, sizeof(buf) / sizeof(char), 10, &len) ) + { + for(size_t i = len ; i < min_width ; ++i) + { + out << '0'; + } + + out << buf; + } +} template void Date::SerializeInt(Stream & out, int val) const { - if( val >= 0 && val < 10 ) - out << '0'; - - out << val; + SerializeInt(out, val, 2); } @@ -530,8 +545,8 @@ void Date::SerializeInt(Stream & out, int val) const template void Date::SerializeYearMonthDay(Stream & out) const { - // !! IMPROVE ME the year should be printed with 4 digits, e.g. 0001 when the year is equal to one - out << year << '-'; + SerializeInt(out, year, 4); + out << '-'; SerializeInt(out, month); out << '-'; SerializeInt(out, day);