After thinking about this a bit, I decided it was probably best to keep constants as static const members of this routine instead of #defines exposed in the public API.

The reason is that these masks and offsets are not needed by programmers, only the unpack_ and pack_ functions. I'd rather all packing and unpacking go through the public function API than have programmers try to do it themselves, so I kept them in the original form. Here is the updated unpack routine:

/**
 * Helper function to unpack a uint64_t into
 * a DRIZZLE_TEMPORAL of type DRIZZLE_TEMPORAL_DATETIME.
 *
 * Returns a bool of whether the unpacking was
 * done correctly.
 *
 * @param source uint64_t to unpack
 * @param destination to unpack into
 */
static bool unpack_uint64_t_to_datetime(const uint64_t *from, DRIZZLE_TEMPORAL *to)
{
  /*
   * This data type is packed as follows:
   *
   * bits:
   *
   * | 63 | 62 | 61 | 60 | 59 | 58 | 57 | 56 |
   * | YY | YY | YY | YY | YY | YY | YY | YY |
   * | 55 | 54 | 53 | 52 | 51 | 50 | 49 | 48 |
   * | YY | YY | YY | YY | YY | YY | MM | MM |
   * | 47 | 46 | 45 | 44 | 43 | 42 | 41 | 40 |
   * | MM | MM | DD | DD | DD | DD | DD | HH |
   * | 39 | 38 | 37 | 36 | 35 | 34 | 33 | 32 |
   * | HH | HH | HH | HH | mm | mm | mm | mm |
   * | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 |
   * | mm | mm | SS | SS | SS | SS | SS | SS |
   * | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
   * | uS | uS | uS | uS | uS | uS | uS | uS |
   * | 15 | 14 | 13 | 12 | 11 | 10 | 09 | 08 |
   * | uS | uS | uS | uS | uS | uS | uS | uS |
   * | 07 | 06 | 05 | 04 | 03 | 02 | 01 | 00 |
   * | uS | uS | uS | uS | TT | TT | TT | TT |
   *
   * Where:
   *
   *  YY == Year      (1 - 9999)
   *  MM == Month     (1 - 12)
   *  DD == Days      (1 - 31)
   *  HH == Hours     (0 - 59)
   *  mm == Minutes   (0 - 59)
   *  SS == Seconds   (0 - 61) to account for leap seconds
   *  uS == Microseconds (0 - 999,999)
   *  TT == Type      (1-4 currently, may change)
   */
  static const uint32_t OFFSET_YEAR=      50;
  static const uint32_t OFFSET_MONTH=     46;
  static const uint32_t OFFSET_DAY=       41;
  static const uint32_t OFFSET_HOUR=      36;
  static const uint32_t OFFSET_MINUTE=    30;
  static const uint32_t OFFSET_SECOND=    24;
  static const uint32_t OFFSET_USECOND=   4;
  static const uint64_t MASK_YEAR=        0xFFFC000000000000ULL;
  static const uint64_t MASK_MONTH=       0x0003C00000000000ULL;
  static const uint64_t MASK_DAY=         0x00003E0000000000ULL;
  static const uint64_t MASK_HOUR=        0x000001F000000000ULL;
  static const uint64_t MASK_MINUTE=      0x0000000FC0000000ULL;
  static const uint64_t MASK_SECOND=      0x000000003F000000ULL;
  static const uint64_t MASK_USECOND=     0x0000000000FFFFF0ULL;
  static const uint64_t MASK_TYPE=        0x000000000000000FULL;

uint32_t tmp_years= (from & MASK_YEAR) >> OFFSET_YEAR; uint32_t tmp_months= (from & MASK_MONTH) >> OFFSET_MONTH;
  uint32_t tmp_days=                      (from & MASK_DAY) >> OFFSET_DAY;
uint32_t tmp_hours= (from & MASK_HOUR) >> OFFSET_HOUR; uint32_t tmp_minutes= (from & MASK_MINUTE) >> OFFSET_MINUTE; uint32_t tmp_seconds= (from & MASK_SECOND) >> OFFSET_SECOND; uint32_t tmp_useconds= (from & MASK_USECOND) >> OFFSET_USECOND; enum drizzle_temporal_type tmp_type= (enum drizzle_temporal_type) (from & MASK_TYPE);

  if (tmp_type != DRIZZLE_TEMPORAL_DATETIME)
    return false;

  /* Construct the DRIZZLE_TEMPORAL */
  to->years= tmp_years;
  to->months= tmp_months;
  to->days= tmp_days;
  to->hours= tmp_hours;
  to->minutes= tmp_minutes;
  to->seconds= tmp_seconds;
  to->useconds= tmp_useconds;

if (tmp_years >= DRIZZLE_MIN_YEARS_EPOCH && tmp_years <= DRIZZLE_MAX_YEARS_EPOCH)
  {
    /* Add timestamp fields for timestamp-like values */
    time_t tmp_epoch_seconds;

    bool res= temporal_to_time_t(to, &tmp_epoch_seconds);
    if (res)
    {
      to->epoch_seconds= tmp_epoch_seconds;
    }
  }
  return true;
}


