~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to drizzled/temporal.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-10-02 14:17:48 UTC
  • mfrom: (1.1.1 upstream)
  • mto: (2.1.17 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20101002141748-m6vbfbfjhrw1153e
Tags: 2010.09.1802-1
* New upstream release.
* Removed pid-file argument hack.
* Updated GPL-2 address to be new address.
* Directly copy in drizzledump.1 since debian doesn't have sphinx 1.0 yet.
* Link to jquery from libjs-jquery. Add it as a depend.
* Add drizzled.8 symlink to the install files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
#include "config.h"
38
38
 
39
39
#include "drizzled/charset_info.h"
40
 
#include "drizzled/my_decimal.h"
 
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"
49
46
 
50
47
#include <time.h>
51
48
 
 
49
#include <cstdio>
52
50
#include <ostream>
53
51
#include <iomanip>
54
52
#include <vector>
83
81
      + _seconds);
84
82
}
85
83
 
 
84
#if defined(TARGET_OS_SOLARIS)
 
85
/* @TODO: Replace this with Boost.DateTime */
 
86
static time_t timegm(struct tm *my_time)
 
87
{
 
88
        time_t local_secs, gm_secs;
 
89
        struct tm gm__rec, *gm_time;
 
90
 
 
91
        // Interpret 't' as the local time and convert it to seconds since the Epoch
 
92
        local_secs = mktime(my_time);
 
93
        if (local_secs == -1)
 
94
  {
 
95
                my_time->tm_hour--;
 
96
                local_secs = mktime (my_time);
 
97
                if (local_secs == -1)
 
98
                        return -1; 
 
99
                local_secs += 3600;
 
100
        }
 
101
        
 
102
        // Get the gmtime based on the local seconds since the Epoch
 
103
        gm_time = gmtime_r(&local_secs, &gm__rec);
 
104
        gm_time->tm_isdst = 0;
 
105
        
 
106
        // Interpret gmtime as the local time and convert it to seconds since the Epoch
 
107
        gm_secs = mktime (gm_time);
 
108
        if (gm_secs == -1)
 
109
  {
 
110
                gm_time->tm_hour--;
 
111
                gm_secs = mktime (gm_time);
 
112
                if (gm_secs == -1)
 
113
                        return -1; 
 
114
                gm_secs += 3600;
 
115
        }
 
116
        
 
117
        // Return the local time adjusted by the difference from GM time.
 
118
        return (local_secs - (gm_secs - local_secs));
 
119
}
 
120
#endif
 
121
 
