#include "stdafx.h"

#include <time.h>

 

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

namespace cx

{

 

 

 

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

typedef void* pointer_t; // 4 bytes

 

//===========================================================================

typedef signed   int int_t ;

typedef unsigned int uint_t;

 

typedef signed             char int8_t  ; //                       -127 ~ +128

typedef signed   short     int  int16_t ; //                    -32,768 ~ +32,767

typedef signed   long      int  int32_t ; //             -2,147,483,648 ~ +2,147,483,647

typedef signed   long long int  int64_t ; // -9,223,372,036,854,775,808 ~ +9,223,372,036,854,775,807

typedef unsigned           char uint8_t ; // 0 ~ 255

typedef unsigned short     int  uint16_t; // 0 ~ 65,535

typedef unsigned long      int  uint32_t; // 0 ~ 4,294,967,295

typedef unsigned long long int  uint64_t; // 0 ~ 18,446,744,073,709,551,615

 

 

typedef bool bool_t;

 

typedef ::time_t time_t;

 

typedef unsigned char byte_t;

 

}

 

 

 

 

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

/*

 

# Julian Period

15 (indiction cycle) × 19 (Metonic cycle) × 28 (Solar cycle) = 7980 years

첫 주기 : 4713 BC ~ AD 3268

 

# Julian Day

Julian Period의 일

천문학에서 사용

Julian Calendar 와는 다른 별도의 개념

 

# Julian Day Number(JDN)

정수 형태의 Julian

-4712/01/01 = 0(BC 4713)

-4712/01/02 = 1

-4712/01/03 = 2

JDN은 날짜 의미로만 사용

 

# Julian Date(JD)

실수 형태의 Julian

-4712/01/01 12:00:00 = 0.0

-4712/01/02 00:00:00 = 0.5

-4712/01/02 12:00:00 = 1.0

-4712/01/03 00:00:00 = 1.5

-4712/01/03 12:00:00 = 2.0

JD는 날짜+시간 의미 사용

 

# Julian Calendar

1582 10 04일 목요일 까지 사용

 

# Gregorian Calendar

1582 10 15일 금요일 부터 사용

 

# Calendar Date

1582 10 05 ~ 1582 10 14일은 존재 안함

 

# Unix Epoch Time

1970 01 01

윤초 없음

 

# 윤초

GMT 12 31 23:59:60 - 한국시간 01 01일 오전 08 59 60

GMT 06 30 23:59:60 - 한국시간 07 01일 오전 08 59 60

필요시 삽입됨

 

# Epoch Time

윤초 표시하지 않음

 

# 참고 URL

https://en.wikipedia.org/wiki/Julian_day

 

http://aa.usno.navy.mil/data/docs/JulianDate.php

https://www.tondering.dk/claus/cal/gregorian.php#country

http://jidolstar.tistory.com/482

 

*/

 

 

 

 

 

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

void jdn_to_modified_jdn        (cx::int_t jdn, cx::int_t& modified_jdn);

void jdn_to_truncated_jd        (cx::int_t jdn, cx::int_t& truncated_jd);

void jdn_to_lilian_date         (cx::int_t jdn, cx::int_t& lilian_date);

void jdn_to_unix_time_day_count (cx::int_t jdn, cx::int_t& unix_time_day_count);

 

//===========================================================================

void gregorian_calendar_date_to_jdn (cx::int_t year, cx::int_t month, cx::int_t day, cx::int_t& jdn);

void julian_calendar_date_to_jdn    (cx::int_t year, cx::int_t month, cx::int_t day, cx::int_t& jdn);

void jdn_to_gregorian_calendar_date (cx::int_t jdn, cx::int_t& year, cx::int_t& month, cx::int_t& day);

void jdn_to_julian_calendar_date    (cx::int_t jdn, cx::int_t& year, cx::int_t& month, cx::int_t& day);

 

//===========================================================================

cx::int_t  calendar_date_from_1582_10_05_to_1582_10_14 (cx::int_t year, cx::int_t month, cx::int_t day);

 

cx::bool_t calendar_date_to_jdn (cx::int_t year, cx::int_t month, cx::int_t day, cx::int_t& jdn);

void       jdn_to_calendar_date (cx::int_t jdn, cx::int_t& year, cx::int_t& month, cx::int_t& day);

 

//===========================================================================

cx::int_t us_get_day_of_week  (cx::int_t jdn);

cx::int_t iso_get_day_of_week (cx::int_t jdn);

 

//===========================================================================

cx::bool_t gregorian_calendar_is_leap_year          (cx::int_t year);

cx::bool_t gregorian_calendar_get_last_day_of_month (cx::int_t year, cx::int_t month, cx::int_t& day);

cx::bool_t gregorian_calendar_is_valid_date         (cx::int_t year, cx::int_t month, cx::int_t day);

 

//===========================================================================

void epoch_to_jdn         (cx::int_t base_epoch_jdn, cx::int_t day_number, cx::int_t& jdn);

void epoch_to_date        (cx::int_t base_epoch_jdn, cx::int_t day_number, cx::int_t& year, cx::int_t& month, cx::int_t& day);

void epoch_to_day_of_week (cx::int_t base_epoch_jdn, cx::int_t day_number, cx::int_t& day_of_week);

void epoch_to_time        (                          cx::int_t daysecond_number, cx::int_t& hour, cx::int_t& minute, cx::int_t& second);

 

//===========================================================================

cx::int_t epoch_second_get_day_number       (cx::time_t epoch_second);

cx::int_t epoch_second_get_daysecond_number (cx::time_t epoch_second);

 

void epoch_second_to_jdn         (cx::int_t base_epoch_jdn, cx::time_t epoch_second, cx::int_t& jdn);

void epoch_second_to_datetime    (cx::int_t base_epoch_jdn, cx::time_t epoch_second, cx::int_t& year, cx::int_t& month, cx::int_t& day, cx::int_t& day_of_week, cx::int_t& hour, cx::int_t& minute, cx::int_t& second);

void epoch_second_to_date        (cx::int_t base_epoch_jdn, cx::time_t epoch_second, cx::int_t& year, cx::int_t& month, cx::int_t& day);

void epoch_second_to_day_of_week (cx::int_t base_epoch_jdn, cx::time_t epoch_second, cx::int_t& day_of_week);

