summaryrefslogtreecommitdiffstats
path: root/kugar/lib/mutil.cpp
blob: e53c1ef245ce8cc8f25d50acef2eb0a5aa7d8199 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/***************************************************************************
             mutil.cpp  -  Kugar utlity class
             -------------------
   begin     : Tue Aug 17 1999
   copyright : (C) 1999 by Mutiny Bay Software
   email     : info@mutinybaysoftware.com
   copyright : (C) 2002 Alexander Dymo
   email     : cloudtemple@mksat.net
***************************************************************************/

#include <math.h>

#include "mutil.h"


namespace Kugar
{

/** Formats a date using one of the pre-defined formats */
TQString MUtil::formatDate( const TQDate& value, int format )
{
    TQString string;
    TQString month, day, year;

    // Reformat date parts - not necessarily required
    month.setNum( value.month() );
    day.setNum( value.day() );

    // Add leading zeros to month and day
    if ( value.month() < 10 )
        month = "0" + month;
    if ( value.day() < 10 )
        day = "0" + day;

    // Create a two digit year
    year.setNum( value.year() );
    year = year.right( 2 );

    // Create the formatted date string
    switch ( format )
    {
    case MUtil::MDY_SLASH:
        string = TQString( "%1/%2/%3" ).arg( value.month() ).arg( value.day() ).arg( year );
        break;
    case MUtil::MDY_DASH:
        string = TQString( "%1-%2-%3" ).arg( value.month() ).arg( value.day() ).arg( year );
        break;
    case MUtil::MMDDY_SLASH:
        string = TQString( "%1/%2/%3" ).arg( month ).arg( day ).arg( year );
        break;
    case MUtil::MMDDY_DASH:
        string = TQString( "%1-%2-%3" ).arg( month ).arg( day ).arg( year );
        break;
    case MUtil::MDYYYY_SLASH:
        string = TQString( "%1/%2/%3" ).arg( value.month() ).arg( value.day() ).arg( value.year() );
        break;
    case MUtil::MDYYYY_DASH:
        string = TQString( "%1-%2-%3" ).arg( value.month() ).arg( value.day() ).arg( value.year() );
        break;
    case MUtil::MMDDYYYY_SLASH:
        string = TQString( "%1/%2/%3" ).arg( month ).arg( day ).arg( value.year() );
        break;
    case MUtil::MMDDYYYY_DASH:
        string = TQString( "%1-%2-%3" ).arg( month ).arg( day ).arg( value.year() );
        break;
    case MUtil::YYYYMD_SLASH:
        string = TQString( "%1/%2/%3" ).arg( value.year() ).arg( value.month() ).arg( value.day() );
        break;
    case MUtil::YYYYMD_DASH:
        string = TQString( "%1-%2-%3" ).arg( value.year() ).arg( value.month() ).arg( value.day() );
        break;
    case MUtil::DDMMYY_PERIOD:
        string = TQString( "%1.%2.%3" ).arg( day ).arg( month ).arg( year );
        break;
    case MUtil::DDMMYYYY_PERIOD:
        string = TQString( "%1.%2.%3" ).arg( day ).arg( month ).arg( value.year() );
        break;
    default:
        string = value.toString();
    }
    return string;
}

/** Returns the count for an array of doubles */
int MUtil::count( TQMemArray<double>* values )
{
    return values->size();
}

/** Returns the sum for an array of doubles */
double MUtil::sum( TQMemArray<double>* values )
{
    double tmpSum = 0.0;
    int size = count( values );

    for ( int i = 0; i < size; i++ )
        tmpSum += values->at( i );

    return tmpSum;
}

/** Returns the average value for an array of doubles */
double MUtil::average( TQMemArray<double>* values )
{
    return sum( values ) / count( values );
}

/** Returns the variance for an array of doubles */
double MUtil::variance( TQMemArray<double>* values )
{
    double tmpVar = 0.0;
    double tmpAvg = average( values );
    int size = count( values );

    for ( int i = 0; i < size; i++ )
        tmpVar += pow( ( values->at( i ) - tmpAvg ), 2 ) / size;

    return tmpVar;
}

/** Returns the standard deviation for an array of doubles */
double MUtil::stdDeviation( TQMemArray<double>* values )
{
    return sqrt( variance( values ) );
}

}