1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <tools/datetimeutils.hxx>
11 #include <rtl/strbuf.hxx>
12 #include <rtl/ustrbuf.hxx>
15 /// Append the number as 2-digit when less than 10.
16 template<class TStringBuffer
>
17 static void lcl_AppendTwoDigits( TStringBuffer
&rBuffer
, sal_Int32 nNum
)
19 if ( nNum
< 0 || nNum
> 99 )
21 rBuffer
.append( "00" );
26 rBuffer
.append( '0' );
28 rBuffer
.append( nNum
);
31 template<class TString
, class TStringBuffer
>
32 static TString
DateTimeToStringImpl( const DateTime
& rDateTime
)
34 const DateTime
& aInUTC( rDateTime
);
35 // HACK: this is correct according to the spec, but MSOffice believes everybody lives
36 // in UTC+0 when reading it back
37 // aInUTC.ConvertToUTC();
39 TStringBuffer
aBuffer( 25 );
40 aBuffer
.append( sal_Int32( aInUTC
.GetYear() ) );
41 aBuffer
.append( '-' );
43 lcl_AppendTwoDigits( aBuffer
, aInUTC
.GetMonth() );
44 aBuffer
.append( '-' );
46 lcl_AppendTwoDigits( aBuffer
, aInUTC
.GetDay() );
47 aBuffer
.append( 'T' );
49 lcl_AppendTwoDigits( aBuffer
, aInUTC
.GetHour() );
50 aBuffer
.append( ':' );
52 lcl_AppendTwoDigits( aBuffer
, aInUTC
.GetMin() );
53 aBuffer
.append( ':' );
55 lcl_AppendTwoDigits( aBuffer
, aInUTC
.GetSec() );
56 aBuffer
.append( 'Z' ); // we are in UTC
58 return aBuffer
.makeStringAndClear();
61 OString
DateTimeToOString( const DateTime
& rDateTime
)
63 return DateTimeToStringImpl
<OString
,OStringBuffer
>(rDateTime
);
66 OUString
DateTimeToOUString( const DateTime
& rDateTime
)
68 return DateTimeToStringImpl
<OUString
,OUStringBuffer
>(rDateTime
);
71 OString
DateToOString( const Date
& rDate
)
73 tools::Time
aTime( tools::Time::EMPTY
);
74 return DateTimeToOString( DateTime( rDate
, aTime
) );
77 OUString
DateToDDMMYYYYOUString( const Date
& rDate
)
79 OUStringBuffer
aBuffer( 25 );
80 lcl_AppendTwoDigits( aBuffer
, rDate
.GetDay() );
81 aBuffer
.append( '/' );
83 lcl_AppendTwoDigits( aBuffer
, rDate
.GetMonth() );
84 aBuffer
.append( '/' );
86 aBuffer
.append( sal_Int32( rDate
.GetYear() ) );
88 return aBuffer
.makeStringAndClear();
91 std::ostream
& operator<<(std::ostream
& os
, const Date
& rDate
)
93 os
<< rDate
.GetYear() << "-" << rDate
.GetMonth() << "-" << rDate
.GetDay();