Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / base / FBaseDateTime.cpp
index ced0cea..4a691d9 100644 (file)
@@ -1,5 +1,4 @@
 //
-// Open Service Platform
 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
 //
 // Licensed under the Apache License, Version 2.0 (the License);
@@ -27,7 +26,6 @@
 #include <FBaseSysLog.h>
 #include <unique_ptr.h>
 
-
 namespace Tizen { namespace Base
 {
 
@@ -39,24 +37,31 @@ enum DateTimeLimits
        MIN_HOUR = 0,
        MIN_MINUTE = 0,
        MIN_SECOND = 0,
+       MIN_TICK = 0,
        _MAX_YEAR = 9999,
        MAX_MONTH = 12,
        MAX_DAY = 31,
        MAX_HOUR = 23,
        MAX_MINUTE = 59,
        MAX_SECOND = 59,
+       MAX_TICK = 999,
        NUM_OF_SEC_IN_DAY = 86400,
        NUM_OF_SEC_IN_HOUR = 3600,
        NUM_OF_SEC_IN_MINUTE = 60,
-       NUM_OF_MILLISEC_IN_DAY = 86400000LL,
-       NUM_OF_MILLISEC_IN_HOUR = 3600000LL,
-       NUM_OF_MILLISEC_IN_MINUTE = 60000LL
+       NUM_OF_TICKS_IN_DAY = 86400000LL,
+       NUM_OF_TICKS_IN_HOUR = 3600000LL,
+       NUM_OF_TICKS_IN_MINUTE = 60000LL,
+       NUM_OF_TICKS_IN_SECOND = 1000LL
 };
 
+static const int TICKS_PER_MILLISECOND = 1;
 static const long SEC_IN_A_DAY = (24L * 60L * 60L);
 static const long long MINIMUM_VALUE_IN_SEC = 86400LL;
 static const int DAYS_IN_LEAP_YEAR[] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
 static const int DAYS[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
+static const int INT_HALF_BIT = 16;
+static const int LOW_16BIT = 0xFFFF;
+static const long long TOTAL_MAX_TICKS = DateTime::GetMaxValue().GetTicks();
 
 DateTime::DateTime(void)
        : __pDateTimeImpl(null)
@@ -77,15 +82,14 @@ DateTime::~DateTime(void)
 result
 DateTime::SetValue(const TimeSpan& value)
 {
-       long long total = (long long) (value.GetTicks() / 1000) + MINIMUM_VALUE_IN_SEC;
-       SysTryReturn(NID_BASE, total >= NUM_OF_SEC_IN_DAY, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
+       long long total = value.GetTicks();
+       SysTryReturn(NID_BASE, total >= MIN_TICK, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
                "[%s] The value of the argument is outside the valid range.", GetErrorMessage(E_OUT_OF_RANGE));
 
-       long long maxSeconds = (long long) (DateTime::GetMaxValue().GetTime().GetTicks() / 1000) + MINIMUM_VALUE_IN_SEC;
-       SysTryReturn(NID_BASE, total <= maxSeconds, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
+       SysTryReturn(NID_BASE, total <= TOTAL_MAX_TICKS, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
                "[%s] The value of the argument is outside the valid range.", GetErrorMessage(E_OUT_OF_RANGE));
 
-       return ConvertSecondsToDate(total, &__dateTime);
+       return ConvertTicksToDate(total, &__dateTime);
 }
 
 void
@@ -102,21 +106,22 @@ DateTime::SetValue(const DateTime& value)
 result
 DateTime::SetValue(int year, int month, int day, int hour, int minute, int second)
 {
-       SysTryReturn(NID_BASE, 
+       SysTryReturn(NID_BASE,
                ((year >= MIN_YEAR && year <= _MAX_YEAR) &&
                 (month >= MIN_MONTH && month <= MAX_MONTH) &&
                 (hour >= MIN_HOUR && hour <= MAX_HOUR) &&
                 (minute >= MIN_MINUTE && minute <= MAX_MINUTE) &&
-                (second >= MIN_SECOND && second <= MAX_SECOND)),
+                ((second & LOW_16BIT) >= MIN_SECOND && (second & LOW_16BIT) <= MAX_SECOND) &&
+                ((second >> INT_HALF_BIT) >= MIN_TICK && (second >> INT_HALF_BIT) <= MAX_TICK)),
                E_OUT_OF_RANGE, E_OUT_OF_RANGE,
-               "[%s] One of the year(%d), month(%d), day(%d), hour(%d), minute(%d), and second(%d) is in out of allowable range.",
-               GetErrorMessage(E_OUT_OF_RANGE), year, month, day, hour, minute, second);
+               "[%s] One of the year(%d), month(%d), day(%d), hour(%d), minute(%d), second(%d) and tick(%d) is out of allowable range.",
+               GetErrorMessage(E_OUT_OF_RANGE), year, month, day, hour, minute, second & LOW_16BIT, second >> INT_HALF_BIT);
 
        int daysInMonth = 0;
        result r = GetDaysInMonth(year, month, daysInMonth);
        SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
        SysTryReturn(NID_BASE, (day >= MIN_DAY && day <= daysInMonth), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
-               ("[%s] day is in out of allowable range."), GetErrorMessage(E_OUT_OF_RANGE));
+               ("[%s] day is out of allowable range."), GetErrorMessage(E_OUT_OF_RANGE));
 
        __dateTime.year = year;
        __dateTime.month = month;
@@ -124,8 +129,42 @@ DateTime::SetValue(int year, int month, int day, int hour, int minute, int secon
        __dateTime.hour = hour;
        __dateTime.minute = minute;
        __dateTime.second = second;
+       return E_SUCCESS;
+}
+
+result
+DateTime::SetValue(int year, int month, int day, int hour, int minute, int second, int millisecond)
+{
+       SysTryReturn(NID_BASE,
+               ((year >= MIN_YEAR && year <= _MAX_YEAR) &&
+                (month >= MIN_MONTH && month <= MAX_MONTH) &&
+                (hour >= MIN_HOUR && hour <= MAX_HOUR) &&
+                (minute >= MIN_MINUTE && minute <= MAX_MINUTE) &&
+                (second >= MIN_SECOND && second <= MAX_SECOND) &&
+                ((millisecond * TICKS_PER_MILLISECOND >= MIN_TICK) && (millisecond * TICKS_PER_MILLISECOND <= MAX_TICK))),
+               E_OUT_OF_RANGE, E_OUT_OF_RANGE,
+               "[%s] One of the year(%d), month(%d), day(%d), hour(%d), minute(%d), second(%d) and tick(%d) is out of allowable range.",
+               GetErrorMessage(E_OUT_OF_RANGE), year, month, day, hour, minute, second, millisecond * TICKS_PER_MILLISECOND);
+
+       int daysInMonth = 0;
+       result r = GetDaysInMonth(year, month, daysInMonth);
+       SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+       SysTryReturn(NID_BASE, (day >= MIN_DAY && day <= daysInMonth), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
+               ("[%s] day is out of allowable range."), GetErrorMessage(E_OUT_OF_RANGE));
 
-       return(E_SUCCESS);
+       __dateTime.year = year;
+       __dateTime.month = month;
+       __dateTime.day = day;
+       __dateTime.hour = hour;
+       __dateTime.minute = minute;
+       __dateTime.second = ((millisecond * TICKS_PER_MILLISECOND) << INT_HALF_BIT) | second;
+       return E_SUCCESS;
+}
+
+result
+DateTime::SetValue(long long ticks)
+{
+       return ConvertTicksToDate(ticks, &__dateTime);
 }
 
 DateTime&
@@ -136,7 +175,7 @@ DateTime::operator =(const DateTime& rhs)
                SetValue(rhs);
        }
 
-       return(*this);
+       return *this;
 }
 
 bool
@@ -196,7 +235,15 @@ DateTime::operator <(const DateTime& rhs) const
        {
                return false;
        }
-       if (__dateTime.second < rhs.__dateTime.second)
+       if ((__dateTime.second & LOW_16BIT) < (rhs.__dateTime.second & LOW_16BIT))
+       {
+               return true;
+       }
+       if ((__dateTime.second & LOW_16BIT) > (rhs.__dateTime.second & LOW_16BIT))
+       {
+               return false;
+       }
+       if ((__dateTime.second >> INT_HALF_BIT) < (rhs.__dateTime.second >> INT_HALF_BIT))
        {
                return true;
        }
@@ -247,44 +294,39 @@ DateTime::operator >(const DateTime& rhs) const
        {
                return false;
        }
-       if (__dateTime.second > rhs.__dateTime.second)
+       if ((__dateTime.second & LOW_16BIT) > (rhs.__dateTime.second & LOW_16BIT))
+       {
+               return true;
+       }
+       if ((__dateTime.second & LOW_16BIT) < (rhs.__dateTime.second & LOW_16BIT))
+       {
+               return false;
+       }
+       if ((__dateTime.second >> INT_HALF_BIT) > (rhs.__dateTime.second >> INT_HALF_BIT))
        {
                return true;
        }
+
        return false;
 }
 
 bool
 DateTime::operator <=(const DateTime& rhs) const
 {
-       return((*this == rhs) || (*this < rhs));
+       return ((*this == rhs) || (*this < rhs));
 }
 
 bool
 DateTime::operator >=(const DateTime& rhs) const
 {
-       return((*this == rhs) || (*this > rhs));
+       return ((*this == rhs) || (*this > rhs));
 }
 
 result
 DateTime::Add(const TimeSpan& t)
 {
-
-       long long seconds = ConvertDateToSeconds(&__dateTime);
-       long long total = (long long) (t.GetTicks() / 1000) + seconds;
-
-       SysTryReturn(NID_BASE, total >= NUM_OF_SEC_IN_DAY, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
-               "[%s] The value of the argument is outside the valid range.", GetErrorMessage(E_OUT_OF_RANGE));
-
-       long long maxSeconds = ConvertDateToSeconds(&(DateTime::GetMaxValue().__dateTime));
-       SysTryReturn(NID_BASE, total <= maxSeconds, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
-               "[%s] The value of the argument is outside the valid range.", GetErrorMessage(E_OUT_OF_RANGE));
-
-       result r = ConvertSecondsToDate(total, &__dateTime);
-       if (r != E_SUCCESS)
-       {
-               SysLogException(NID_BASE, r, "[%s] Propagating.", GetErrorMessage(r));
-       }
+       result r = AddTicks(t.GetTicks());
+       SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
 
        return r;
 }
