LibreOffice Module tools (master) 1
datetimeutils.cxx
Go to the documentation of this file.
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
11#include <rtl/strbuf.hxx>
12
13
15static void lcl_AppendTwoDigits( OStringBuffer &rBuffer, sal_Int32 nNum )
16{
17 if ( nNum < 0 || nNum > 99 )
18 {
19 rBuffer.append( "00" );
20 return;
21 }
22
23 if ( nNum < 10 )
24 rBuffer.append( '0' );
25
26 rBuffer.append( nNum );
27}
28
29OString DateTimeToOString( const DateTime& rDateTime )
30{
31 const 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 OStringBuffer aBuffer( 25 );
37 aBuffer.append( sal_Int32( aInUTC.GetYear() ) );
38 aBuffer.append( '-' );
39
41 aBuffer.append( '-' );
42
44 aBuffer.append( 'T' );
45
47 aBuffer.append( ':' );
48
50 aBuffer.append( ':' );
51
53 aBuffer.append( 'Z' ); // we are in UTC
54
55 return aBuffer.makeStringAndClear();
56}
57
58OString DateToOString( const Date& rDate )
59{
61 return DateTimeToOString( DateTime( rDate, aTime ) );
62}
63
64OString DateToDDMMYYYYOString( const Date& rDate )
65{
66 OStringBuffer aBuffer( 25 );
68 aBuffer.append( '/' );
69
71 aBuffer.append( '/' );
72
73 aBuffer.append( sal_Int32( rDate.GetYear() ) );
74
75 return aBuffer.makeStringAndClear();
76}
77
78std::ostream& operator<<(std::ostream& os, const Date& rDate)
79{
80 os << rDate.GetYear() << "-" << rDate.GetMonth() << "-" << rDate.GetDay();
81 return os;
82}
Represents a date in the proleptic Gregorian calendar.
Definition: date.hxx:55
sal_Int16 GetYear() const
Definition: date.hxx:113
sal_uInt16 GetDay() const
Definition: date.hxx:101
sal_uInt16 GetMonth() const
Definition: date.hxx:107
sal_uInt16 GetSec() const
Definition: time.hxx:91
sal_uInt16 GetMin() const
Definition: time.hxx:88
sal_uInt16 GetHour() const
Definition: time.hxx:85
OString DateTimeToOString(const DateTime &rDateTime)
static void lcl_AppendTwoDigits(OStringBuffer &rBuffer, sal_Int32 nNum)
Append the number as 2-digit when less than 10.
OString DateToOString(const Date &rDate)
std::ostream & operator<<(std::ostream &os, const Date &rDate)
OString DateToDDMMYYYYOString(const Date &rDate)
std::unique_ptr< char[]> aBuffer