Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : */
9 :
10 : #include <tools/datetimeutils.hxx>
11 : #include <rtl/strbuf.hxx>
12 :
13 :
14 : /// Append the number as 2-digit when less than 10.
15 0 : static void lcl_AppendTwoDigits( OStringBuffer &rBuffer, sal_Int32 nNum )
16 : {
17 0 : if ( nNum < 0 || nNum > 99 )
18 : {
19 0 : rBuffer.append( "00" );
20 0 : return;
21 : }
22 :
23 0 : if ( nNum < 10 )
24 0 : rBuffer.append( '0' );
25 :
26 0 : rBuffer.append( nNum );
27 : }
28 :
29 0 : OString DateTimeToOString( const DateTime& rDateTime )
30 : {
31 0 : DateTime aInUTC( rDateTime );
32 : // HACK: this is correct according to the spec, but MSOffice believes everybody lives
33 : // in UTC+0 when reading it back
34 : // aInUTC.ConvertToUTC();
35 :
36 0 : OStringBuffer aBuffer( 25 );
37 0 : aBuffer.append( sal_Int32( aInUTC.GetYear() ) );
38 0 : aBuffer.append( '-' );
39 :
40 0 : lcl_AppendTwoDigits( aBuffer, aInUTC.GetMonth() );
41 0 : aBuffer.append( '-' );
42 :
43 0 : lcl_AppendTwoDigits( aBuffer, aInUTC.GetDay() );
44 0 : aBuffer.append( 'T' );
45 :
46 0 : lcl_AppendTwoDigits( aBuffer, aInUTC.GetHour() );
47 0 : aBuffer.append( ':' );
48 :
49 0 : lcl_AppendTwoDigits( aBuffer, aInUTC.GetMin() );
50 0 : aBuffer.append( ':' );
51 :
52 0 : lcl_AppendTwoDigits( aBuffer, aInUTC.GetSec() );
53 0 : aBuffer.append( 'Z' ); // we are in UTC
54 :
55 0 : return aBuffer.makeStringAndClear();
56 : }
57 :
58 0 : OString DateToOString( const Date& rDate )
59 : {
60 0 : Time aTime( Time::EMPTY );
61 0 : return DateTimeToOString( DateTime( rDate, aTime ) );
62 : }
63 :
64 0 : OString DateToDDMMYYYYOString( const Date& rDate )
65 : {
66 0 : OStringBuffer aBuffer( 25 );
67 0 : lcl_AppendTwoDigits( aBuffer, rDate.GetDay() );
68 0 : aBuffer.append( '/' );
69 :
70 0 : lcl_AppendTwoDigits( aBuffer, rDate.GetMonth() );
71 0 : aBuffer.append( '/' );
72 :
73 0 : aBuffer.append( sal_Int32( rDate.GetYear() ) );
74 :
75 0 : return aBuffer.makeStringAndClear();
76 : }
|