86
122
void Temporal::set_epoch_seconds()
87
123
{
88
124
  /* 
795
831
 
796
832
  return *this;
797
833
}
798
 
#ifdef NOTYETIMPLEMENTED
799
 
Date& Date::operator+=(const TemporalIntervalYear &rhs)
800
 
{
801
 
  /* Simple one...add the years and adjust for any leaps */
802
 
  int64_t new_years= _years;
803
 
  new_years+= rhs._years;
804
 
  if (new_years > DRIZZLE_MAX_YEARS_SQL)
805
 
  {
806
 
    /* 
807
 
     * Set everything to zero. We got an overflow.
808
 
     * @TODO Exceptions would be great here...
809
 
     */
810
 
    _reset();
811
 
    _overflow= true;
812
 
    return *this;
813
 
  }
814
 
  _years= (uint32_t) new_years;
815
 
  if (_months == 2 && _days == 29 && days_in_gregorian_year_month(_years, _months) != 366)
816
 
    _days= 28;
817
 
  return *this;
818
 
819
 
 
820
 
Date& Date::operator-=(const TemporalIntervalYear &rhs)
821
 
{
822
 
  /* Simple one...subtract the years and adjust for any leaps */
823
 
  int64_t new_years= _years;
824
 
  new_years-= rhs._years;
825
 
  if (new_years < 0)
826
 
  {
827
 
    /* 
828
 
     * Set everything to zero. We got an overflow.
829
 
     * @TODO Exceptions would be great here...
830
 
     */
831
 
    _reset();
832
 
    _overflow= true;
833
 
    return *this;
834
 
  }
835
 
  _years= (uint32_t) new_years;
836
 
  if (_months == 2 && _days == 29 && days_in_gregorian_year_month(_years, _months) != 366)
837
 
    _days= 28;
838
 
  return *this;
839
 
840
 
 
841
 
Date& Date::operator+=(const TemporalIntervalDayOrWeek &rhs)
842
 
{
843
 
  /* Simple one...add the days */
844
 
  int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, _days) + rhs._days;
845
 
  gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
846
 
  return *this;
847
 
848
 
 
849
 
Date& Date::operator-=(const TemporalIntervalDayOrWeek &rhs)
850
 
{
851
 
  /* Simple one...subtract the days */
852
 
  int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, _days) - rhs._days;
853
 
  gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
854
 
  return *this;
855
 
856
 
 
857
 
Date& Date::operator+=(const TemporalIntervalYearMonth &rhs)
858
 
{
859
 
  /* Simple one...add the months in the period adjust */
860
 
  int64_t period= (_years * 12) + (rhs._years * 12) + (_months - 1) + rhs._months;
861
 
  int64_t new_years= (period / 12);
862
 
  if (new_years > DRIZZLE_MAX_YEARS_SQL)
863
 
  {
864
 
    /* 
865
 
     * Set everything to zero. We got an overflow.
866
 
     * @TODO Exceptions would be great here...
867
 
     */
868
 
    _reset();
869
 
    _overflow= true;
870
 
    return *this;
871
 
  }
872
 
  _years= (uint32_t) new_years;
873
 
  _months= (uint32_t) (period % 12) + 1;
874
 
  
875
 
  /* Adjust day if the new month doesn't have enough days */
876
 
  uint32_t days_in_new_month= days_in_gregorian_year_month(_years, _months);
877
 
  if (_days > days_in_new_month)
878
 
    _days= days_in_new_month;
879
 
  return *this;
880
 
881
 
 
882
 
Date& Date::operator-=(const TemporalIntervalYearMonth &rhs)
883
 
{
884
 
  /* Simple one...subtract the months in the period and adjust */
885
 
  int64_t period= (_years * 12) - (rhs._years * 12) + (_months - 1) - rhs._months;
886
 
  int64_t new_years= (period / 12);
887
 
  if (new_years < 0)
888
 
  {
889
 
    /* 
890
 
     * Set everything to zero. We got an overflow.
891
 
     * @TODO Exceptions would be great here...
892
 
     */
893
 
    _reset();
894
 
    _overflow= true;
895
 
    return *this;
896
 
  }
897
 
  _years= (uint32_t) (period / 12);
898
 
  _months= (uint32_t) (period % 12) + 1;
899
 
  
900
 
  /* Adjust day if the new month doesn't have enough days */
901
 
  uint32_t days_in_new_month= days_in_gregorian_year_month(_years, _months);
902
 
  if (_days > days_in_new_month)
903
 
    _days= days_in_new_month;
904
 
  return *this;
905
 
906
 
 
907
 
Date& Date::operator+=(const TemporalIntervalDayOrLess &rhs)
908
 
{
909
 
  /* 
910
 
   * Convert the temporal and the interval into a number of 
911
 
   * microseconds, then add them together and convert the
912
 
   * resulting microseconds back into a broken-down temporal
913
 
   * component.
914
 
   */
915
 
  int64_t new_seconds;
916
 
  int64_t new_microseconds;
917
 
  int64_t extra_sec;
918
 
  int64_t new_days;
919
 
  new_microseconds= _useconds + rhs._useconds;
920
 
  extra_sec= new_microseconds / INT64_C(1000000);
921
 
  new_microseconds= new_microseconds % INT64_C(1000000);
922
 
 
923
 
  new_seconds= ((_days - 1) * 3600 * 24) + (_hours * 3600) + (_minutes * 60) + _seconds;
924
 
  new_seconds+= (rhs._days * 3600 * 24) + (rhs._hours * 3600) + (rhs._minutes * 60) + rhs._seconds;
925
 
  new_seconds+= extra_sec;
926
 
 
927
 
  if (new_microseconds < 0)
928
 
  {
929
 
    new_microseconds+= INT64_C(1000000);
930
 
    new_seconds--;
931
 
  }
932
 
  
933
 
  new_days= new_seconds / (3600 * 24L);
934
 
  new_seconds-= new_days * 3600 * 24L;
935
 
  if (new_seconds < 0)
936
 
  {
937
 
    new_days--;
938
 
    new_seconds+= 3600 * 24L;
939
 
  }
940
 
  _useconds= (uint32_t) new_microseconds;
941
 
  _seconds= (uint32_t) (new_seconds % 60);
942
 
  _minutes= (uint32_t) ((new_seconds / 60) % 60);
943
 
  _hours= (uint32_t) (new_seconds / 3600);
944
 
  int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, 1) + new_days;
945
 
  gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
946
 
  return *this;
947
 
}
948
 
 
949
 
Date& Date::operator-=(const TemporalIntervalDayOrLess &rhs)
950
 
{
951
 
  /* 
952
 
   * Convert the temporal and the interval into a number of 
953
 
   * microseconds, then subtract them from each other and convert 
954
 
   * the resulting microseconds back into a broken-down temporal
955
 
   * component.
956
 
   */
957
 
  int64_t new_seconds;
958
 
  int64_t new_microseconds;
959
 
  int64_t extra_sec;
960
 
  int64_t new_days;
961
 
  new_microseconds= _useconds - rhs._useconds;
962
 
  extra_sec= new_microseconds / INT64_C(1000000);
963
 
  new_microseconds= new_microseconds % INT64_C(1000000);
964
 
 
965
 
  new_seconds= ((_days - 1) * 3600 * 24) + (_hours * 3600) + (_minutes * 60) + _seconds;
966
 
  new_seconds-= (rhs._days * 3600 * 24) + (rhs._hours * 3600) + (rhs._minutes * 60) + rhs._seconds;
967
 
  new_seconds+= extra_sec;
968
 
 
969
 
  if (new_microseconds < 0)
970
 
  {
971
 
    new_microseconds+= INT64_C(1000000);
972
 
    new_seconds--;
973
 
  }
974
 
  
975
 
  new_days= new_seconds / (3600 * 24L);
976
 
  new_seconds-= new_days * 3600 * 24L;
977
 
  if (new_seconds < 0)
978
 
  {
979
 
    new_days--;
980
 
    new_seconds+= 3600 * 24L;
981
 
  }
982
 
  _useconds= (uint32_t) new_microseconds;
983
 
  _seconds= (uint32_t) (new_seconds % 60);
984
 
  _minutes= (uint32_t) ((new_seconds / 60) % 60);
985
 
  _hours= (uint32_t) (new_seconds / 3600);
986
 
  int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, 1) + new_days;
987
 
  gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
988
 
  return *this;
989
 
}
990
 
#endif /* NOTYETIMPLEMENTED */
 
834
 
991
835
/*
992
836
 * Comparison operators between a Date and a Timestamp
993
837
 */
1019
863
}
1020
864
bool Date::operator>(const Timestamp& rhs)
1021
865
{
1022
 
  return ! (*this < rhs);
 
866
  return ! (*this <= rhs);
1023
867
}
1024
868
bool Date::operator>=(const Timestamp& rhs)
1025
869
{
1026
 
  return ! (*this <= rhs);
 
870
  return ! (*this < rhs);
1027
871
}
1028
872
/*
1029
873
 * Comparison operators between a Timestamp and a Date
1056
900
}
1057
901
bool Timestamp::operator>(const Date& rhs)
1058
902
{
1059
 
  return ! (*this < rhs);
 
903
  return ! (*this <= rhs);
1060
904
}
1061
905
bool Timestamp::operator>=(const Date& rhs)
1062
906
{
1063
 
  return ! (*this <= rhs);
 
907
  return ! (*this < rhs);
1064
908
}
1065
909
/*
1066
910
 * Comparison operators between a Timestamp and a DateTime
1109
953
}
1110
954
bool Timestamp::operator>(const DateTime& rhs)
1111
955
{
1112
 
  return ! (*this < rhs);
 
956
  return ! (*this <= rhs);
1113
957
}
1114
958
bool Timestamp::operator>=(const DateTime& rhs)
1115
959
{
1116
 
  return ! (*this <= rhs);
 
960
  return ! (*this < rhs);
1117
961
}
1118
962
/*
1119
963
 * Comparison operators between two Timestamps
1136
980
}
1137
981
bool Timestamp::operator>(const Timestamp& rhs)
1138
982
{
1139
 
  return ! (*this < rhs);
 
983
  return ! (*this <= rhs);
1140
984
}
1141
985
bool Timestamp::operator>=(const Timestamp& rhs)
1142
986
{
1143
 
  return ! (*this <= rhs);
 
987
  return ! (*this < rhs);
1144
988
}
1145
989
 
1146
990
/**
1341
1185
bool Time::from_int32_t(const int32_t from)
1342
1186
{
1343
1187
  uint32_t copy_from= (uint32_t) from;
1344
 
  _hours= copy_from % INT32_C(10000);
1345
 
  _minutes= copy_from % INT32_C(100);
1346
 
  _seconds= copy_from & 3; /* Masks off all but last 2 digits */
 
1188
  _hours= copy_from / INT32_C(10000);
 
1189
  _minutes= (copy_from % INT32_C(10000)) / INT32_C(100);
 
1190
  _seconds= copy_from % INT32_C(100); /* Masks off all but last 2 digits */
1347
1191
  return is_valid();
1348
1192
}
1349
1193
 
1382
1226
    else if (copy_from <  DRIZZLE_YY_PART_YEAR * 10000000000LL + 101000000LL)
1383
1227
      return false;
1384
1228
    else if (copy_from <= 991231235959LL)
1385
 
      copy_from= copy_from + 19000000000000LL;          /* YYMMDDHHMMSS, 1970-1999 */
 
1229
      copy_from= copy_from + 19000000000000LL;    /* YYMMDDHHMMSS, 1970-1999 */
1386
1230
  }
1387
1231
 
1388
1232
  part1= (int64_t) (copy_from / 1000000LL);
1561
1405
bool Timestamp::is_valid() const
1562
1406
{
1563
1407
  return DateTime::is_valid() 
1564
 
      && in_unix_epoch_range(_years, _months, _days, _hours, _minutes, _seconds);
 
1408
      && in_unix_epoch_range(_years, _months, _days, _hours, _minutes, _seconds)
 
1409
      && (_seconds <= 59);
1565
1410
}
1566
1411
 
1567
1412
bool MicroTimestamp::is_valid() const