c++ - 我们可以从相同类型的范围中获取 `std::chrono::milliseconds` 变量吗?

标签 c++ c++-chrono

我构建了一个函数,它将特定日期作为输入并以 std::chrono::milliseconds 格式返回该日期。

milliseconds lowerRangeBound = TimeStamp(mm, dd, HH, MM, SS, yyyy);

例如,

milliseconds a = TimeStamp(8/*month*/, 23/*day*/, 14/*hours*/, 46/*minutes*/, 32/*seconds*/, 2017/*year*/);

以转换后的字符串格式返回:2017.08.23-14.46.32

我现在想做但行不通的是给定两个日期 ( milliseconds ) 以在这两个日期定义的范围内取一个随机日期。例如,给定

milliseconds a = TimeStamp(8/*month*/, 23/*day*/, 13/*hours*/, 46/*minutes*/, 32/*seconds*/, 2017/*year*/);
milliseconds b = TimeStamp(10/*month*/, 23/*day*/, 13/*hours*/, 46/*minutes*/, 32/*seconds*/, 2017/*year*/);

预期的输出是一个 milliseconds c ,它在字符串格式中是一个像这样的日期 2017.09.13-12.56.12 。请注意,所需的输出是 milliseconds ,提供字符串格式是为了在 readable format 中说话。

到目前为止我尝试的是将每个 milliseconds 变量转换为 long 数字 ( .count() ) 并获得 long 范围内的 random [a,b] 。然而,输出日期是无关紧要的:1977.12.06-16.27.02

你能帮帮我吗?

提前谢谢你。

编辑:下面的代码是受此 link 的启发

milliseconds TimeStamp(int mm, int dd, int HH, int MM, int SS, int yyyy) {

    tm ttm = tm();
    ttm.tm_year = yyyy - 1900; // Year since 1900
    ttm.tm_mon = mm - 1; // Month since January 
    ttm.tm_mday = dd; // Day of the month [1-31]
    ttm.tm_hour = HH; // Hour of the day [00-23]
    ttm.tm_min = MM;
    ttm.tm_sec = SS;

    time_t ttime_t = mktime(&ttm);
    system_clock::time_point time_point_result = std::chrono::system_clock::from_time_t(ttime_t);
    milliseconds now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(time_point_result).time_since_epoch();
    return now_ms;
}

milliseconds getRandomTimestamp(int mm_1, int dd_1, int HH_1, int MM_1, int SS_1, int yyyy_1,
    int mm_2, int dd_2, int HH_2, int MM_2, int SS_2, int yyyy_2, int N) {

    milliseconds lowerRangeBound = fixedTimeStamp(mm_1, dd_1, HH_1, MM_1, SS_1, yyyy_1);
    milliseconds upperRangeBound = fixedTimeStamp(mm_2, dd_2, HH_2, MM_2, SS_2, yyyy_2);

    long lowerRange_ = lowerRangeBound.count();
    long upperRange_ = upperRangeBound.count();

    //long output = rand() % (upperRange_ - lowerRange_ + 1) + lowerRange_;
    // rand() replaced after @Jarod42's suggestion.
    std::default_random_engine generator;
    std::uniform_int_distribution<int> distribution(lowerRange_, upperRange_);
    long output = distribution(generator);        

    std::chrono::duration<long> dur(output);
    return dur;
}

最佳答案

可能是因为混合问题,您对数学感到困惑。

我建议您以毫秒为单位编写“随机时间戳”例程。

您可以在单独的步骤中执行从/到时间戳的转换。

我会这样写例程:

#include<random>
#include<chrono>
#include<cassert>
#include<iostream>

template<class Eng>
std::chrono::milliseconds getRandomTimestamp(Eng& eng, 
                                             std::chrono::milliseconds low, 
                                             std::chrono::milliseconds high) 
{
    // deduce the actual integer type. 
    // Otherwise we'll have to guess what T is when using
    // uniform_int_distribution<T>
    using count_type = decltype(low.count());

    // from this we can deduce the correct integer distribution
    using dist_type = std::uniform_int_distribution<count_type>;

    // create the distribution generator
    auto dist = dist_type(low.count(), high.count());

    // and apply it to the random generator, returning
    // milliseconds somewhere between low and high
    return std::chrono::milliseconds(dist(eng));    
}


int main()
{
    using namespace std::literals;

    std::random_device dev;
    std::default_random_engine eng(dev());

    auto t1 = 10'000'000ms;
    auto t2 = 11'000'000ms;
    auto t3 = getRandomTimestamp(eng, t1, t2);

    assert(t1 <= t3);
    assert(t3 <= t2);

    std::cout << t1.count() << std::endl;
    std::cout << t2.count() << std::endl;
    std::cout << t3.count() << std::endl;
}

关于c++ - 我们可以从相同类型的范围中获取 `std::chrono::milliseconds` 变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46825247/

相关文章:

c++ - 有趣的范围问题,解释?

c++ - 在 C++11 中, protected 意味着公共(public)?

c++ - 没有无限缓冲区的 Winsock WSAAsyncSelect 发送

c++ - std::chrono::clock,硬件时钟和周期计数

c++ - 将纳秒(从午夜开始)转换为可打印时间?

c++ - double 到 time 的转换

c++ - 如何在 double 中存储 chrono 的时间?

c++ - Chrono 程序中永远不会触发条件语句

c++ - 在无限循环中使用或不使用 join() 方法

c++ - 没有空构造函数编译失败