@@ -292,13 +334,10 @@ DateTime::Add(const TimeSpan& t)
 result
 DateTime::AddDays(int days)
 {
-       TimeSpan ts(days * NUM_OF_SEC_IN_DAY * 1000LL);
+       long long ticks = static_cast< long long >(days) * NUM_OF_TICKS_IN_DAY;
 
-       result r = Add(ts);
-       if (IsFailed(r))
-       {
-               SysLogException(NID_BASE, r, "[%s] Propagating.", GetErrorMessage(r));
-       }
+       result r = AddTicks(ticks);
+       SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
 
        return r;
 }
@@ -306,13 +345,10 @@ DateTime::AddDays(int days)
 result
 DateTime::AddHours(int hours)
 {
-       TimeSpan ts(hours * NUM_OF_SEC_IN_HOUR * 1000LL);
+       long long ticks = static_cast< long long >(hours) * NUM_OF_TICKS_IN_HOUR;
 
-       result r = Add(ts);
-       if (IsFailed(r))
-       {
-               SysLogException(NID_BASE, r, "[%s] Propagating.", GetErrorMessage(r));
-       }
+       result r = AddTicks(ticks);
+       SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
 
        return r;
 }
@@ -320,13 +356,10 @@ DateTime::AddHours(int hours)
 result
 DateTime::AddMinutes(int minutes)
 {
-       TimeSpan ts(minutes * NUM_OF_SEC_IN_MINUTE * 1000LL);
+       long long ticks = static_cast< long long >(minutes) * NUM_OF_TICKS_IN_MINUTE;
 
-       result r = Add(ts);
-       if (IsFailed(r))
-       {
-               SysLogException(NID_BASE, r, "[%s] Propagating.", GetErrorMessage(r));
-       }
+       result r = AddTicks(ticks);
+       SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
 
        return r;
 }