void epoch_second_to_time        (                          cx::time_t epoch_second, cx::int_t& hour, cx::int_t& minute, cx::int_t& second);

void datetime_to_epoch_second    (cx::int_t year, cx::int_t month, cx::int_t day, cx::int_t hour, cx::int_t minute, cx::int_t second, cx::int_t base_epoch_jdn, cx::time_t& epoch_second);

 

//===========================================================================

cx::int_t unix_time_get_base_epoch_jdn (void);

 

void unix_time_to_jdn         (cx::time_t t, cx::int_t& jdn);

void unix_time_to_datetime    (cx::time_t t, cx::int_t& year, cx::int_t& month, cx::int_t& day, cx::int_t& day_of_week, cx::int_t& hour, cx::int_t& minute, cx::int_t& second);

void unix_time_to_date        (cx::time_t t, cx::int_t& year, cx::int_t& month, cx::int_t& day);

void unix_time_to_day_of_week (cx::time_t t, cx::int_t& day_of_week);

void unix_time_to_time        (cx::time_t t, cx::int_t& hour, cx::int_t& minute, cx::int_t& second);

void datetime_to_unix_time    (cx::int_t year, cx::int_t month, cx::int_t day, cx::int_t hour, cx::int_t minute, cx::int_t second, cx::time_t& t);

 

 

 

 

 

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

void jdn_to_modified_jdn (cx::int_t jdn, cx::int_t& modified_jdn)

{

         /*

         # Modified JD

        

         Epoch:       0h Nov 17, 1858

         Calculation: JD 2400000.5

         */

 

         modified_jdn = jdn-2400001;

}

 

void jdn_to_truncated_jd (cx::int_t jdn, cx::int_t& truncated_jd)

{

         /*

         # Truncated JD

        

         Introduced by NASA in 1979

        

         Epoch:       0h May 24, 1968

         Calculation: floor (JD 2440000.5)

 

         cf:

         ceil () 소수점 이하 올림

         floor() 소수점 이하 버림

         round() 소수점 반올림

         */

 

         truncated_jd = jdn-2440001;

}

 

void jdn_to_lilian_date (cx::int_t jdn, cx::int_t& lilian_date)

{

         /*

         # Lilian date

 

         Count of days of the Gregorian calendar

        

         Epoch:       Oct 15, 1582

         Calculation: floor (JD 2299159.5)

         */

 

         lilian_date = jdn-2299160;

}

 

void jdn_to_unix_time_day_count (cx::int_t jdn, cx::int_t& unix_time_day_count)

{

         /*

         # Unix Time

 

         Count of seconds

 

         Epoch:       0h Jan 1, 1970

         Calculation: (JD 2440587.5) × 86400

         */

 

         unix_time_day_count = jdn-2440588;

}

 

 

 

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

// Converting Gregorian calendar date to Julian Day Number(noon)

void gregorian_calendar_date_to_jdn (cx::int_t year, cx::int_t month, cx::int_t day, cx::int_t& jdn)

{

         // The algorithm is valid for all (possibly proleptic) Gregorian calendar dates after November 23, -4713.

 

         cx::int_t J;

         cx::int_t Y;

         cx::int_t M;

         cx::int_t D;

 

 

         Y = year;

         M = month;

         D = day;

 

        

         cx::int_t M1;

         cx::int_t Y1;

 

 

         M1 = (M - 14) / 12;

         Y1 = Y + 4800;

         J  = 1461 * (Y1 + M1) / 4 + 367 * (M - 2 - 12 * M1) / 12 - (3 * ((Y1 + M1 +100) / 100)) / 4 + D - 32075;

 

 

         jdn = J;

}

 

// Converting Julian calendar date to Julian Day Number(noon)

void julian_calendar_date_to_jdn (cx::int_t year, cx::int_t month, cx::int_t day, cx::int_t& jdn)

{

         // The algorithm is valid for all (possibly proleptic) Julian calendar years >= -4712, that is, for all JDN >= 0.

 

         cx::int_t J;

         cx::int_t Y;

         cx::int_t M;

         cx::int_t D;

 

 

         Y = year;

         M = month;

         D = day;

 

         J = 367 * Y - (7 * (Y + 5001 + (M - 9) / 7)) / 4 + (275 * M) / 9 + D + 1729777;

 

 

         jdn = J;

}

 

#if 0

void jdn_to_gregorian_calendar_date (cx::int_t jdn, cx::int_t& year, cx::int_t& month, cx::int_t& day)

{

         cx::int_t J;

         cx::int_t Y;

         cx::int_t M;

         cx::int_t D;

 

        

         J = jdn;

 

 

         cx::int_t p;

         cx::int_t q;

         cx::int_t r;

         cx::int_t s;

         cx::int_t t;

         cx::int_t u;

         cx::int_t v;

 

 

         p = J + 68569;

         q = 4*p/146097;

         r = p - (146097*q + 3)/4;

         s = 4000*(r+1)/1461001;

         t = r - 1461*s/4 + 31;

         u = 80*t/2447;

         v = u/11;

 

         Y = 100*(q-49)+s+v;

         M = u + 2 - 12*v;

         D = t - 2447*u/80;

 

         year  = Y;

         month = M;

         day   = D;

}

#endif

 

void jdn_to_gregorian_calendar_date (cx::int_t jdn, cx::int_t& year, cx::int_t& month, cx::int_t& day)

{

         const cx::int_t y = 4716;

         const cx::int_t j = 1401;

         const cx::int_t m = 2;

         const cx::int_t n = 12;

         const cx::int_t r = 4;

         const cx::int_t p = 1461;

         const cx::int_t v = 3;

         const cx::int_t u = 5;

         const cx::int_t s = 153;

         const cx::int_t w = 2;

         const cx::int_t B = 274277;

         const cx::int_t C = -38;

 

         cx::int_t J;

         cx::int_t Y;

         cx::int_t M;

         cx::int_t D;

 

 

         J = jdn;

 

 

         cx::int_t f;

         cx::int_t e;

         cx::int_t g;

         cx::int_t h;

 

         f = J + j + (((4 * J + B) / 146097) * 3) / 4 + C;

         e = r * f + v;

         g = (e % p) / r;

         h = u * g + w;

        

         D = (h % s) / u  + 1;

         M = (h / s + m) % n + 1;

         Y = (e / p) - y + (n + m - M) / n;

 

 

         year  = Y;

         month = M;

         day   = D;

}

 

