1. Binary date storage

These routines allow you to save dates using only
3-bytes.

bytes = date2bytes{{12, 31, 1997})
--bytes = {byte 1, byte 2, byte 3}

-----------------------------------------------
--date format is {month, day, year}
--supports years 0-45099
--I could be EASILY presuaded to allow for
--negative years. (B.C. years)
-----------------------------------------------
global function date2bytes(sequence dte)
  integer temp
  sequence bytes

  dte[1..2] = dte[1..2] - 1
  bytes = repeat(0, 3)
  dte[3] = dte[3] * 372
  dte[2] = dte[2] * 12
  temp = dte[1] + dte[2] + dte[3]

  bytes[1] = remainder(temp, 256)
  temp = temp - bytes[1]
  temp = temp / 256

  bytes[2] = remainder(temp, 256)
  temp = temp - bytes[2]
  temp = temp / 256

  bytes[3] = temp

  return bytes
end function
-----------------------------------------------
global function bytes2date(sequence bytes)
  integer temp
  sequence dte

  temp = bytes[3] * 65536
  temp = temp + bytes[2] * 256
  temp = temp + bytes[1]

  dte = repeat(0, 3)
  --Month
  dte[1] = remainder(temp, 12)
  temp = temp - dte[1]
  temp = temp / 12

  --Day
  dte[2] = remainder(temp, 31)
  temp = temp - dte[2]
  temp = temp / 31

  --Year
  dte[3] = temp

  dte[1..2] = dte[1..2] + 1

  return dte
end function
-----------------------------------------------

sequence dte, bytes, dte2

dte = {6, 25, 1997}

bytes = date2bytes(dte)
dte2 = bytes2date(bytes)

? dte
? dte2
? bytes

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu