Click here to Skip to main content
15,892,537 members
Articles / Desktop Programming / MFC

What's New in ACF 0.3

Rate me:
Please Sign up or sign in to vote.
3.39/5 (7 votes)
23 Jun 20043 min read 40.5K   193   8  
This article introduces what's new in ACF (Another C++ Framework) version 0.3.
//---------------------------------------------------------------------
//
// Copyright (C) 2004 Yingle Jia
//
// Permission to copy, use, modify, sell and distribute this software is 
// granted provided this copyright notice appears in all copies. 
// This software is provided "as is" without express or implied warranty, 
// and with no claim as to its suitability for any purpose.
//

#include "stdafx.h"

namespace Acf {

using namespace Acf::Text;

const int MillisecondsPerSecond = 1000;
const int MillisecondsPerMinute = MillisecondsPerSecond * 60;
const int MillisecondsPerHour = MillisecondsPerMinute * 60;
const int MillisecondsPerDay = MillisecondsPerHour * 24;

const int64 MaxSeconds = Int64::MaxValue / TimeSpan::TicksPerSecond;
const int64 MinSeconds = Int64::MinValue / TimeSpan::TicksPerSecond;

const int64 MaxMilliSeconds = Int64::MaxValue / TimeSpan::TicksPerMillisecond;
const int64 MinMilliSeconds = Int64::MinValue / TimeSpan::TicksPerMillisecond;


//---------------------------------------------------------------------
// struct TimeSpan

// Static Fields

/*static*/ const int64 TimeSpan::TicksPerMillisecond = 10000;
/*static*/ const int64 TimeSpan::TicksPerSecond = TimeSpan::TicksPerMillisecond * 1000;
/*static*/ const int64 TimeSpan::TicksPerMinute = TimeSpan::TicksPerSecond * 60;
/*static*/ const int64 TimeSpan::TicksPerHour = TimeSpan::TicksPerMinute * 60;
/*static*/ const int64 TimeSpan::TicksPerDay = TimeSpan::TicksPerHour * 24;

/*static*/ const TimeSpan TimeSpan::Zero = TimeSpan(0);
/*static*/ const TimeSpan TimeSpan::MinValue = TimeSpan(Int64::MinValue);
/*static*/ const TimeSpan TimeSpan::MaxValue = TimeSpan(Int64::MaxValue);

// Constructors

TimeSpan::TimeSpan(int64 ticks)
{
    this->_ticks = ticks;
}

TimeSpan::TimeSpan(int hours, int minutes, int seconds)
{
    int64 totalMilliSeconds = ((int64)hours * 3600 + minutes * 60 + seconds) * 1000;

    if (totalMilliSeconds > MaxMilliSeconds || totalMilliSeconds < MinMilliSeconds)
        throw ArgumentOutOfRangeException();

    this->_ticks =  (int64)totalMilliSeconds * TimeSpan::TicksPerMillisecond;
}

TimeSpan::TimeSpan(int days, int hours, int minutes, int seconds)
{
    int64 totalMilliSeconds = ((int64)days * 3600 * 24 + hours * 3600 + minutes * 60 + seconds) * 1000;

    if (totalMilliSeconds > MaxMilliSeconds || totalMilliSeconds < MinMilliSeconds)
        throw ArgumentOutOfRangeException();

    this->_ticks =  (int64)totalMilliSeconds * TimeSpan::TicksPerMillisecond;
}

TimeSpan::TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds)
{
    int64 totalMilliSeconds = ((int64)days * 3600 * 24 + hours * 3600 + minutes * 60 + seconds) * 1000 + milliseconds;

    if (totalMilliSeconds > MaxMilliSeconds || totalMilliSeconds < MinMilliSeconds)
        throw ArgumentOutOfRangeException();

    this->_ticks =  (int64)totalMilliSeconds * TimeSpan::TicksPerMillisecond;
}

// Properties

int64 TimeSpan::get_Ticks() const
{
    return this->_ticks;
}

int TimeSpan::get_Days() const
{
    return (int)(this->_ticks / TimeSpan::TicksPerDay);
}

int TimeSpan::get_Hours() const
{
    return (int)((this->_ticks / TimeSpan::TicksPerHour) % 24);
}

int TimeSpan::get_Minutes() const
{
    return (int)((this->_ticks / TimeSpan::TicksPerMinute) % 60);
}

int TimeSpan::get_Seconds() const
{
    return (int)((this->_ticks / TimeSpan::TicksPerSecond) % 60);
}

int TimeSpan::get_Milliseconds() const
{
    return (int)((this->_ticks / TimeSpan::TicksPerMillisecond) % 1000);
}

double TimeSpan::get_TotalDays() const
{
    return ((double)this->_ticks) / (double)TimeSpan::TicksPerDay;
}

double TimeSpan::get_TotalHours() const
{
    return ((double)this->_ticks) / (double)TimeSpan::TicksPerHour;
}

double TimeSpan::get_TotalMinutes() const
{
    return ((double)this->_ticks) / (double)TimeSpan::TicksPerMinute;
}

double TimeSpan::get_TotalSeconds() const
{
    return ((double)this->_ticks) / (double)TimeSpan::TicksPerSecond;
}