void jdn_to_julian_calendar_date (cx::int_t jdn, cx::int_t& year, cx::int_t& month, cx::int_t& day)

{

         const cx::int_t y = 4716;

         const cx::int_t j = 1401;

         const cx::int_t m = 2;

         const cx::int_t n = 12;

         const cx::int_t r = 4;

         const cx::int_t p = 1461;

         const cx::int_t v = 3;

         const cx::int_t u = 5;

         const cx::int_t s = 153;

         const cx::int_t w = 2;

         const cx::int_t B = 274277;

         const cx::int_t C = -38;

 

 

         cx::int_t J;

         cx::int_t Y;

         cx::int_t M;

         cx::int_t D;

 

 

         J = jdn;

 

 

         cx::int_t f;

         cx::int_t e;

         cx::int_t g;

         cx::int_t h;

 

         f = J + j;

         e = r * f + v;

         g = (e % p) / r;

         h = u * g + w;

        

         D = (h % s) / u  + 1;

         M = (h / s + m) % n + 1;

         Y = (e / p) - y + (n + m - M) / n;

 

 

         year  = Y;

         month = M;

         day   = D;

}

 

 

 

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

cx::int_t calendar_date_from_1582_10_05_to_1582_10_14 (cx::int_t year, cx::int_t month, cx::int_t day)

{

         cx::int_t from_1582_10_05_to_1582_10_14;

 

 

 

         from_1582_10_05_to_1582_10_14 = 0;

         if     (year< 1582)

         {

                  from_1582_10_05_to_1582_10_14 = -1;

         }

         else if (year==1582)

         {

                  if      (month <  10)

                  {

                           from_1582_10_05_to_1582_10_14 = -1;

                  }

                  else if (month == 10)

                  {

                           if      (day <= 4)

                           {

                                   from_1582_10_05_to_1582_10_14 = -1;

                           }

                           else if (day >= 15)

                           {

                                   from_1582_10_05_to_1582_10_14 = 1;

                           }

                  }

                  else if (month >  10)

                  {

                           from_1582_10_05_to_1582_10_14 = 1;

                  }

         }

         else if (year> 1582)

         {

                  from_1582_10_05_to_1582_10_14 = 1;

         }

 

 

         return from_1582_10_05_to_1582_10_14;

}

 

cx::bool_t calendar_date_to_jdn (cx::int_t year, cx::int_t month, cx::int_t day, cx::int_t& jdn)

{

         cx::int_t from_1582_10_05_to_1582_10_14;

 

 

 

         from_1582_10_05_to_1582_10_14 = calendar_date_from_1582_10_05_to_1582_10_14(year,month,day);

 

         if      (0> from_1582_10_05_to_1582_10_14)

         {

                  julian_calendar_date_to_jdn    (year,month,day, jdn);

         }

         else if (0==from_1582_10_05_to_1582_10_14)

         {

                  return false;

         }

         else if (0< from_1582_10_05_to_1582_10_14)

         {

                  gregorian_calendar_date_to_jdn (year,month,day, jdn);

         }

 

         return true;

}

 

void jdn_to_calendar_date (cx::int_t jdn, cx::int_t& year, cx::int_t& month, cx::int_t& day)

{

         // jdn: 2299160 = 1582-10-04 Julian Calendar Date

         // jdn: 2299161 = 1582-10-15 Gregorian Calendar Date

 

         if (jdn<=2299160)

         {

                  jdn_to_julian_calendar_date    (jdn, year,month,day);

         }

         else

         {

                  jdn_to_gregorian_calendar_date (jdn, year,month,day);

         }

}

 

 

 

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

cx::int_t us_get_day_of_week (cx::int_t jdn)

{

         /*

         W1 = mod(J + 1, 7)

 

         0=Sun

         1=Mon

         2=Tue

         3=Wed

         4=Thu

         5=Fri

         6=Sat

         */

 

         return (jdn + 1) % 7;

}

 

cx::int_t iso_get_day_of_week (cx::int_t jdn)

{

         /*

         W0 = mod (J, 7) + 1

 

         1=Mon

         2=Tue

         3=Wed

         4=Thu

         5=Fri

         6=Sat

         7=Sun

         */

 

         return (jdn % 7) + 1;

}

 

 

 

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

cx::bool_t gregorian_calendar_is_leap_year (cx::int_t year)

{

         if ( (0==(year%400))

              ||

              (0!=(year%100) && 0==(year%4)) )

         {

                  return true;

         }

 

         return false;

}

 

cx::bool_t gregorian_calendar_get_last_day_of_month (cx::int_t year, cx::int_t month, cx::int_t& day)

{

         cx::int_t month_days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

 

 

         if (gregorian_calendar_is_leap_year(year))

         {

                  month_days[1] = 29;

         }

 

        

         if ( !( 0<month && month<13) )

         {

                  return false;

         }

 

 

         day = month_days[month-1];

 

         return true;

}

 

cx::bool_t gregorian_calendar_is_valid_date (cx::int_t year, cx::int_t month, cx::int_t day)

{

         cx::int_t last_day_of_month;

 

 

 

         if (false==gregorian_calendar_get_last_day_of_month(year, month, last_day_of_month))

         {

                  return false;

         }

 

         if ( day < 0 || day > last_day_of_month )

         {

                  return false;

         }

 

         return true;

}

 

 

 

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

void epoch_to_jdn (cx::int_t base_epoch_jdn, cx::int_t day_number, cx::int_t& jdn)

{       

         jdn = base_epoch_jdn + day_number;

}

 

void epoch_to_date (cx::int_t base_epoch_jdn, cx::int_t day_number, cx::int_t& year, cx::int_t& month, cx::int_t& day)

{

         cx::int_t jdn;

 

        

         epoch_to_jdn(base_epoch_jdn, day_number, jdn);

        

 

         jdn_to_gregorian_calendar_date(jdn, year, month, day);

}

 

void epoch_to_day_of_week (cx::int_t base_epoch_jdn, cx::int_t day_number, cx::int_t& day_of_week)

{

         cx::int_t jdn;

 

        

         epoch_to_jdn(base_epoch_jdn, day_number, jdn);

        

 

         day_of_week = us_get_day_of_week(jdn);

}

 

void epoch_to_time (cx::int_t daysecond_number, cx::int_t& hour, cx::int_t& minute, cx::int_t& second)

