/src/libreoffice/tools/source/datetime/datetimeutils.cxx
Line | Count | Source |
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 <tools/datetime.hxx> |
12 | | #include <rtl/strbuf.hxx> |
13 | | #include <rtl/ustrbuf.hxx> |
14 | | |
15 | | |
16 | | /// Append the number as 2-digit when less than 10. |
17 | | template<class TStringBuffer> |
18 | | static void lcl_AppendTwoDigits( TStringBuffer &rBuffer, sal_Int32 nNum ) |
19 | 200 | { |
20 | 200 | if ( nNum < 0 || nNum > 99 ) |
21 | 0 | { |
22 | 0 | rBuffer.append( "00" ); |
23 | 0 | return; |
24 | 0 | } |
25 | | |
26 | 200 | if ( nNum < 10 ) |
27 | 82 | rBuffer.append( '0' ); |
28 | | |
29 | 200 | rBuffer.append( nNum ); |
30 | 200 | } Unexecuted instantiation: datetimeutils.cxx:void lcl_AppendTwoDigits<rtl::OStringBuffer>(rtl::OStringBuffer&, int) datetimeutils.cxx:void lcl_AppendTwoDigits<rtl::OUStringBuffer>(rtl::OUStringBuffer&, int) Line | Count | Source | 19 | 200 | { | 20 | 200 | if ( nNum < 0 || nNum > 99 ) | 21 | 0 | { | 22 | 0 | rBuffer.append( "00" ); | 23 | 0 | return; | 24 | 0 | } | 25 | | | 26 | 200 | if ( nNum < 10 ) | 27 | 82 | rBuffer.append( '0' ); | 28 | | | 29 | 200 | rBuffer.append( nNum ); | 30 | 200 | } |
|
31 | | |
32 | | template<class TString, class TStringBuffer> |
33 | | static TString DateTimeToStringImpl( const DateTime& rDateTime ) |
34 | 40 | { |
35 | 40 | const DateTime& aInUTC( rDateTime ); |
36 | | // HACK: this is correct according to the spec, but MSOffice believes everybody lives |
37 | | // in UTC+0 when reading it back |
38 | | // aInUTC.ConvertToUTC(); |
39 | | |
40 | 40 | TStringBuffer aBuffer( 25 ); |
41 | 40 | aBuffer.append( sal_Int32( aInUTC.GetYear() ) ); |
42 | 40 | aBuffer.append( '-' ); |
43 | | |
44 | 40 | lcl_AppendTwoDigits( aBuffer, aInUTC.GetMonth() ); |
45 | 40 | aBuffer.append( '-' ); |
46 | | |
47 | 40 | lcl_AppendTwoDigits( aBuffer, aInUTC.GetDay() ); |
48 | 40 | aBuffer.append( 'T' ); |
49 | | |
50 | 40 | lcl_AppendTwoDigits( aBuffer, aInUTC.GetHour() ); |
51 | 40 | aBuffer.append( ':' ); |
52 | | |
53 | 40 | lcl_AppendTwoDigits( aBuffer, aInUTC.GetMin() ); |
54 | 40 | aBuffer.append( ':' ); |
55 | | |
56 | 40 | lcl_AppendTwoDigits( aBuffer, aInUTC.GetSec() ); |
57 | 40 | aBuffer.append( 'Z' ); // we are in UTC |
58 | | |
59 | 40 | return aBuffer.makeStringAndClear(); |
60 | 40 | } Unexecuted instantiation: datetimeutils.cxx:rtl::OString DateTimeToStringImpl<rtl::OString, rtl::OStringBuffer>(DateTime const&) datetimeutils.cxx:rtl::OUString DateTimeToStringImpl<rtl::OUString, rtl::OUStringBuffer>(DateTime const&) Line | Count | Source | 34 | 40 | { | 35 | 40 | const DateTime& aInUTC( rDateTime ); | 36 | | // HACK: this is correct according to the spec, but MSOffice believes everybody lives | 37 | | // in UTC+0 when reading it back | 38 | | // aInUTC.ConvertToUTC(); | 39 | | | 40 | 40 | TStringBuffer aBuffer( 25 ); | 41 | 40 | aBuffer.append( sal_Int32( aInUTC.GetYear() ) ); | 42 | 40 | aBuffer.append( '-' ); | 43 | | | 44 | 40 | lcl_AppendTwoDigits( aBuffer, aInUTC.GetMonth() ); | 45 | 40 | aBuffer.append( '-' ); | 46 | | | 47 | 40 | lcl_AppendTwoDigits( aBuffer, aInUTC.GetDay() ); | 48 | 40 | aBuffer.append( 'T' ); | 49 | | | 50 | 40 | lcl_AppendTwoDigits( aBuffer, aInUTC.GetHour() ); | 51 | 40 | aBuffer.append( ':' ); | 52 | | | 53 | 40 | lcl_AppendTwoDigits( aBuffer, aInUTC.GetMin() ); | 54 | 40 | aBuffer.append( ':' ); | 55 | | | 56 | 40 | lcl_AppendTwoDigits( aBuffer, aInUTC.GetSec() ); | 57 | 40 | aBuffer.append( 'Z' ); // we are in UTC | 58 | | | 59 | 40 | return aBuffer.makeStringAndClear(); | 60 | 40 | } |
|
61 | | |
62 | | OString DateTimeToOString( const DateTime& rDateTime ) |
63 | 0 | { |
64 | 0 | return DateTimeToStringImpl<OString,OStringBuffer>(rDateTime); |
65 | 0 | } |
66 | | |
67 | | OUString DateTimeToOUString( const DateTime& rDateTime ) |
68 | 40 | { |
69 | 40 | return DateTimeToStringImpl<OUString,OUStringBuffer>(rDateTime); |
70 | 40 | } |
71 | | |
72 | | OString DateToOString( const Date& rDate ) |
73 | 0 | { |
74 | 0 | tools::Time aTime( tools::Time::EMPTY ); |
75 | 0 | return DateTimeToOString( DateTime( rDate, aTime ) ); |
76 | 0 | } |
77 | | |
78 | | OUString DateToDDMMYYYYOUString( const Date& rDate ) |
79 | 0 | { |
80 | 0 | OUStringBuffer aBuffer( 25 ); |
81 | 0 | lcl_AppendTwoDigits( aBuffer, rDate.GetDay() ); |
82 | 0 | aBuffer.append( '/' ); |
83 | |
|
84 | 0 | lcl_AppendTwoDigits( aBuffer, rDate.GetMonth() ); |
85 | 0 | aBuffer.append( '/' ); |
86 | |
|
87 | 0 | aBuffer.append( sal_Int32( rDate.GetYear() ) ); |
88 | |
|
89 | 0 | return aBuffer.makeStringAndClear(); |
90 | 0 | } |
91 | | |
92 | | std::ostream& operator<<(std::ostream& os, const Date& rDate) |
93 | 0 | { |
94 | 0 | os << rDate.GetYear() << "-" << rDate.GetMonth() << "-" << rDate.GetDay(); |
95 | 0 | return os; |
96 | 0 | } |