Python: C # binary coding datetime

I need to extract financial price data from a binary file. This pricing data is usually extracted with a piece of C # code. The biggest problem I am facing is getting significant time and time.

Binary data is as follows:

'\x14\x11\x00\x00{\x14\xaeG\xe1z(@\x9a\x99\x99\x99\x99\x99(@q=\n\xd7\xa3p(@\x9a\x99\x99\x99\x99\x99(@\xac\x00\x19\x00\x00\x00\x00\x00\x08\x01\x00\x00\x00"\xd8\x18\xe0\xdc\xcc\x08'

C # code that retrieves it correctly:

StockID = reader.ReadInt32();
Open = reader.ReadDouble();
High = reader.ReadDouble();
Low = reader.ReadDouble();
Close = reader.ReadDouble();
Volume = reader.ReadInt64();
TotalTrades = reader.ReadInt32();
Timestamp = reader.ReadDateTime();

That's where I got the python. I have a couple of problems.

In [1]: barlength = 56; barformat = 'i4dqiq'
In [2]: pricebar = f.read(barlength)
In [3]: pricebar
Out[3]: '\x95L\x00\x00)\\\x8f\xc2\xf5\xc8N@D\x1c\xeb\xe26\xcaN@\x7fj\xbct\x93\xb0N@\xd7\xa3p=\n\xb7N@\xf6\xdb\x02\x00\x00\x00\x00\x00J\x03\x00\x00\x00"\xd8\x18\xe0\xdc\xcc\x08'
In [4]: struct.unpack(barformat, pricebar)
Out[4]: 
(19605,                # stock id
 61.57,                # open
 61.579800000000006,   # high
 61.3795,              # low
 61.43,                # close
 187382,               # volume -- seems reasonable
 842,                  # TotalTrades -- seems reasonable
 634124502600000000L   # datetime -- no idea what this means!
)

I used python built in to the struct module , but it has some problems.

  • I'm not sure which format characters match Int32 vs Int64 in C # code, although several different attempts returned the same python tuple.

  • , , , , : , TotalTrades , unsigned int OR long (l, L, I)

  • . .

+3
2

, .net - 0001-01-01T00: 00: 00Z, 100 . :

>>> x = 634124502600000000
>>> secs = x / 10.0 ** 7
>>> secs
63412450260.0
>>> import datetime
>>> delta = datetime.timedelta(seconds=secs)
>>> delta
datetime.timedelta(733940, 34260)
>>> ts = datetime.datetime(1,1,1) + delta
>>> ts
datetime.datetime(2010, 6, 18, 9, 31)
>>>

- 2010-06-18. , 9,5 UTC? , .

"" , , , , : , TotalTrades , , unsigned int OR, unsigned long (l, L, I) "": , (1) "long" "int" (32 ) (2 ) , , . , 8- 0 127 , , .

+2

#, ReadInt32, ReadDouble, ReadDateTime .., , ...

  • , i l, , i/l Int32 q Int64.

  • , i/l i/l, 32- , 0 2147483647 . , TotalTrades 2147483647, . , .

  • , , , DateTime.Ticks.

    , 100 , 00:00:00 1 January 0001.

    , , - 634124502600000000, 09:31:00 18 June 2010.

0

Source: https://habr.com/ru/post/1752922/


All Articles