{

         hour   =  daysecond_number / 3600;

         minute = (daysecond_number % 3600) / 60;

         second = (daysecond_number % 3600) % 60;

}

 

 

 

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

cx::int_t epoch_second_get_day_number (cx::time_t epoch_second)

{

         const cx::int_t second_per_day = 86400; // 24*60*60;

        

         cx::int_t day_number;

 

 

         day_number = static_cast<cx::int_t>(epoch_second / second_per_day);

 

 

         return day_number;

}

 

cx::int_t epoch_second_get_daysecond_number (cx::time_t epoch_second)

{

         const cx::int_t second_per_day = 86400; // 24*60*60;

        

         cx::int_t daysecond_number;

 

 

         daysecond_number = static_cast<cx::int_t>(epoch_second % second_per_day);

 

 

         return daysecond_number;

}

 

//===========================================================================

void epoch_second_to_jdn (cx::int_t base_epoch_jdn, cx::time_t epoch_second, cx::int_t& jdn)

{

         cx::int_t day_number;

 

        

         day_number = epoch_second_get_day_number(epoch_second);

        

 

         epoch_to_jdn(base_epoch_jdn, day_number, jdn);

}

 

void epoch_second_to_datetime (cx::int_t base_epoch_jdn, cx::time_t epoch_second, cx::int_t& year, cx::int_t& month, cx::int_t& day, cx::int_t& day_of_week, cx::int_t& hour, cx::int_t& minute, cx::int_t& second)

{

         cx::int_t day_number;

         cx::int_t daysecond_number;

        

         cx::int_t jdn;

 

 

         day_number       = epoch_second_get_day_number      (epoch_second);

         daysecond_number = epoch_second_get_daysecond_number(epoch_second);

        

 

         epoch_to_jdn(base_epoch_jdn, day_number, jdn);

 

        

         jdn_to_gregorian_calendar_date(jdn, year, month, day);

         day_of_week = us_get_day_of_week(jdn);

 

        

         epoch_to_time(daysecond_number, hour, minute, second);

}

 

void epoch_second_to_date (cx::int_t base_epoch_jdn, cx::time_t epoch_second, cx::int_t& year, cx::int_t& month, cx::int_t& day)

{

         cx::int_t day_number;

        

         cx::int_t jdn;

 

 

         day_number = epoch_second_get_day_number(epoch_second);

        

 

         epoch_to_jdn(base_epoch_jdn, day_number, jdn);

 

        

         jdn_to_gregorian_calendar_date(jdn, year, month, day);

}

 

void epoch_second_to_day_of_week (cx::int_t base_epoch_jdn, cx::time_t epoch_second, cx::int_t& day_of_week)

{

         cx::int_t day_number;

        

         cx::int_t jdn;

 

 

         day_number = epoch_second_get_day_number(epoch_second);

 

        

         epoch_to_jdn(base_epoch_jdn, day_number, jdn);

 

        

         day_of_week = us_get_day_of_week(jdn);

}

 

void epoch_second_to_time (cx::time_t epoch_second, cx::int_t& hour, cx::int_t& minute, cx::int_t& second)

{

         cx::int_t daysecond_number;

 

 

         daysecond_number = epoch_second_get_daysecond_number(epoch_second);

        

 

         epoch_to_time(daysecond_number, hour, minute, second);

}

 

void datetime_to_epoch_second (cx::int_t year, cx::int_t month, cx::int_t day, cx::int_t hour, cx::int_t minute, cx::int_t second, cx::int_t base_epoch_jdn, cx::time_t& epoch_second)

{

         const cx::int_t second_per_day = 86400; // 24*60*60;

 

         cx::int_t jdn;

 

         cx::time_t day_number;

         cx::time_t daysecond_number;

 

 

         gregorian_calendar_date_to_jdn (year, month, day, jdn);

 

        

         day_number       = jdn - base_epoch_jdn;

         daysecond_number = hour*3600 + minute*60 + second;

        

 

         epoch_second = day_number*second_per_day + daysecond_number;

}

 

 

 

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

cx::int_t unix_time_get_base_epoch_jdn (void)

{

         const cx::int_t base_jdn = 2440588; // 1970 01 01 12:00:00

 

 

         return base_jdn;

}

 

void unix_time_to_jdn (cx::time_t t, cx::int_t& jdn)

{

         epoch_second_to_jdn (unix_time_get_base_epoch_jdn(), t, jdn);

}

 

void unix_time_to_datetime (cx::time_t t, cx::int_t& year, cx::int_t& month, cx::int_t& day, cx::int_t& day_of_week, cx::int_t& hour, cx::int_t& minute, cx::int_t& second)

{

         epoch_second_to_datetime (unix_time_get_base_epoch_jdn(), t, year, month, day, day_of_week, hour, minute, second);

}

 

void unix_time_to_date (cx::time_t t, cx::int_t& year, cx::int_t& month, cx::int_t& day)

{

         epoch_second_to_date (unix_time_get_base_epoch_jdn(), t, year, month, day);

}

 

void unix_time_to_day_of_week (cx::time_t t, cx::int_t& day_of_week)

{

         epoch_second_to_day_of_week (unix_time_get_base_epoch_jdn(), t, day_of_week);

}

 

void unix_time_to_time (cx::time_t t, cx::int_t& hour, cx::int_t& minute, cx::int_t& second)

{

         epoch_second_to_time (t, hour, minute, second);

}

 

void datetime_to_unix_time (cx::int_t year, cx::int_t month, cx::int_t day, cx::int_t hour, cx::int_t minute, cx::int_t second, cx::time_t& t)

{

         datetime_to_epoch_second (year, month, day, hour, minute, second, unix_time_get_base_epoch_jdn(), t);

}


 

 

/////////////////////////////////////////////////////////////////////////////

//===========================================================================

void show (

         cx::int_t jdn        ,

         cx::int_t year       ,

         cx::int_t month      ,

         cx::int_t day        ,

         cx::int_t day_of_week,

         cx::int_t hour       ,

         cx::int_t minute     ,

         cx::int_t second

         )