@@ -337,56 +370,49 @@ DateTime::AddMonths(int months)
        DateTime tmp;
        tmp.SetValue(*this);
 
-       tmp.__dateTime.year += months / MAX_MONTH;
+       tmp.__dateTime.year += months / MAX_MONTH; // Get the year to add
 
        int tempMonth = tmp.__dateTime.month + months % MAX_MONTH;
-       if (tempMonth > MAX_MONTH)
+       if (tempMonth > MAX_MONTH)      // Month was added and moved to next year
        {
                tmp.__dateTime.year++;
                tmp.__dateTime.month = tempMonth - MAX_MONTH;
        }
-       else if (tempMonth < 0)
+       else if (tempMonth <= 0)        // Month was subtracted and moved to previous year
        {
                tmp.__dateTime.year--;
                tmp.__dateTime.month = MAX_MONTH + tempMonth;
        }
-       else if (tempMonth == 0)
-       {
-               tmp.__dateTime.year--;
-               tmp.__dateTime.month = MAX_MONTH;
-       }
-       else
+       else                                            // Keep current year
        {
                tmp.__dateTime.month += months % MAX_MONTH;
        }
 
        // Check the days in Month
-       if (tmp.__dateTime.month != 2)      // Not Feb
+       if (tmp.__dateTime.month != 2)      // Current month is not Feb
        {
-               if (tmp.__dateTime.day == 31)
+               if (tmp.__dateTime.day == 31)   // Previous day is 31th
                {
                        result r = SetValue(tmp.__dateTime.year, tmp.__dateTime.month, tmp.__dateTime.day);
-                       if (IsFailed(r))
+                       if (IsFailed(r))                        // Current month doesn't have 31th
                        {
-                               r = tmp.AddDays(-1);
-                               SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+                               tmp.__dateTime.day = 30;        // Set day to 30th
                        }
                }
        }
