views:

40

answers:

1

What is the best way to format a transaction id?

Conditions

Max 15 characters: XXXXXXXXXXXXXXX

-All transaction must be unique

-Can contain both numerical and alphabetical characters

-May contain Year, month, day

+2  A: 

Are you talking about formatting it or creating it? If the latter, one solution I've used in the past is the venerable:

YYMMDDhhmmssnnn

where YY..ss is the obvious and nnn is simply a number that increments every time you get a transaction ID. That way, as long as you don't ask for more than a thousand IDs per second, you'll have your uniqueness and the IDs will only roll over every century.

Of course, if you really wanted a little more compaction, you could use the fact that many of those two-digit fields are a waste. Set up a character array able to encode some of the fields into a single characterfrom the set:

abcdefghijklmnopqrstuvwxyz (values  0-25)
ABCDEFGHIJKLMNOPQRSTUVWXYZ (       26-51)
0123456789                 (       52-61)

(62 characters) and use it to encode the MM, DD, hh, mm and ss into a single character.

For example, April being month 4 could be coded as e. Forty-five minutes past the hour would have the minute field encoded as T.

That saves 5 characters, add in the century CC to stop it from rolling over for another 8,000 years or so, and you can then handle a million IDs per second, ten million if you encode CC the same way although you'll end up with a Y6K problem :-)

So right now (2010-02-25/20:22:15), you would get, assuming the nnnnnn is currently 0):

2010 c z u w p 000000
  |  | | | | |    |
  |  | | | | |    +- sequence number within second
  |  | | | | +------ p    =   15 (second)
  |  | | | +-------- w    =   22 (minute)
  |  | | +---------- u    =   20 (hour)
  |  | +------------ z    =   25 (day)
  |  +-------------- c    =    2 (month)
  +----------------- 2010 = 2010 (year)
paxdiablo
I love your logic, paxdiablo :)thanks
K001