{

         cx::int_t modified_jdn;

         cx::int_t truncated_jd;

         cx::int_t lilian_date;

         cx::int_t unix_time_day_count;

 

 

         jdn_to_modified_jdn       (jdn, modified_jdn       );

         jdn_to_truncated_jd       (jdn, truncated_jd       );

         jdn_to_lilian_date        (jdn, lilian_date        );

         jdn_to_unix_time_day_count(jdn, unix_time_day_count);

 

         printf ("%+05d-%02d-%02d(%d) %02d:%02d:%02d = %8d / MJDN=%8d, TJD=%8d, LILIAN=%8d, UNIX=%8d \r\n",

                  year,month,day, day_of_week,

                  hour, minute, second,

                  jdn,

                  modified_jdn       ,

                  truncated_jd       ,

                  lilian_date        ,

                  unix_time_day_count

                  );

}

 

int _tmain(int argc, _TCHAR* argv[])

{

         cx::int_t jdn         = 0;

         cx::int_t year        = 0;

         cx::int_t month       = 0;

         cx::int_t day         = 0;

         cx::int_t day_of_week = 0;

         cx::int_t hour        = 0;

         cx::int_t minute      = 0;

         cx::int_t second      = 0;

 

         cx::int_t day_count = 0;

 

         cx::time_t t;

 

 

         //-----------------------------------------------------------------------

         // 12h Jan 1, 4713 BC

         jdn = 0;

         jdn_to_calendar_date(jdn, year,month,day); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

 

         year  =-4712;

         month =1;

         day   =1;

         calendar_date_to_jdn(year,month,day, jdn); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         printf ("\r\n");

 

 

 

         //-----------------------------------------------------------------------

         // BC 1

         year  =0;

         month =1;

         day   =1;

         calendar_date_to_jdn(year,month,day, jdn); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         jdn_to_calendar_date(jdn, year,month,day); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         printf ("\r\n");

 

 

 

         //-----------------------------------------------------------------------

         // 1 AD

         year  =1;

         month =1;

         day   =1;

         calendar_date_to_jdn(year,month,day, jdn); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         jdn_to_calendar_date(jdn, year,month,day); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         printf ("\r\n");

 

 

         //-----------------------------------------------------------------------

         // AD 2

         year  =2;

         month =1;

         day   =1;

         calendar_date_to_jdn(year,month,day, jdn); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         jdn_to_calendar_date(jdn, year,month,day); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         printf ("\r\n");

 

 

         //-----------------------------------------------------------------------

         jdn = 2299161-2;

         jdn_to_calendar_date(jdn, year,month,day); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

 

         jdn = 2299161-1;

         jdn_to_calendar_date(jdn, year,month,day); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

 

         jdn = 2299161;

         jdn_to_calendar_date(jdn, year,month,day); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

 

         jdn = 2299161+1;

         jdn_to_calendar_date(jdn, year,month,day); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

 

 

         year  =1582;

         month =10;

         day   = 3;

         calendar_date_to_jdn(year,month,day, jdn); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

 

         year  =1582;

         month =10;

         day   = 4;

         calendar_date_to_jdn(year,month,day, jdn); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

 

         // Lilian date : Oct 15, 1582 = floor (JD 2299159.5) ; Count of days of the Gregorian calendar

         year  =1582;

         month =10;

         day   =15;

         calendar_date_to_jdn(year,month,day, jdn); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

 

         year  =1582;

         month =10;

         day   =16;

         calendar_date_to_jdn(year,month,day, jdn); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         printf ("\r\n");

 

 

         //-----------------------------------------------------------------------

         // Reduced JD (JD - 2400000)

         year  =1858;

         month =11;

         day   =16;

         calendar_date_to_jdn(year,month,day, jdn); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         jdn_to_calendar_date(jdn, year,month,day); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         printf ("\r\n");

 

 

         //-----------------------------------------------------------------------

         // Modified JD: 0h Nov 17, 1858 = (JD 2400000.5)

         year  =1858;

         month =11;

         day   =17;

         calendar_date_to_jdn(year,month,day, jdn); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         jdn_to_calendar_date(jdn, year,month,day); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         printf ("\r\n");

 

 

         //-----------------------------------------------------------------------

         // Truncated JD: 0h May 24, 1968 = floor (JD 2440000.5) ; Introduced by NASA in 1979

         year  =1968;

         month =5;

         day   =24;

         calendar_date_to_jdn(year,month,day, jdn); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         jdn_to_calendar_date(jdn, year,month,day); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         printf ("\r\n");

 

 

         //-----------------------------------------------------------------------

         // Unix Time: 0h Jan 1, 1970 = (JD 2440587.5) × 86400

         year  =1970;

         month =1;

         day   =1;

         calendar_date_to_jdn(year,month,day, jdn); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         jdn_to_calendar_date(jdn, year,month,day); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         printf ("\r\n");

 

 

         //-----------------------------------------------------------------------

         year  =2000;

         month =1;

         day   =1;

         calendar_date_to_jdn(year,month,day, jdn); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         jdn_to_calendar_date(jdn, year,month,day); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         printf ("\r\n");

 

 

 

         //-----------------------------------------------------------------------

         year  =2018;

         month =10;

         day   =9;

         calendar_date_to_jdn(year,month,day, jdn); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         jdn_to_calendar_date(jdn, year,month,day); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         printf ("\r\n");

 

 

         //-----------------------------------------------------------------------

         t = time(NULL) + 9*60*60;

 

         day_count = static_cast<cx::int_t>(t / 86400);

         printf ("time() / 86400 = %d \r\n", day_count);

 

         unix_time_to_jdn     (t, jdn);

         unix_time_to_datetime(t, year,month,day, day_of_week, hour, minute, second);

 

         show (jdn, year, month, day, day_of_week, hour, minute, second);

 

         calendar_date_to_jdn(year,month,day, jdn); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

         jdn_to_calendar_date(jdn, year,month,day); day_of_week = us_get_day_of_week(jdn); show(jdn, year, month, day, day_of_week, hour, minute, second);

 

         printf ("\r\n");

 

         return 0;

}

 