-       else                                // Feb
+       else                                                                    // Current month is Feb
        {
-               if (tmp.__dateTime.day > 28)
+               if (tmp.__dateTime.day > 28)            // Previous day is over 28th
                {
-                       if (tmp.IsLeapYear())
+                       if (tmp.IsLeapYear())                   // Leap year
                        {
-                               tmp.__dateTime.day = 29;
+                               tmp.__dateTime.day = 29;        // Set day to 29th
                        }
-                       else
+                       else                                                    // Not leap year
                        {
-                               tmp.__dateTime.day = 28;
+                               tmp.__dateTime.day = 28;        // Set day to 28th
                        }
                }
-
        }
 
        SysTryReturn(NID_BASE, (tmp <= DateTime::GetMaxValue()) && (tmp >= DateTime::GetMinValue()), E_OUT_OF_RANGE,
@@ -400,13 +426,33 @@ DateTime::AddMonths(int months)
 result
 DateTime::AddSeconds(int seconds)
 {
-       TimeSpan ts(seconds * 1000LL);
+       long long ticks = static_cast< long long >(seconds) * NUM_OF_TICKS_IN_SECOND;
 
-       result r = Add(ts);
-       if (IsFailed(r))
-       {
-               SysLogException(NID_BASE, r, "[%s] Propagating.", GetErrorMessage(r));
-       }
+       result r = AddTicks(ticks);
+       SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
+
+       return r;
+}
+
+result
+DateTime::AddMilliseconds(long long milliseconds)
+{
+       long long ticks = milliseconds * TICKS_PER_MILLISECOND;
+
+       result r = AddTicks(ticks);
+       SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
+
+       return r;
+}
+
+result
+DateTime::AddTicks(long long ticks)
+{
+       long long total = ConvertDateToTicks(&__dateTime);
+       total += ticks;
+
+       result r = ConvertTicksToDate(total, &__dateTime);
+       SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
 
        return r;
 }
@@ -457,10 +503,8 @@ DateTime::Compare(const DateTime& dt1, const DateTime& dt2)
        {
                return -1;
        }
-       else
-       {
-               return(0);
-       }
+
+       return 0;
 }
 
 int
@@ -478,7 +522,7 @@ DateTime::Equals(const Object& obj) const
                return false;
        }
 
-       return(*this == *pOther);
+       return (*this == *pOther);
 }
 
 int