Francesco wrote:
Disclaimer I'm not a c programmer but did some assembler in te '90

Below a routine that have the advantage to not use offsets 4,36,24,30,50,46,41; I will play those to national "lotteria" however

Alternatively the offsets (and the bit lenghts) should be made available to the world with public constants IM(very)HO

so what about this routine to unpack the data ?

/**
  * Helper function to unpack a uint64_t into
  * a DRIZZLE_TEMPORAL of type DRIZZLE_TEMPORAL_DATETIME.
  *
  * Returns a bool of whether the unpacking was
  * done correctly.
  *
  * @param source uint64_t to unpack
  * @param destination to unpack into
  */
static bool unpack_uint64_t_to_datetime(const uint64_t *from, DRIZZLE_TEMPORAL *to)
{
   /*
    * This data type is packed as follows:
    *
    * bits:
    *
    * | 63 | 62 | 61 | 60 | 59 | 58 | 57 | 56 |
    * | YY | YY | YY | YY | YY | YY | YY | YY |
    * | 55 | 54 | 53 | 52 | 51 | 50 | 49 | 48 |
    * | YY | YY | YY | YY | YY | YY | MM | MM |
    * | 47 | 46 | 45 | 44 | 43 | 42 | 41 | 40 |
    * | MM | MM | DD | DD | DD | DD | DD | HH |
    * | 39 | 38 | 37 | 36 | 35 | 34 | 33 | 32 |
    * | HH | HH | HH | HH | mm | mm | mm | mm |
    * | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 |
    * | mm | mm | SS | SS | SS | SS | SS | SS |
    * | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
    * | uS | uS | uS | uS | uS | uS | uS | uS |
    * | 15 | 14 | 13 | 12 | 11 | 10 | 09 | 08 |
    * | uS | uS | uS | uS | uS | uS | uS | uS |
    * | 07 | 06 | 05 | 04 | 03 | 02 | 01 | 00 |
    * | uS | uS | uS | uS | TT | TT | TT | TT |
    *
    * Where:
    *
    *  YY == Year      (1 - 9999)
    *  MM == Month     (1 - 12)
    *  DD == Days      (1 - 31)
    *  HH == Hours     (0 - 59)
    *  mm == Minutes   (0 - 59)
    *  SS == Seconds   (0 - 61) to account for leap seconds
    *  uS == Microseconds (0 - 999,999)
    *  TT == Type      (1-4 currently, may change)
    */
   static const BITS_FOR_YEAR= 14;
   static const BITS_FOR_MONTH= 4;
   static const BITS_FOR_DAYS= 5;
   static const BITS_FOR_HOURS= 5;
   static const BITS_FOR_MINUTES= 6;
   static const BITS_FOR_SECONDS= 6;
   static const BITS_FOR_USECONDS= 20;
   static const BITS_FOR_TYPE= 4;

   uint64_t tmp_from = from
   enum drizzle_temporal_type tmp_type= tmp_from && (2**BITS_FOR_TYPE -1);
   tmp_from= tmp_from >> BITS_FOR_TYPE
   uint32_t tmp_useconds= tmp_from & (2**BITS_FOR_USECONDS -1);
   tmp_from= tmp_from >> BITS_FOR_USECONDS
   uint32_t tmp_seconds= tmp_from & (2**BITS_FOR_SECONDS -1);
   tmp_from= tmp_from >> BITS_FOR_SECONDS
   uint32_t tmp_minutes= tmp_from & (2**BITS_FOR_MINUTES -1);
   tmp_from= tmp_from >> BITS_FOR_MINUTES
   uint32_t tmp_hours= tmp_from & (2**BITS_FOR_HOURS -1);
   tmp_from= tmp_from >> BITS_FOR_HOURS
   uint32_t tmp_days= tmp_from & (2**BITS_FOR_DAYS -1);
   tmp_from= tmp_from >> BITS_FOR_DAYS
   uint32_t tmp_month= tmp_from & (2**BITS_FOR_MONTH -1);
   tmp_from= tmp_from >> BITS_FOR_MONTH
uint32_t tmp_year= tmp_from & (2**BITS_FOR_YEAR -1);
   if (tmp_type != DRIZZLE_TEMPORAL_DATETIME)
     return false;

   /* Construct the DRIZZLE_TEMPORAL */
   to->years= tmp_years;
   to->months= tmp_months;
   to->days= tmp_days;
   to->hours= tmp_hours;
   to->minutes= tmp_minutes;
   to->seconds= tmp_seconds;
   to->useconds= tmp_useconds;

if (tmp_years >= DRIZZLE_MIN_YEARS_EPOCH && tmp_years <= DRIZZLE_MAX_YEARS_EPOCH)
   {
     /* Add timestamp fields for timestamp-like values */
     time_t tmp_epoch_seconds;

     bool res= temporal_to_time_t(to, &tmp_epoch_seconds);
     if (res)
     {
       to->epoch_seconds= tmp_epoch_seconds;
     }
   }
   return true;
}


Il mercoledì 31 dicembre 2008 17:46:41 Jay Pipes ha scritto:
Brian Aker wrote:
Hi!

On Dec 29, 2008, at 6:39 AM, Jay Pipes wrote:
a) The DATETIME data type should now be able to support MICROSECOND
precision in the same 8-byte storage requirements.  I've added
routines for packing and unpacking 64-bit integers with all the needed
parts of the date, including microseconds.
What method are you using?
I created the following two methods for DATETIME:

They work with a new structure similar to the current DRIZZLE_TIME
struct called DRIZZLE_TEMPORAL:

/**
  * Structure which is used to represent temporal values inside Drizzle.
  *
  * This struct represents temporal data in both the Drizzle date system
and * the UNIX Epoch system.  Therefore, it handles:
  *
  * - years in the range 0000-9999
  * - months in the range 1-12
  * - days in the range 1-31
  * - hours in the range 0-59
  * - minutes in the range 0-59
  * - seconds in the range 0-61 to account for leap seconds
  * - seconds since Epoch
  * - microseconds in the range 0 - 999,999
  * - nanoseconds in the range 0 - 999,999,999
  */
typedef struct st_drizzle_temporal
{
   enum enum_drizzle_temporal_type type;
   int years;
   int months;
   int days;
   int hours;
   int minutes;
   int seconds;
   time_t epoch_seconds;
   uint32_t useconds;
   uint32_t nseconds;
} DRIZZLE_TEMPORAL;

/**
  * Helper function to pack a DRIZZLE_TEMPORAL_DATETIME into
  * a uint64_t
  *
  * Returns a bool of whether the packing was
  * done correctly.
  *
  * @param source to pack from
  * @param destination uint64_t to pack into
  */