/*

-4712-01-01(1) 00:00:00 =        0 / MJDN=-2400001, TJD=-2440001, LILIAN=-2299160, UNIX=-2440588

-4712-01-01(1) 00:00:00 =        0 / MJDN=-2400001, TJD=-2440001, LILIAN=-2299160, UNIX=-2440588

 

+0000-01-01(4) 00:00:00 =  1721058 / MJDN= -678943, TJD= -718943, LILIAN= -578102, UNIX= -719530

+0000-01-01(4) 00:00:00 =  1721058 / MJDN= -678943, TJD= -718943, LILIAN= -578102, UNIX= -719530

 

+0001-01-01(6) 00:00:00 =  1721424 / MJDN= -678577, TJD= -718577, LILIAN= -577736, UNIX= -719164

+0001-01-01(6) 00:00:00 =  1721424 / MJDN= -678577, TJD= -718577, LILIAN= -577736, UNIX= -719164

 

+0002-01-01(0) 00:00:00 =  1721789 / MJDN= -678212, TJD= -718212, LILIAN= -577371, UNIX= -718799

+0002-01-01(0) 00:00:00 =  1721789 / MJDN= -678212, TJD= -718212, LILIAN= -577371, UNIX= -718799

 

+1582-10-03(3) 00:00:00 =  2299159 / MJDN= -100842, TJD= -140842, LILIAN=      -1, UNIX= -141429

+1582-10-04(4) 00:00:00 =  2299160 / MJDN= -100841, TJD= -140841, LILIAN=       0, UNIX= -141428

+1582-10-15(5) 00:00:00 =  2299161 / MJDN= -100840, TJD= -140840, LILIAN=       1, UNIX= -141427

+1582-10-16(6) 00:00:00 =  2299162 / MJDN= -100839, TJD= -140839, LILIAN=       2, UNIX= -141426

+1582-10-03(3) 00:00:00 =  2299159 / MJDN= -100842, TJD= -140842, LILIAN=      -1, UNIX= -141429

+1582-10-04(4) 00:00:00 =  2299160 / MJDN= -100841, TJD= -140841, LILIAN=       0, UNIX= -141428

+1582-10-15(5) 00:00:00 =  2299161 / MJDN= -100840, TJD= -140840, LILIAN=       1, UNIX= -141427

+1582-10-16(6) 00:00:00 =  2299162 / MJDN= -100839, TJD= -140839, LILIAN=       2, UNIX= -141426

 

+1858-11-16(2) 00:00:00 =  2400000 / MJDN=      -1, TJD=  -40001, LILIAN=  100840, UNIX=  -40588

+1858-11-16(2) 00:00:00 =  2400000 / MJDN=      -1, TJD=  -40001, LILIAN=  100840, UNIX=  -40588

 

+1858-11-17(3) 00:00:00 =  2400001 / MJDN=       0, TJD=  -40000, LILIAN=  100841, UNIX=  -40587

+1858-11-17(3) 00:00:00 =  2400001 / MJDN=       0, TJD=  -40000, LILIAN=  100841, UNIX=  -40587

 

+1968-05-24(5) 00:00:00 =  2440001 / MJDN=   40000, TJD=       0, LILIAN=  140841, UNIX=    -587

+1968-05-24(5) 00:00:00 =  2440001 / MJDN=   40000, TJD=       0, LILIAN=  140841, UNIX=    -587

 

+1970-01-01(4) 00:00:00 =  2440588 / MJDN=   40587, TJD=     587, LILIAN=  141428, UNIX=       0

+1970-01-01(4) 00:00:00 =  2440588 / MJDN=   40587, TJD=     587, LILIAN=  141428, UNIX=       0

 

+2000-01-01(6) 00:00:00 =  2451545 / MJDN=   51544, TJD=   11544, LILIAN=  152385, UNIX=   10957

+2000-01-01(6) 00:00:00 =  2451545 / MJDN=   51544, TJD=   11544, LILIAN=  152385, UNIX=   10957

 

+2018-10-09(2) 00:00:00 =  2458401 / MJDN=   58400, TJD=   18400, LILIAN=  159241, UNIX=   17813

+2018-10-09(2) 00:00:00 =  2458401 / MJDN=   58400, TJD=   18400, LILIAN=  159241, UNIX=   17813

 

time() / 86400 = 17809

+2018-10-05(5) 16:30:50 =  2458397 / MJDN=   58396, TJD=   18396, LILIAN=  159237, UNIX=   17809

+2018-10-05(5) 16:30:50 =  2458397 / MJDN=   58396, TJD=   18396, LILIAN=  159237, UNIX=   17809

+2018-10-05(5) 16:30:50 =  2458397 / MJDN=   58396, TJD=   18396, LILIAN=  159237, UNIX=   17809

 

*/

 


Posted by 셈말짓기 :

char* sitoa (char* a, int i)  
{  
 int   sign=0;  
 int   temp=0;  
 char  buf[16];  
 char* ptr;   

 ptr = buf;   

 /* zero then return */ 
 if( i )  
 {  
  /* make string in reverse form */ 
  if( i < 0 )
  {   
   i = ~i + 1;   
   sign++;
  }  
  while( i )
  {   
   *ptr++ = (i % 10) + '0';   
   i = i / 10;
  }  
  if(sign)  
  {
   *ptr++ = '-';  
  }
  *ptr = '\0';   

  /* copy reverse order */ 
  for( i=0; i < (int)strlen(buf); i++ )  
  {
   *a++ = buf[strlen(buf)-i-1];  
  }
 }      
 else
 {
  *a++ = '0';   
 }

 return a;  
}   

char* uitoa (char *a, unsigned int i)  
{  
 int   sign=0;  
 int   temp=0;  
 char  buf[16];  
 char* ptr;   

 ptr = buf;   

 /* zero then return */ 
 if( i )  
 {  
  while( i )
  {   
   *ptr++ = (i % 10) + '0';   
   i = i / 10;
  }  
  *ptr = '\0';   

  /* copy reverse order */ 
  for( i=0; i < (int)strlen(buf); i++ )  
  {
   *a++ = buf[strlen(buf)-i-1];  
  }
 }      
 else
 {
  *a++ = '0';   
 }

 return a;  
}   

char* htoa( char *a, unsigned int x, int opt)  
{  
 int   i;  
 int   sign=0;  
 int   temp=0;  
 char  buf[16];  
 char* ptr;   

 ptr = buf;   

 /* zero then return */ 
 if( x )  
 {  
  /* make string in reverse form */ 
  while( x )  
  {
   *ptr++ = (x&0x0f)<10 ? (x&0x0f)+'0' : (x&0x0f)-10+opt; x>>= 4;
  }   

  *ptr = '\0';   

  /* copy reverse order */ 
  for( i=0; i < (int)strlen(buf); i++ )  
  {
   *a++ = buf[strlen(buf)-i-1];  
  }
 }  
 else
 {
  *a++ = '0';   
 }
 return a;  
}

