views:

787

answers:

2

Hi,

I'm trying to build an interface between iTunes e MediaMonkeys. When I imported my tracks from iTunes to MM, the field LastPlayed wasn't considered.

So I decided to build an interface that reads the value from iTunes and updates the MM database.

I'm using the package from phxsoftware in order to access the SQLite database used by MM.

The database field is a REAL data type, which is mapped as DbType.Single. When I do the update, I'm converting a DateTime object (provided by IITTrack) to Single, using Convert.ToSingle(DateTime).

But I'm receiving an error, telling that Invalid cast from 'DateTime' to 'Single'.

Any hints about this?

TIA,

Bob

+2  A: 

DateTime has a Ticks property of type long, so you can convert it easily to Single

Convert.ToSingle(DateTime.Ticks);
Jhonny D. Cano -Leftware-
Though he will probably need to scale and offset it, depending on what units the SQLite REAL field is measuring, and what the baseline is (e.g. 1 Jan 0001, 1 Jan 1970, etc.).
itowlson
IIRC, SQLite actually doesn't distinguish between int and float, it just has 'Number' - so whatever mapped the field probably just erred on the side of caution and made it a float.
Blorgbeard
You are wrong. From SQLite docs:# INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.# REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.
Mash
I stand corrected.
Blorgbeard
+4  A: 
  1. SQLite is not using Single (single precision 32-bit) it's actually using doubles (64 bit).
  2. According to http://www.mediamonkey.com/wiki/index.php/ISDBSongData::LastPlayed you need to do following:

    LastPlayed.Subtract(new DateTime(1899, 12, 30, 0, 0, 0, DateTimeKind.Utc)).TotalDays;

It's actually better to move that constant date to some static readonly value...

P.S. Comment that SQLite doesn't differs integers and floats is WRONG - it has only 2 numeric types - 8-bytes floats and 8-bytes integers.

Mash
I don't know... it seems like this solution just save the Date (days), not the time
Jhonny D. Cano -Leftware-
Just a minor correction: LastPlayed.Subtract(new DateTime(1899, 12, 30, 0, 0, 0, DateTimeKind.Utc))
Bob Rivers
That's not true - it saves number of days as floating point value and because of that it actually contains time as well.
Mash
Thanks for correction, that's true.
Mash