Domanda

Ho un database SQL Server e sto tirando date da esso e convertendo il tipo di timestamp_t in int64 in quanto tale:

Int64 from_timestamp_t(dtl::timestamp_t& t)
{
    // create a new posix time structure
    boost::posix_time::ptime pt
    (
    boost::gregorian::date           ( t.year, t.month, t.day),
    boost::posix_time::time_duration ( t.hour, t.minute, t.second, t.fraction )
    );

    ptime epoch(date(1970, Jan, 1));
    boost::posix_time::time_duration fromEpoch = pt - epoch;

    // return it to caller
    return fromEpoch.total_milliseconds();
}

Cerco di convertire di nuovo in una spinta ptime da un INT64 in quanto tale:

ptime from_epoch_ticks(Int64 ticksFromEpoch)
{
    ptime epoch(date(1970, Jan, 1), time_duration(0,0,0));
    ptime time = epoch + boost::posix_time::milliseconds(ticksFromEpoch);

    return time;
}

Per qualche motivo, e non riesco a capire perché, le mie date, le ore, ecc. Sono tutte corrette, ma i miei minuti sono avanti a pochi minuti da quello che dovrebbero essere. È perché i timestamp del database sono in una risoluzione dei secondi e sto usando millisecondi? Come lo risolvo?

Applicare la seguente modifica come suggerito da Dan sembra aver risolto il problema:

Int64 from_timestamp_t(dtl::timestamp_t& t)
{
    int count = t.fraction * (time_duration::ticks_per_second() % 1000);

    boost::posix_time::ptime pt
        (
        boost::gregorian::date           ( t.year, t.month, t.day ),
        boost::posix_time::time_duration ( t.hour, t.minute, t.second, count )
        );

    ptime epoch(date(1970, Jan, 1), time_duration(0, 0, 0, 0));

    boost::posix_time::time_duration fromEpoch = pt - epoch;

    return fromEpoch.total_milliseconds();
}

Nessuna soluzione corretta

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top