0

我有一个下面的方法,它将接受oldTimestamp作为输入参数uint64_t......现在我需要在上面应用一个公式,oldTimestamp每当我应用该公式时,我总是得到0作为start值。

而且我尝试以current timestamp相同的方法以毫秒为单位获取long int数据类型(不确定这是否是正确的数据类型),并且每当我再次在 上应用相同的公式时currentTimestamp,我也总是得到end价值0......

以下是我的方法 -

void getRecord(uint64_t oldTimestamp) {

    int start = (oldTimestamp / (60 * 60 * 1000 * 24))  % 14;

    cout<<"START: " << start <<endl; // this always print out as 0..?

    struct timeval tp;
    gettimeofday(&tp, NULL);

    long int currentTimestamp = tp.tv_sec * 1000 + tp.tv_usec / 1000; //get current timestamp in milliseconds

    int end = (currentTimestamp / (60 * 60 * 1000 * 24))  % 14;

    cout<<"END: " << end <<endl; // this always print out as 0 as well..?

}

我在这里做错了什么吗?

可能是,我为 currentTimestamp 和 oldTimestamp 使用了错误的数据类型,我不应该使用intfor startand end

更新:-

这样的事情会好吗?

void getRecord(uint64_t oldTimestamp) {

    uint64_t start = (oldTimestamp / (60 * 60 * 1000 * 24))  % 14;

    cout<<"START: " << start <<endl;

    struct timeval tp;
    gettimeofday(&tp, NULL);

    uint64_t currentTimestamp = tp.tv_sec * 1000 + tp.tv_usec / 1000; //get current timestamp in milliseconds

    uint64_t end = (currentTimestamp / (60 * 60 * 1000 * 24))  % 14;

    cout<<"END: " << end <<endl;

}
4

2 回答 2

1
void getRecord(uint64_t oldTimestamp) { // you are using uint64_t here

int start = (oldTimestamp / (60 * 60 * 1000 * 24))  % 14; // you are using int here

您可能会遇到溢出问题,这会调用未定义的行为。

您需要uint64_t用作您的start类型。你的currentTimestampend变量也是如此。

于 2013-10-24T20:33:53.430 回答
0

问题是您使用 int 作为开始和结束。

于 2013-10-24T20:31:30.147 回答