@@ -486,7 +530,7 @@ DateTime::GetHashCode(void) const
 {
        TimeSpan t = GetTime();
        int hash = t.GetHashCode();
-       return(hash ^ (hash >> 16));
+       return (hash ^ (hash >> INT_HALF_BIT));
 }
 
 TimeSpan
@@ -506,7 +550,7 @@ DateTime::GetTimeOfDay(void) const
 
        long long span = total - since;
 
-       return(TimeSpan(span * 1000));
+       return (TimeSpan(span * NUM_OF_TICKS_IN_SECOND));
 }
 
 result
@@ -527,7 +571,7 @@ DateTime::GetDaysInMonth(int year, int month, int& days)
        }
        else
        {
-               days = (daysInMonth[month]);
+               days = daysInMonth[month];
        }
 
        return E_SUCCESS;
@@ -536,24 +580,18 @@ DateTime::GetDaysInMonth(int year, int month, int& days)
 result
 DateTime::Subtract(const TimeSpan& t)
 {
-       long long total = ConvertDateToSeconds(&__dateTime);
-       long long span = total - (t.GetTicks() / 1000);
+       DateTime dt1;
+       long long total = ConvertDateToTicks(&__dateTime);
+       long long span = total - t.GetTicks();
 
-       SysTryReturn(NID_BASE, span > 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] The arguments contain invalid values.",
+       SysTryReturn(NID_BASE, span >= MIN_TICK, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] The arguments contain invalid values.",
                GetErrorMessage(E_OUT_OF_RANGE));
 
-       SysTryReturn(NID_BASE, span >= NUM_OF_SEC_IN_DAY, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
-               "[%s] The arguments contain invalid values.", GetErrorMessage(E_OUT_OF_RANGE));
-
-       long long maxSeconds = ConvertDateToSeconds(&(DateTime::GetMaxValue().__dateTime));
-       SysTryReturn(NID_BASE, span <= maxSeconds, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
+       SysTryReturn(NID_BASE, span <= TOTAL_MAX_TICKS, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
                "[%s] The value of the argument is outside the valid range.", GetErrorMessage(E_OUT_OF_RANGE));
 
-       result r = ConvertSecondsToDate(span, &__dateTime);
-       if (r != E_SUCCESS)
-       {
-               SysLogException(NID_BASE, r, "[%s] Propagating.", GetErrorMessage(r));
-       }
+       result r = ConvertTicksToDate(span, &__dateTime);
+       SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
 
        return r;
 }
@@ -565,7 +603,7 @@ DateTime::ToString(void) const
 
        swprintf(date, (sizeof(date) / sizeof(wchar_t)), L"%2d/%2d/%4d %2d:%2d:%2d",
                         __dateTime.month, __dateTime.day, __dateTime.year, __dateTime.hour, __dateTime.minute,
-                        __dateTime.second);
+                        (__dateTime.second & LOW_16BIT));
 
        if (__dateTime.month < 10)
        {
@@ -603,14 +641,12 @@ DateTime::ToString(void) const
                date[14] = L'0';
        }
 
-       if (__dateTime.second < 10)
+       if ((__dateTime.second & LOW_16BIT) < 10)
        {
                date[17] = L'0';
        }
 
-       String string(date);
-
-       return string;
+       return String(date);
 }
 
 result
@@ -626,43 +662,43 @@ DateTime::Parse(const String& str, DateTime& dt)
        SysTryReturn(NID_BASE, pTmp != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
                GetErrorMessage(E_OUT_OF_MEMORY));
 
-       wchar_t* pMchar = (wchar_t*) str.GetPointer();
+       wchar_t* pMchar = const_cast< wchar_t* >(str.GetPointer());
 
        // month
        wcsncpy(pTmp.get(), pMchar, 2);
        pTmp[2] = L'\0';
 
-       int month = (int) wcstol(pTmp.get(), null, 10);
+       int month = static_cast< int >(wcstol(pTmp.get(), null, 10));
 
        // day
        wcsncpy(pTmp.get(), pMchar + 3, 2);
        pTmp[2] = L'\0';
 
-       int day = (int) wcstol(pTmp.get(), null, 10);
+       int day = static_cast< int >(wcstol(pTmp.get(), null, 10));
 
        // year
        wcsncpy(pTmp.get(), pMchar + 6, 4);
        pTmp[4] = L'\0';
 