static bool pack_datetime_to_uint64_t(const DRIZZLE_TEMPORAL *from,
uint64_t *to)
{
   /*
    * This data type is packed as follows:
    *
    * bits:
    *
    * | 63 | 62 | 61 | 60 | 59 | 58 | 57 | 56 |
    * | YY | YY | YY | YY | YY | YY | YY | YY |
    * | 55 | 54 | 53 | 52 | 51 | 50 | 49 | 48 |
    * | YY | YY | YY | YY | YY | YY | MM | MM |
    * | 47 | 46 | 45 | 44 | 43 | 42 | 41 | 40 |
    * | MM | MM | DD | DD | DD | DD | DD | HH |
    * | 39 | 38 | 37 | 36 | 35 | 34 | 33 | 32 |
    * | HH | HH | HH | HH | mm | mm | mm | mm |
    * | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 |
    * | mm | mm | SS | SS | SS | SS | SS | SS |
    * | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
    * | uS | uS | uS | uS | uS | uS | uS | uS |
    * | 15 | 14 | 13 | 12 | 11 | 10 | 09 | 08 |
    * | uS | uS | uS | uS | uS | uS | uS | uS |
    * | 07 | 06 | 05 | 04 | 03 | 02 | 01 | 00 |
    * | uS | uS | uS | uS | TT | TT | TT | TT |
    *
    * Where:
    *
    *  YY == Year      (1 - 9999)
    *  MM == Month     (1 - 12)
    *  DD == Days      (1 - 31)
    *  HH == Hours     (0 - 59)
    *  mm == Minutes   (0 - 59)
    *  SS == Seconds   (0 - 61) to account for leap seconds
    *  uS == Microseconds (0 - 999,999)
    *  TT == Type      (1-4 currently, may change)
    */

   if (from->type != DRIZZLE_TEMPORAL_DATETIME)
     return false;

   uint64_t packed_int= from->type
                       + (from->useconds << 4)
                       + (from->seconds << 24)
                       + (from->minutes << 30)
                       + (from->hours << 36)
                       + (from->days << 41)
                       + (from->months << 46)
                       + (from->years << 50);

   *to= packed_int;
   return true;
}

/**
  * Helper function to unpack a uint64_t into
  * a DRIZZLE_TEMPORAL of type DRIZZLE_TEMPORAL_DATETIME.
  *
  * Returns a bool of whether the unpacking was
  * done correctly.
  *
  * @param source uint64_t to unpack
  * @param destination to unpack into
  */