char * ltoa (long val, char *buf, unsigned radix)
{
 char *p;  /* pointer to traverse string */
 char *firstdig;  /* pointer to first digit */
 char temp;       /* temp char */
 unsigned digval; /* value of digit */

 p = buf;

 if (radix == 10 && val < 0) {
  /* negative, so output '-' and negate */
  *p++ = '-';
  val = (unsigned long)(-(long)val);
 }

 firstdig = p;    /* save pointer to first digit */

 do {
  digval = (unsigned) (val % radix);
  val /= radix;       /* get next digit */

  /* convert to ascii and store */
  if (digval > 9)
   *p++ = (char) (digval - 10 + 'a');  /* a letter */
  else
   *p++ = (char) (digval + '0');       /* a digit */
 } while (val > 0);

 /* We now have the digit of the number in the buffer, but in reverse
 order.  Thus we reverse them now. */

 *p-- = '\\0';     /* terminate string; p points to last digit */

 do {
  temp = *p;
  *p = *firstdig;
  *firstdig = temp;   /* swap *p and *firstdig */
  --p;
  ++firstdig;  /* advance to next two digits */
 } while (firstdig < p); /* repeat until halfway */

 return buf;
}


char *ftoa(float f, int *status)
{
 typedef union
 {
  long  L;
  float F;
 } LF_t;

 long  mantissa, int_part, frac_part;
 short exp2;

 LF_t  x;
 char* p;
 

 static char outbuf[15];


 *status = 0;
 if (f == 0.0)
 {
  outbuf[0] = '0';
  outbuf[1] = '.';
  outbuf[2] = '0';
  outbuf[3] =  0;
  return outbuf;
 }
 x.F = f;

 exp2      = (unsigned char)(x.L >> 23) - 127;
 mantissa  = (x.L & 0xFFFFFF) | 0x800000;
 frac_part = 0;
 int_part  = 0;

 if (exp2 >= 31)
 {
  *status = _FTOA_TOO_LARGE;
  return 0;
 }
 else if (exp2 < -23)
 {
  *status = _FTOA_TOO_SMALL;
  return 0;
 }
 else if (exp2 >= 23)
 {
  int_part = mantissa << (exp2 - 23);
 }
 else if (exp2 >= 0)
 {
  int_part = mantissa >> (23 - exp2);
  frac_part = (mantissa << (exp2 + 1)) & 0xFFFFFF;
 }
 else /* if (exp2 < 0) */
 {
  frac_part = (mantissa & 0xFFFFFF) >> -(exp2 + 1);
 }
 p = outbuf;

 if (x.L < 0)
 {
  *p++ = '-';
 }
 if (int_part == 0)
 {
  *p++ = '0';
 }
 else
 {
  ltoa(p, int_part, 10);
  while (*p)
   p++;
 }
 *p++ = '.';

 if (frac_part == 0)
  *p++ = '0';
 else
 {
  char m, max;

  max = sizeof (outbuf) - (p - outbuf) - 1;
  if (max > 7)
  {
   max = 7;
  }

  /* print BCD */
  for (m = 0; m < max; m++)
  {
   /* frac_part *= 10; */
   frac_part = (frac_part << 3) + (frac_part << 1);

   *p++ = (frac_part >> 24) + '0';
   frac_part &= 0xFFFFFF;
  }
  /* delete ending zeroes */
  for (--p; p[0] == '0' && p[-1] != '.'; --p)
   ;
  ++p;
 }
 *p = 0;

 return outbuf;
}

Posted by 셈말짓기 :

// TestSprintF.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//

#include "stdafx.h"

int sprintf_with_args (char* buf, char* format, void** args)
{  
 char* start;  

 char  field_format[32];
 char* field;
 char* field_type;

 int   result;


 start = buf;

 while( *format )  
 {  
  if( *format != '%' )
  {  
   *buf++ = *format++;  
   continue;  
  }

  field      = field_format;
  field_type = 0;

  *field++ = *format++;
  while( *format && !field_type && field<field_format+32)
  {
   switch (*format)
   {
   case '%':
   case 'c'://int
   case 'C':
   case 'd':
   case 'i':
   case 'o':
   case 'u':
   case 'x':
   case 'X':
   case 'e':
   case 'E'://double
   case 'f':
   case 'g':
   case 'G':
   case 'a':
   case 'A':
   case 'n'://int*
   case 'p'://void*
   case 's'://char*
   case 'S'://wchar_t*
    field_type = format;
    break;
   }
   *field++ = *format++;
  }

  if (field_type)
  {
   *field = 0;
#pragma warning( push )
#pragma warning( disable: 4996 )
   switch (*field_type)
   {
   case '%':
    *buf++ = '%'; 
    break;

   case 'c'://int
   case 'C':
   case 'd':
   case 'i':
   case 'o':
   case 'u':
   case 'x':
   case 'X':
    result = sprintf (buf, field_format, *((int*)(*args)));
    if (result>0)
    {
     buf+=result;
    }
    else
    {
     return -1;
    }
    args++;
    break;
   case 'e':
   case 'E'://double
   case 'f':
   case 'g':
   case 'G':
   case 'a':
   case 'A':
    result = sprintf (buf, field_format, *((double*)(*args)));
    if (result>0)
    {
     buf+=result;
    }
    else
    {
     return -1;
    }
    args++;
    break;
   case 'n'://int*
   case 'p'://void*
   case 's'://char*
   case 'S'://wchar_t*
    result = sprintf (buf, field_format, *((int**)(*args)));
    if (result>0)
    {
     buf+=result;
    }
    else
    {
     return -1;
    }
    args++;
    break;
   }
  }
#pragma warning( pop )
 }
 *buf = '\0';   

 return (int)(buf-start); 

 return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
 char*  pFormatString = "문자열%%[%d][%d][%5d] [%s] [%x] [%e][%.03f]메%q세지 \r\n";
 char   szMessage[256];
 void*  pParameters[7];
 int    Parameters[32];
 char   param1 = 100;
 short  param2 = 101;
 int    param3 = 102;
 char*  param4 = "xxx";
 int    param5 = -1;
 double param6 = 0.1;
 double param7 = 0.5;

 Parameters[0] = (int)param1;
 Parameters[1] = (int)param2;
 Parameters[2] = (int)param3;
 Parameters[3] = (int)param4;
 Parameters[4] = (int)param5;
 memcpy (&Parameters[5], &param6, sizeof(double));
 memcpy (&Parameters[7], &param7, sizeof(double));

#if 0
 pParameters[0] = &Parameters[0];
 pParameters[1] = &Parameters[1];
 pParameters[2] = &Parameters[2];
 pParameters[3] = &Parameters[3];
 pParameters[4] = &Parameters[4];
 pParameters[5] = &Parameters[5];
 pParameters[6] = &Parameters[7];
#else
 pParameters[0] = &param1;
 pParameters[1] = &param2;
 pParameters[2] = &param3;
 pParameters[3] = &param4;
 pParameters[4] = &param5;
 pParameters[5] = &param6;
 pParameters[6] = &param7;
#endif

 sprintf_with_args (szMessage, pFormatString, pParameters);
 printf (szMessage);

 return 0;
}


 