double TimeSpan::get_TotalMilliseconds() const
{
    double temp = (double)this->_ticks / (double)TimeSpan::TicksPerMillisecond;

    if (temp > MaxMilliSeconds)
        return (double)MaxMilliSeconds;
    if (temp < MinMilliSeconds)
        return (double)MinMilliSeconds;

    return temp;
}

// Static Methods

/*static*/ bool TimeSpan::Equals(const TimeSpan& t1, const TimeSpan& t2)
{
    return (t1.Ticks == t2.Ticks);
}

/*static*/ int TimeSpan::Compare(const TimeSpan& t1, const TimeSpan& t2)
{
    if (t1.Ticks > t2.Ticks)
        return 1;
    if (t1.Ticks < t2.Ticks)
        return -1;
    return 0;
}

/*static*/ TimeSpan TimeSpan::FromTicks(int64 value)
{
    return TimeSpan(value);
}

/*static*/ TimeSpan TimeSpan::FromDays(double value)
{
    return FromMilliseconds(value * MillisecondsPerDay);
}

/*static*/ TimeSpan TimeSpan::FromHours(double value)
{
    return FromMilliseconds(value * MillisecondsPerHour);
}

/*static*/ TimeSpan TimeSpan::FromMinutes(double value)
{
    return FromMilliseconds(value * MillisecondsPerMinute);
}

/*static*/ TimeSpan TimeSpan::FromSeconds(double value)
{
    return FromMilliseconds(value * MillisecondsPerSecond);
}

/*static*/ TimeSpan TimeSpan::FromMilliseconds(double value)
{
    if (value > MaxMilliSeconds || value < MinMilliSeconds)
        throw ArgumentOutOfRangeException();

    return TimeSpan((int64)value * TimeSpan::TicksPerMillisecond);
}

// [-][d.]hh:mm:ss[.ff]
/*static*/ TimeSpan TimeSpan::Parse(String* s)
{
    // TODO: TimeSpan::Parse
    throw NotImplementedException();
}

// Methods

TimeSpan TimeSpan::Add(const TimeSpan& ts)
{
    int64 result = this->_ticks + ts.Ticks;

    if ((this->_ticks >> 63 == ts.Ticks >> 63) && 
        (this->_ticks >> 63 != result >> 63))
        throw ArgumentException();

    return TimeSpan(result);
}

TimeSpan TimeSpan::Subtract(const TimeSpan& ts)
{
    int64 result = this->_ticks - ts.Ticks;

    if ((this->_ticks >> 63 == ts.Ticks >> 63) && 
        (this->_ticks >> 63 != result >> 63))
        throw ArgumentException();

    return TimeSpan(result);
}

TimeSpan TimeSpan::Duration()
{
    return TimeSpan(this->_ticks >= 0 ? this->_ticks: -this->_ticks);
}

TimeSpan TimeSpan::Negate()
{
    return TimeSpan(-this->_ticks);
}

int TimeSpan::GetHashCode()
{
    return (int)this->_ticks ^ (int)(this->_ticks >> 32);
}

// Returns "[-][d.]hh:mm:ss[.ff]"
StringPtr TimeSpan::ToString()
{
    StringBuilderPtr sb = new StringBuilder();

    int day = (int)(this->_ticks / TimeSpan::TicksPerDay);
    int64 time = this->_ticks % TimeSpan::TicksPerDay;

    // Sign
    if (this->_ticks < 0)
    {
        sb->Append(L'-');
        day = -day;
        time = -time;
    }

    // Days
    if (day != 0)
    {
        sb->Append(day);
        sb->Append(L'.');
    }

    // Hours
    sb->Append(Number::FormatDecimal((uint)(time / TimeSpan::TicksPerHour % 24), 2));
    sb->Append(L':');

    // Minutes
    sb->Append(Number::FormatDecimal((uint)(time / TimeSpan::TicksPerMinute % 60), 2));
    sb->Append(L':');

    // Seconds
    sb->Append(Number::FormatDecimal((uint)(time / TimeSpan::TicksPerSecond % 60), 2));
    int f = (int)(time % TimeSpan::TicksPerSecond);
    if (f != 0)
    {
        sb->Append(L'.');
        sb->Append(Number::FormatDecimal((uint)f, 7));
    }

    return sb->ToString();
}


//---------------------------------------------------------------------
// class Boxed<TimeSpan>

// Methods

//
// Object Members
//

/*override*/ bool Boxed<TimeSpan>::Equals(Object* obj)
{
    Boxed<TimeSpan>* o = dynamic_cast<Boxed<TimeSpan>*>(obj);
    if (o == null)
        return false;
    else
        return (this->Value == o->Value);
}

/*override*/ int Boxed<TimeSpan>::GetHashCode()
{
    return this->Value.GetHashCode();
}

/*override*/ StringPtr Boxed<TimeSpan>::ToString()
{
    return this->Value.ToString();
}

} // namespace Acf

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
Yingle Jia is a software engineer located in Beijing, China. He currently works at IBM CSDL (China Software Development Lab). His interests include C++/COM/C#/.NET/XML, etc. He likes coding and writing.

He is the creator of ACF (Another C++ Framework) project. See http://acfproj.sourceforge.net/.

He also has a blog at http://blogs.wwwcoder.com/yljia/

Comments and Discussions