File indexing completed on 2024-04-28 05:52:07

0001 /*
0002     SPDX-FileCopyrightText: 2007-2009 Stefan Böhmann <kde@hilefoks.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "tea.h"
0007 
0008 #include <KLocalizedString>
0009 
0010 
0011 Tea::Tea(const QString &name, const unsigned time)
0012   : m_time( 0 )
0013 {
0014     setName( name );
0015     setTime( time );
0016 }
0017 
0018 
0019 QString Tea::name() const
0020 {
0021     return m_name;
0022 }
0023 
0024 
0025 void Tea::setName(const QString &name)
0026 {
0027     if( name.isEmpty() ) {
0028         m_name = i18n( "Custom Tea" );
0029     }
0030     else {
0031         m_name = name;
0032     }
0033 }
0034 
0035 
0036 unsigned Tea::time() const
0037 {
0038     return m_time;
0039 }
0040 
0041 
0042 void Tea::setTime(const unsigned time)
0043 {
0044     m_time = time;
0045 }
0046 
0047 
0048 QString Tea::timeToString(const bool longdesc) const
0049 {
0050     return int2time( m_time, longdesc );
0051 }
0052 
0053 
0054 QString Tea::int2time(const int time, const bool longdesc)
0055 {
0056     QString str;
0057     const unsigned min = 60;
0058     const unsigned hour = 60 * min;
0059     const unsigned day = 24 * hour;
0060     const unsigned year = 365 * day;
0061 
0062     if( time / year ) {
0063         if( longdesc ) {
0064             str.append( i18np( "%1 year", "%1 years", time/year ) );
0065         }
0066         else {
0067             str.append( i18np( "%1 a", "%1 a", time/year ) );
0068         }
0069     }
0070 
0071     if( (time % year) / day ) {
0072         if( !str.isEmpty() ) {
0073             str.append( QLatin1Char( ' ' ) );
0074         }
0075 
0076         if( longdesc ) {
0077             str.append( i18np( "%1 day", "%1 days", (time % year) / day ) );
0078         }
0079         else {
0080             str.append( i18np( "%1 d", "%1 d", (time % year) / day ) );
0081         }
0082     }
0083 
0084     if( (time % day) / hour ) {
0085         if( !str.isEmpty() ) {
0086             str.append( QLatin1Char( ' ' ) );
0087         }
0088 
0089         if( longdesc ) {
0090             str.append( i18np( "%1 hour", "%1 hours", ( (time%day ) / hour ) ) );
0091         }
0092         else {
0093             str.append( i18np( "%1 h", "%1 h", ( (time % day) / hour) ) );
0094         }
0095     }
0096 
0097     if( (time % hour) / min ) {
0098         if( !str.isEmpty() ) {
0099             str.append( QLatin1Char( ' ' ) );
0100         }
0101 
0102         if( longdesc ) {
0103             str.append( i18np( "%1 minute", "%1 minutes", ( (time % hour) / min) ) );
0104         }
0105         else {
0106             str.append( i18np( "%1 min", "%1 min", ( (time % hour) / min) ) );
0107         }
0108     }
0109 
0110     if( time % min ) {
0111         if( !str.isEmpty() ) {
0112             str.append( QLatin1Char( ' ' ) );
0113         }
0114 
0115         if( longdesc ) {
0116             str.append( i18np( "%1 second", "%1 seconds", time % min ) );
0117         }
0118         else {
0119             str.append( i18np( "%1 s", "%1 s", time % min ) );
0120         }
0121     }
0122 
0123     return str;
0124 }
0125 
0126 // kate: word-wrap off; encoding utf-8; indent-width 4; tab-width 4; line-numbers on; mixed-indent off; remove-trailing-space-save on; replace-tabs-save on; replace-tabs on; space-indent on;
0127 // vim:set spell et sw=4 ts=4 nowrap cino=l1,cs,U1:
0128 
0129