-       int year = (int) wcstol(pTmp.get(), null, 10);
+       int year = static_cast< int >(wcstol(pTmp.get(), null, 10));
 
        // hour
        wcsncpy(pTmp.get(), pMchar + 11, 2);
        pTmp[2] = L'\0';
 
-       int hour = (int) wcstol(pTmp.get(), null, 10);
+       int hour = static_cast< int >(wcstol(pTmp.get(), null, 10));
 
        // minute
        wcsncpy(pTmp.get(), pMchar + 14, 2);
        pTmp[2] = L'\0';
 
-       int minute = (int) wcstol(pTmp.get(), null, 10);
+       int minute = static_cast< int >(wcstol(pTmp.get(), null, 10));
 
        // second
        wcsncpy(pTmp.get(), pMchar + 17, 2);
        pTmp[2] = L'\0';
 
-       int sec = (int) wcstol(pTmp.get(), null, 10);
+       int sec = static_cast< int >(wcstol(pTmp.get(), null, 10));
 
        // construct date time
        DateTime tmpDt;
@@ -707,7 +743,25 @@ DateTime::GetMinute(void) const
 int
 DateTime::GetSecond(void) const
 {
-       return __dateTime.second;
+       return (__dateTime.second & LOW_16BIT);
+}
+
+int
+DateTime::GetMillisecond(void) const
+{
+       return ((__dateTime.second >> INT_HALF_BIT) / TICKS_PER_MILLISECOND);
+}
+
+long long
+DateTime::GetTicks(void) const
+{
+       return ConvertDateToTicks(&__dateTime);
+}
+
+int
+DateTime::GetTicksPerSecond(void)
+{
+       return NUM_OF_TICKS_IN_SECOND;
 }
 
 TimeSpan
@@ -715,13 +769,13 @@ DateTime::GetTime(void) const
 {
        long long seconds = ConvertDateToSeconds(&__dateTime) - ConvertDateToSeconds(&(DateTime::GetMinValue().__dateTime));
 
-       return TimeSpan(seconds * 1000);
+       return TimeSpan(seconds * NUM_OF_TICKS_IN_SECOND);
 }
 
 const DateTime&
 DateTime::GetMaxValue(void)
 {
-       static DateTime maxValue(_MAX_YEAR, MAX_MONTH, MAX_DAY, MAX_HOUR, MAX_MINUTE, MAX_SECOND);
+       static DateTime maxValue(_MAX_YEAR, MAX_MONTH, MAX_DAY, MAX_HOUR, MAX_MINUTE, MAX_SECOND, MAX_TICK);
 
        return maxValue;
 }
@@ -729,7 +783,7 @@ DateTime::GetMaxValue(void)
 const DateTime&
 DateTime::GetMinValue(void)
 {
-       static DateTime minValue(MIN_YEAR, MIN_MONTH, MIN_DAY, MIN_HOUR, MIN_MINUTE, MIN_SECOND);
+       static DateTime minValue(MIN_YEAR, MIN_MONTH, MIN_DAY, MIN_HOUR, MIN_MINUTE, MIN_SECOND, MIN_TICK);
 
        return minValue;
 }
@@ -746,147 +800,106 @@ DateTime::IsLeapYear(int year)
        return year >= 0 && (!(year % 4) && ((year % 100) || !(year % 400)));
 }
 
-DateTime::DateTime(int year, int month, int day, int hour, int minute, int second)
+DateTime::DateTime(int year, int month, int day, int hour, int minute, int second, int tick)
        : __pDateTimeImpl(null)
 {
-       SetValue(year, month, day, hour, minute, second);
+       SetValue(year, month, day, hour, minute, (tick << INT_HALF_BIT) | second);
 }
 
 result