static bool unpack_uint64_t_to_datetime(const uint64_t *from,
DRIZZLE_TEMPORAL *to)
{
   /*
    * This data type is packed as follows:
    *
    * bits:
    *
    * | 63 | 62 | 61 | 60 | 59 | 58 | 57 | 56 |
    * | YY | YY | YY | YY | YY | YY | YY | YY |
    * | 55 | 54 | 53 | 52 | 51 | 50 | 49 | 48 |
    * | YY | YY | YY | YY | YY | YY | MM | MM |
    * | 47 | 46 | 45 | 44 | 43 | 42 | 41 | 40 |
    * | MM | MM | DD | DD | DD | DD | DD | HH |
    * | 39 | 38 | 37 | 36 | 35 | 34 | 33 | 32 |
    * | HH | HH | HH | HH | mm | mm | mm | mm |
    * | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 |
    * | mm | mm | SS | SS | SS | SS | SS | SS |
    * | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
    * | uS | uS | uS | uS | uS | uS | uS | uS |
    * | 15 | 14 | 13 | 12 | 11 | 10 | 09 | 08 |
    * | uS | uS | uS | uS | uS | uS | uS | uS |
    * | 07 | 06 | 05 | 04 | 03 | 02 | 01 | 00 |
    * | uS | uS | uS | uS | TT | TT | TT | TT |
    *
    * Where:
    *
    *  YY == Year      (1 - 9999)
    *  MM == Month     (1 - 12)
    *  DD == Days      (1 - 31)
    *  HH == Hours     (0 - 59)
    *  mm == Minutes   (0 - 59)
    *  SS == Seconds   (0 - 61) to account for leap seconds
    *  uS == Microseconds (0 - 999,999)
    *  TT == Type      (1-4 currently, may change)
    */
   static const BITS_FOR_YEAR= 14;
   static const BITS_FOR_MONTH= 4;
   static const BITS_FOR_DAYS= 5;
   static const BITS_FOR_HOURS= 5;
   static const BITS_FOR_MINUTES= 6;
   static const BITS_FOR_SECONDS= 6;
   static const BITS_FOR_USECONDS= 20;
   static const BITS_FOR_TYPE= 4;

   uint32_t tmp_years= from >> 50;
   uint32_t tmp_months= (from >> 46) - ((from >> 50) *
(2**BITS_FOR_MONTH)); uint32_t tmp_days= (from >> 41) - ((from >> 46) *
(2**BITS_FOR_DAYS)); uint32_t tmp_hours= (from >> 36) - ((from >> 41) *
(2**BITS_FOR_HOURS)); uint32_t tmp_minutes= (from >> 30) - ((from >> 36) *
(2**BITS_FOR_MINUTES));
   uint32_t tmp_seconds= (from >> 24) - ((from >> 30) *
(2**BITS_FOR_SECONDS));
   uint32_t tmp_useconds= (from >> 4) - ((from >> 24) *
(2**BITS_FOR_USECONDS));
   enum drizzle_temporal_type tmp_type= from - ((from >> 4) *
(2**BITS_FOR_TYPE));

   if (tmp_type != DRIZZLE_TEMPORAL_DATETIME)
     return false;

   /* Construct the DRIZZLE_TEMPORAL */
   to->years= tmp_years;
   to->months= tmp_months;
   to->days= tmp_days;
   to->hours= tmp_hours;
   to->minutes= tmp_minutes;
   to->seconds= tmp_seconds;
   to->useconds= tmp_useconds;

   if (tmp_years >= DRIZZLE_MIN_YEARS_EPOCH && tmp_years <=
DRIZZLE_MAX_YEARS_EPOCH)
   {
     /* Add timestamp fields for timestamp-like values */
     time_t tmp_epoch_seconds;

     bool res= temporal_to_time_t(to, &tmp_epoch_seconds);
     if (res)
     {
       to->epoch_seconds= tmp_epoch_seconds;
     }
   }
   return true;
}

The above routines would, of course, be used to pack and unpack into the
native integer format used by Drizzle and MySQL, which is
cross-architecture compatible for storage via the drizzled/korr.h macros.

NOTE: The code above is untested!  It is a piece of a stand-alone
library that can be used to translate to and from DRIZZLE_TEMPORAL and
standard time types like time_t, timeval, and timespec, and other
standard types like uint64_t.  In addition, the str_to_time and
str_to_datetime functions from mysys/my_time.cc have been dramatically
simplified and code-paths shortened.

I'll be done with the small unit testing piece today and will have
cleaned up any errors by then.

b) I have the workings of a new data type, called TIMESTAMP_HIRES (or
whatever, I don't really care about the name), which is an 8 byte
column type which stores seconds from Epoch along with a nanosecond
precision (basically, it's a storable struct timespec.)
Is it compatible with gettimeofday?
Completely.

Does anyone know why Field_timestamp and Field_datetime inherit from
Field_str?  I've seen no indication in the comments as to why this is
the case... I'd like to get some explanation as to why this is the
case before I proceed.
Just for a couple of bits of class inheritance. At one point in time I
had a 16byte INT class for instance that I used for UUID and IPV6. It
was there just to keep me from copy/paste.
OK, good to know.  I assume you're not opposed to the idea of me
scrapping the Field_str inheritance for DATE, DATETIME, and TIMESTAMP?
With only UTF8 character sets, I don't see any need to inherit from
Field_str.

Cheers,

-jay

_______________________________________________
Mailing list: https://launchpad.net/~drizzle-discuss
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~drizzle-discuss
More help   : https://help.launchpad.net/ListHelp



_______________________________________________
Mailing list: https://launchpad.net/~drizzle-discuss
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~drizzle-discuss
More help   : https://help.launchpad.net/ListHelp

Reply via email to