Posted by 셈말짓기 :

EnumChildWindows

2009. 7. 3. 12:07 from 셈말짓기/옹알이

Child Window 순회 함수...
그런데... MS가 만든게 있었네 =_=;
 
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
 CWindow wnd (hwnd);
 wnd.SetFont ((HFONT)lParam);

 return TRUE;
}

BOOL myEnumChildWindows (HWND hParent, WNDENUMPROC lpEnumProc, LPARAM lParam)
{
 HWND hEnum;
 BOOL bContinue = TRUE;

 TCHAR szCaption[256];

 hEnum = ::GetWindow (hParent, GW_CHILD);
 while (NULL!=hEnum && TRUE==bContinue)
 {
  ::GetWindowText (hEnum, szCaption, 256);
  ATLTRACE (_T("Child=%08x,%s \r\n"), hEnum, szCaption);

  bContinue = lpEnumProc (hEnum, lParam);

  if (NULL!=::GetWindow (hEnum, GW_CHILD))
  {
   if (TRUE==bContinue)
    bContinue=EnumChildWindows (hEnum, lpEnumProc, lParam);
  }

  hEnum = ::GetNextWindow (hEnum, GW_HWNDNEXT);
 }

 return bContinue;
}

Posted by 셈말짓기 :
http://www.codeproject.com/KB/cpp/cppproperties.aspx
에 있는걸 참고해서 문제가 있었던 object1.prop = object2.prop의 문제를 수정하였으나
그 이외에도 문제가 많다.


#include <stdio.h>
#include <atlbase.h>
#include <iostream>

/////////////////////////////////////////////////////////////////////////////
//
// Interface of the CProperty<TContainer,TValue> class
//    Ref=http://www.codeproject.com/KB/cpp/cppproperties.aspx
//        
/////////////////////////////////////////////////////////////////////////////
template<typename TContainer, typename TValue>
class CProperty
{
public:
    typedef TValue (TContainer::*SETPROC)(TValue value);
    typedef TValue (TContainer::*GETPROC)(void) const;

private:
    TValue      m_Value;
    SETPROC     m_pSetProc;
    GETPROC     m_pGetProc;
    TContainer* m_pContainer;

public:
    CProperty() :
        m_pSetProc  (NULL),
        m_pGetProc  (NULL),
        m_pContainer(NULL)
    {
    }

    CProperty(const CProperty& o)
    {
        m_Value = o.Get();
    }

public:
    void Bind (TContainer* container, GETPROC getproc, SETPROC setproc)
    {
        m_pContainer = container;
        m_pGetProc   = getproc;
        m_pSetProc   = setproc;
    }

    CProperty<TContainer,TValue> operator = (const CProperty& rhs)
    {
        Set(rhs.Get());
    
        return *this;
    }

    TValue Get (void) const
    {
        ATLASSERT(m_pContainer != NULL);
        ATLASSERT(m_pGetProc   != NULL);

        return (m_pContainer->*m_pGetProc)();
    }

    TValue Set (const TValue& value)
    {
        ATLASSERT(m_pContainer != NULL);
        ATLASSERT(m_pSetProc   != NULL);

        return (m_pContainer->*m_pSetProc)(value);    
    }

    operator TValue (void) const
    {
        return Get ();
    }

    TValue operator = (const TValue& value)
    {
        return Set (value);
    }
};

/////////////////////////////////////////////////////////////////////////////
//
// Interface of the CProperties class
//
/////////////////////////////////////////////////////////////////////////////
class CProperties
{
private:
    int m_Count;

public:
    CProperty<CProperties,int> Count;

public:
    CProperties () : m_Count(0)
    {
        Count.Bind (this, &CProperties::get_Count, &CProperties::set_Count);
    }

public:
    int get_Count (void) const
    {
        printf ("\t\t%08x::get_Count()=%d\r\n", this, m_Count);

        return m_Count;
    }

    int set_Count (int n)
    {
        m_Count = n;

        printf ("\t\t%08x::set_Count(%d)\r\n", this, m_Count);

        return m_Count;
    }
};

/////////////////////////////////////////////////////////////////////////////
//
// Test
//
/////////////////////////////////////////////////////////////////////////////
static int Test (void)
{
    CProperties prop;
    
    prop.Count = 100;

    return prop.Count;
}

/////////////////////////////////////////////////////////////////////////////
//
// Startup
//
/////////////////////////////////////////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
    CProperties Properties1, Properties2;

    printf ("\t\tProperties1=%08x\r\n", &Properties1);
    printf ("\t\tProperties2=%08x\r\n", &Properties2);

    printf ("-------------------------------------------------------------\r\n");

    printf ("Test()=%d\r\n", Test());

    printf ("-------------------------------------------------------------\r\n");

    Properties1.Count = 200;

    Properties2.Count = Properties1.Count;

    Properties1.Count = 100;

    std::cout << "Properties1.Count=" <<Properties1.Count << std::endl;
    std::cout << "Properties2.Count=" <<Properties2.Count << std::endl;

    printf ("-------------------------------------------------------------\r\n");

    int n=Properties2.Count;

    printf ("printf() n     =%d\r\n",n);
    printf ("printf() cast  =%d\r\n",(int)Properties2.Count);
    printf ("printf() nocast=%d\r\n",Properties2.Count); // 방법없음

    return 0;
}

Posted by 셈말짓기 :