-DateTime::ConvertSecondsToDate(long long seconds, TmDateTime* pDateTime)
+DateTime::ConvertTicksToDate(long long ticks, TmDateTime* pDateTime)
 {
-       DateTime dt;
+       SysTryReturnResult(NID_BASE, (ticks >= MIN_TICK) && (ticks <= TOTAL_MAX_TICKS), E_OUT_OF_RANGE,
+               "[%s] The arguments (%lld) contain invalid values.", GetErrorMessage(E_OUT_OF_RANGE), ticks);
 
-       int year = 0;
        int month = 0;
        int day = 0;
        int hour = 0;
        int minute = 0;
        int second = 0;
-       int totalDays = 0;
-       int tempDays = 0;
-       int tempSeconds = 0;
-       bool leapYear = false;
+       int tempTicks = 0;
 
-       totalDays = (int) (seconds / SEC_IN_A_DAY);
+       int totalDays = static_cast< int >(ticks / NUM_OF_TICKS_IN_DAY) + 1;
 
        // Get a year and leapYear
-       year = CountYears(totalDays);
-       SysTryReturn(NID_BASE, year >= MIN_YEAR && year <= _MAX_YEAR, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
-               "[%s] The arguments (%d) contain invalid values.", GetErrorMessage(E_OUT_OF_RANGE), seconds);
+       int year = CountYears(totalDays);
 
        // Check a leapYear
-       leapYear = DateTime::IsLeapYear(year);
+       bool leapYear = DateTime::IsLeapYear(year);
 
        // Get days without year;
-       tempDays = totalDays - CountDays(year);
-
-       // Check the boundary of days
-       if (leapYear)
-       {
-               if (tempDays > 366)
-               {
-                       year += 1;
-                       tempDays -= 366;
-               }
-       }
-       else
-       {
-               if (tempDays > 365)
-               {
-                       year += 1;
-                       tempDays -= 365;
-               }
-       }
+       int tempDays = totalDays - CountDays(year);
 
        // Get month
        if (tempDays == 0)      //  month = 0 , day = 0
        {
-               dt.__dateTime.year = year;
-               dt.__dateTime.month = 0;
-               dt.__dateTime.day = 0;
-               dt.__dateTime.hour = 0;
-               dt.__dateTime.minute = 0;
-               dt.__dateTime.second = 0;
-
-               tempSeconds = (int) (seconds - ConvertDateToSeconds(&(dt.__dateTime)));
-
-               // Get Hour
-               hour = tempSeconds / NUM_OF_SEC_IN_HOUR;
-
-               tempSeconds -= hour * NUM_OF_SEC_IN_HOUR;
-
-               // Get Minute
-               minute = tempSeconds / NUM_OF_SEC_IN_MINUTE;
-
-               // Get Second
-               second = tempSeconds - minute * NUM_OF_SEC_IN_MINUTE;
-
-               SetValue(year, 1, 1, hour, minute, second);
-
-               return E_SUCCESS;
+               month = 0;
+               day = 0;
        }
        else if (leapYear)
        {
-               int leapYearDaysCount = 0;
+               int idx = 0;
 
-               for (; leapYearDaysCount < 12; leapYearDaysCount++)
+               for (; idx < 12; ++idx)
                {
-                       if ((DAYS_IN_LEAP_YEAR[leapYearDaysCount] < tempDays) && (tempDays <= DAYS_IN_LEAP_YEAR[leapYearDaysCount + 1]))
+                       if ((DAYS_IN_LEAP_YEAR[idx] < tempDays) && (tempDays <= DAYS_IN_LEAP_YEAR[idx + 1]))
                        {
-                               month = leapYearDaysCount + 1;
                                break;
                        }
                }
 
-               SysTryReturn(NID_BASE, leapYearDaysCount != 12, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
-                       "[%s] The arguments (%d) contain invalid values.", GetErrorMessage(E_OUT_OF_RANGE), seconds);
-
-               day = tempDays - DAYS_IN_LEAP_YEAR[month - 1];
+               month = idx + 1;
+               day = tempDays - DAYS_IN_LEAP_YEAR[idx];
        }
        else
        {
-               int daysCount = 0;
+               int idx = 0;
 
-               for (; daysCount < 12; daysCount++)
+               for (; idx < 12; ++idx)
                {
-                       if ((DAYS[daysCount] < tempDays) && (tempDays <= DAYS[daysCount + 1]))
+                       if ((DAYS[idx] < tempDays) && (tempDays <= DAYS[idx + 1]))
                        {
-                               month = daysCount + 1;
                                break;
                        }
                }
 
-               SysTryReturn(NID_BASE, daysCount != 12, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
-                       "[%s] The arguments (%d) contain invalid values.", GetErrorMessage(E_OUT_OF_RANGE), seconds);
-
-               // Get day
-               day = tempDays - DAYS[month - 1];
+               month = idx + 1;
+               day = tempDays - DAYS[idx];
        }
 
-       dt.SetValue(year, month, day, 0, 0, 0);
+       DateTime dt(year, month, day, 0, 0, 0, 0);
 
        // Get Hour
-       tempSeconds = (int) (seconds - ConvertDateToSeconds(&(dt.__dateTime)));
-       hour = tempSeconds / NUM_OF_SEC_IN_HOUR;
+       tempTicks = static_cast< int >(ticks - ConvertDateToTicks(&(dt.__dateTime)));
+       hour = tempTicks / NUM_OF_TICKS_IN_HOUR;
 
        // Get Minute
-       tempSeconds -= hour * NUM_OF_SEC_IN_HOUR;
-       minute = tempSeconds / NUM_OF_SEC_IN_MINUTE;
+       tempTicks -= hour * NUM_OF_TICKS_IN_HOUR;
+       minute = tempTicks / NUM_OF_TICKS_IN_MINUTE;
 
        // Get Second
-       second = tempSeconds - minute * NUM_OF_SEC_IN_MINUTE;
+       tempTicks -= minute * NUM_OF_TICKS_IN_MINUTE;
+       second = tempTicks / NUM_OF_TICKS_IN_SECOND;
+
+       // Get Tick
+       tempTicks -= second * NUM_OF_TICKS_IN_SECOND;
+       second = ((tempTicks << INT_HALF_BIT) | second);
 
-       SetValue(year, month, day, hour, minute, second);
+       pDateTime->year = year;
+       pDateTime->month = month;
+       pDateTime->day = day;
+       pDateTime->hour = hour;
+       pDateTime->minute = minute;
+       pDateTime->second = second;
 
        return E_SUCCESS;
 }
 
 long long
