~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/temporal.cc

Merged Pawel from lp:~pblokus/drizzle/unittests-plugin-interfaces

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
#include "drizzled/decimal.h"
41
41
#include "drizzled/calendar.h"
42
42
#include "drizzled/temporal.h"
43
 
#ifdef NOTYETIMPLEMENTED
44
 
#include "drizzled/temporal_interval.h"
45
 
#endif
46
43
#include "drizzled/temporal_format.h"
47
44
#include "drizzled/time_functions.h"
48
45
#include "time.h"
796
793
 
797
794
  return *this;
798
795
}
799
 
#ifdef NOTYETIMPLEMENTED
800
 
Date& Date::operator+=(const TemporalIntervalYear &rhs)
801
 
{
802
 
  /* Simple one...add the years and adjust for any leaps */
803
 
  int64_t new_years= _years;
804
 
  new_years+= rhs._years;
805
 
  if (new_years > DRIZZLE_MAX_YEARS_SQL)
806
 
  {
807
 
    /* 
808
 
     * Set everything to zero. We got an overflow.
809
 
     * @TODO Exceptions would be great here...
810
 
     */
811
 
    _reset();
812
 
    _overflow= true;
813
 
    return *this;
814
 
  }
815
 
  _years= (uint32_t) new_years;
816
 
  if (_months == 2 && _days == 29 && days_in_gregorian_year_month(_years, _months) != 366)
817
 
    _days= 28;
818
 
  return *this;
819
 
820
 
 
821
 
Date& Date::operator-=(const TemporalIntervalYear &rhs)
822
 
{
823
 
  /* Simple one...subtract the years and adjust for any leaps */
824
 
  int64_t new_years= _years;
825
 
  new_years-= rhs._years;
826
 
  if (new_years < 0)
827
 
  {
828
 
    /* 
829
 
     * Set everything to zero. We got an overflow.
830
 
     * @TODO Exceptions would be great here...
831
 
     */
832
 
    _reset();
833
 
    _overflow= true;
834
 
    return *this;
835
 
  }
836
 
  _years= (uint32_t) new_years;
837
 
  if (_months == 2 && _days == 29 && days_in_gregorian_year_month(_years, _months) != 366)
838
 
    _days= 28;
839
 
  return *this;
840
 
841
 
 
842
 
Date& Date::operator+=(const TemporalIntervalDayOrWeek &rhs)
843
 
{
844
 
  /* Simple one...add the days */
845
 
  int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, _days) + rhs._days;
846
 
  gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
847
 
  return *this;
848
 
849
 
 
850
 
Date& Date::operator-=(const TemporalIntervalDayOrWeek &rhs)
851
 
{
852
 
  /* Simple one...subtract the days */
853
 
  int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, _days) - rhs._days;
854
 
  gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
855
 
  return *this;
856
 
857
 
 
858
 
Date& Date::operator+=(const TemporalIntervalYearMonth &rhs)
859
 
{
860
 
  /* Simple one...add the months in the period adjust */
861
 
  int64_t period= (_years * 12) + (rhs._years * 12) + (_months - 1) + rhs._months;
862
 
  int64_t new_years= (period / 12);
863
 
  if (new_years > DRIZZLE_MAX_YEARS_SQL)
864
 
  {
865
 
    /* 
866
 
     * Set everything to zero. We got an overflow.
867
 
     * @TODO Exceptions would be great here...
868
 
     */
869
 
    _reset();
870
 
    _overflow= true;
871
 
    return *this;
872
 
  }
873
 
  _years= (uint32_t) new_years;
874
 
  _months= (uint32_t) (period % 12) + 1;
875
 
  
876
 
  /* Adjust day if the new month doesn't have enough days */
877
 
  uint32_t days_in_new_month= days_in_gregorian_year_month(_years, _months);
878
 
  if (_days > days_in_new_month)
879
 
    _days= days_in_new_month;
880
 
  return *this;
881
 
882
 
 
883
 
Date& Date::operator-=(const TemporalIntervalYearMonth &rhs)
884
 
{
885
 
  /* Simple one...subtract the months in the period and adjust */
886
 
  int64_t period= (_years * 12) - (rhs._years * 12) + (_months - 1) - rhs._months;
887
 
  int64_t new_years= (period / 12);
888
 
  if (new_years < 0)
889
 
  {
890
 
    /* 
891
 
     * Set everything to zero. We got an overflow.
892
 
     * @TODO Exceptions would be great here...
893
 
     */
894
 
    _reset();
895
 
    _overflow= true;
896
 
    return *this;
897
 
  }
898
 
  _years= (uint32_t) (period / 12);
899
 
  _months= (uint32_t) (period % 12) + 1;
900
 
  
901
 
  /* Adjust day if the new month doesn't have enough days */
902
 
  uint32_t days_in_new_month= days_in_gregorian_year_month(_years, _months);
903
 
  if (_days > days_in_new_month)
904
 
    _days= days_in_new_month;
905
 
  return *this;
906
 
907
 
 
908
 
Date& Date::operator+=(const TemporalIntervalDayOrLess &rhs)
909
 
{
910
 
  /* 
911
 
   * Convert the temporal and the interval into a number of 
912
 
   * microseconds, then add them together and convert the
913
 
   * resulting microseconds back into a broken-down temporal
914
 
   * component.
915
 
   */
916
 
  int64_t new_seconds;
917
 
  int64_t new_microseconds;
918
 
  int64_t extra_sec;
919
 
  int64_t new_days;
920
 
  new_microseconds= _useconds + rhs._useconds;
921
 
  extra_sec= new_microseconds / INT64_C(1000000);
922
 
  new_microseconds= new_microseconds % INT64_C(1000000);
923
 
 
924
 
  new_seconds= ((_days - 1) * 3600 * 24) + (_hours * 3600) + (_minutes * 60) + _seconds;
925
 
  new_seconds+= (rhs._days * 3600 * 24) + (rhs._hours * 3600) + (rhs._minutes * 60) + rhs._seconds;
926
 
  new_seconds+= extra_sec;
927
 
 
928
 
  if (new_microseconds < 0)
929
 
  {
930
 
    new_microseconds+= INT64_C(1000000);
931
 
    new_seconds--;
932
 
  }
933
 
  
934
 
  new_days= new_seconds / (3600 * 24L);
935
 
  new_seconds-= new_days * 3600 * 24L;
936
 
  if (new_seconds < 0)
937
 
  {
938
 
    new_days--;
939
 
    new_seconds+= 3600 * 24L;
940
 
  }
941
 
  _useconds= (uint32_t) new_microseconds;
942
 
  _seconds= (uint32_t) (new_seconds % 60);
943
 
  _minutes= (uint32_t) ((new_seconds / 60) % 60);
944
 
  _hours= (uint32_t) (new_seconds / 3600);
945
 
  int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, 1) + new_days;
946
 
  gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
947
 
  return *this;
948
 
}
949
 
 
950
 
Date& Date::operator-=(const TemporalIntervalDayOrLess &rhs)
951
 
{
952
 
  /* 
953
 
   * Convert the temporal and the interval into a number of 
954
 
   * microseconds, then subtract them from each other and convert 
955
 
   * the resulting microseconds back into a broken-down temporal
956
 
   * component.
957
 
   */
958
 
  int64_t new_seconds;
959
 
  int64_t new_microseconds;
960
 
  int64_t extra_sec;
961
 
  int64_t new_days;
962
 
  new_microseconds= _useconds - rhs._useconds;
963
 
  extra_sec= new_microseconds / INT64_C(1000000);
964
 
  new_microseconds= new_microseconds % INT64_C(1000000);
965
 
 
966
 
  new_seconds= ((_days - 1) * 3600 * 24) + (_hours * 3600) + (_minutes * 60) + _seconds;
967
 
  new_seconds-= (rhs._days * 3600 * 24) + (rhs._hours * 3600) + (rhs._minutes * 60) + rhs._seconds;
968
 
  new_seconds+= extra_sec;
969
 
 
970
 
  if (new_microseconds < 0)
971
 
  {
972
 
    new_microseconds+= INT64_C(1000000);
973
 
    new_seconds--;
974
 
  }
975
 
  
976
 
  new_days= new_seconds / (3600 * 24L);
977
 
  new_seconds-= new_days * 3600 * 24L;
978
 
  if (new_seconds < 0)
979
 
  {
980
 
    new_days--;
981
 
    new_seconds+= 3600 * 24L;
982
 
  }
983
 
  _useconds= (uint32_t) new_microseconds;
984
 
  _seconds= (uint32_t) (new_seconds % 60);
985
 
  _minutes= (uint32_t) ((new_seconds / 60) % 60);
986
 
  _hours= (uint32_t) (new_seconds / 3600);
987
 
  int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, 1) + new_days;
988
 
  gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
989
 
  return *this;
990
 
}
991
 
#endif /* NOTYETIMPLEMENTED */
 
796
 
992
797
/*
993
798
 * Comparison operators between a Date and a Timestamp
994
799
 */
1020
825
}
1021
826
bool Date::operator>(const Timestamp& rhs)
1022
827
{
1023
 
  return ! (*this < rhs);
 
828
  return ! (*this <= rhs);
1024
829
}
1025
830
bool Date::operator>=(const Timestamp& rhs)
1026
831
{
1027
 
  return ! (*this <= rhs);
 
832
  return ! (*this < rhs);
1028
833
}
1029
834
/*
1030
835
 * Comparison operators between a Timestamp and a Date
1057
862
}
1058
863
bool Timestamp::operator>(const Date& rhs)
1059
864
{
1060
 
  return ! (*this < rhs);
 
865
  return ! (*this <= rhs);
1061
866
}
1062
867
bool Timestamp::operator>=(const Date& rhs)
1063
868
{
1064
 
  return ! (*this <= rhs);
 
869
  return ! (*this < rhs);
1065
870
}
1066
871
/*
1067
872
 * Comparison operators between a Timestamp and a DateTime
1110
915
}
1111
916
bool Timestamp::operator>(const DateTime& rhs)
1112
917
{
1113
 
  return ! (*this < rhs);
 
918
  return ! (*this <= rhs);
1114
919
}
1115
920
bool Timestamp::operator>=(const DateTime& rhs)
1116
921
{
1117
 
  return ! (*this <= rhs);
 
922
  return ! (*this < rhs);
1118
923
}
1119
924
/*
1120
925
 * Comparison operators between two Timestamps
1137
942
}
1138
943
bool Timestamp::operator>(const Timestamp& rhs)
1139
944
{
1140
 
  return ! (*this < rhs);
 
945
  return ! (*this <= rhs);
1141
946
}
1142
947
bool Timestamp::operator>=(const Timestamp& rhs)
1143
948
{
1144
 
  return ! (*this <= rhs);
 
949
  return ! (*this < rhs);
1145
950
}
1146
951
 
1147
952
/**
1342
1147
bool Time::from_int32_t(const int32_t from)
1343
1148
{
1344
1149
  uint32_t copy_from= (uint32_t) from;
1345
 
  _hours= copy_from % INT32_C(10000);
1346
 
  _minutes= copy_from % INT32_C(100);
1347
 
  _seconds= copy_from & 3; /* Masks off all but last 2 digits */
 
1150
  _hours= copy_from / INT32_C(10000);
 
1151
  _minutes= (copy_from % INT32_C(10000)) / INT32_C(100);
 
1152
  _seconds= copy_from % INT32_C(100); /* Masks off all but last 2 digits */
1348
1153
  return is_valid();
1349
1154
}
1350
1155
 
1383
1188
    else if (copy_from <  DRIZZLE_YY_PART_YEAR * 10000000000LL + 101000000LL)
1384
1189
      return false;
1385
1190
    else if (copy_from <= 991231235959LL)
1386
 
      copy_from= copy_from + 19000000000000LL;          /* YYMMDDHHMMSS, 1970-1999 */
 
1191
      copy_from= copy_from + 19000000000000LL;    /* YYMMDDHHMMSS, 1970-1999 */
1387
1192
  }
1388
1193
 
1389
1194
  part1= (int64_t) (copy_from / 1000000LL);