-DateTime::ConvertDateToSeconds(const TmDateTime* pDateTime) const
+DateTime::ConvertDateToTicks(const TmDateTime* pDateTime) const
 {
        long long days = 0;
-       long long seconds = 0;
+       long long ticks = 0;
 
        // Add year
        days = CountDays(pDateTime->year);
@@ -905,21 +918,42 @@ DateTime::ConvertDateToSeconds(const TmDateTime* pDateTime) const
        }
 
        // Add days
-       days += pDateTime->day;
+       days += pDateTime->day - 1;
 
-       // Convert to Seconds
-       seconds = days * NUM_OF_SEC_IN_DAY;
+       // Convert to ticks
+       ticks = days * NUM_OF_TICKS_IN_DAY;
 
        // Add hours
-       seconds += pDateTime->hour * NUM_OF_SEC_IN_HOUR;
+       ticks += pDateTime->hour * NUM_OF_TICKS_IN_HOUR;
 
        // Add minutes
-       seconds += pDateTime->minute * NUM_OF_SEC_IN_MINUTE;
+       ticks += pDateTime->minute * NUM_OF_TICKS_IN_MINUTE;
 
-       // Add minutes
-       seconds += pDateTime->second;
+       // Add seconds
+       ticks += ((pDateTime->second) & LOW_16BIT) * NUM_OF_TICKS_IN_SECOND;
+
+       // Add ticks
+       ticks += ((pDateTime->second) >> INT_HALF_BIT);
+
+       return ticks;
+}
+
+result
+DateTime::ConvertSecondsToDate(long long seconds, TmDateTime* pDateTime)
+{
+       long long ticks = seconds * NUM_OF_TICKS_IN_SECOND;
 
-       return seconds;
+       return ConvertTicksToDate(ticks, pDateTime);
+}
+
+long long
+DateTime::ConvertDateToSeconds(const TmDateTime* pDateTime) const
+{
+       long long ticks = ConvertDateToTicks(pDateTime);
+
+       return ticks / NUM_OF_TICKS_IN_SECOND;
 }
 
 }} // Tizen::Base
+
+