Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我们可以从同一类型的范围中获取'std::chrono::millizes'变量吗?_C++_Chrono - Fatal编程技术网

C++ 我们可以从同一类型的范围中获取'std::chrono::millizes'变量吗?

C++ 我们可以从同一类型的范围中获取'std::chrono::millizes'变量吗?,c++,chrono,C++,Chrono,我构造了一个函数,该函数以特定日期作为输入,并以std::chrono::millides格式返回该日期 milliseconds lowerRangeBound = TimeStamp(mm, dd, HH, MM, SS, yyyy); 比如说, milliseconds a = TimeStamp(8/*month*/, 23/*day*/, 14/*hours*/, 46/*minutes*/, 32/*seconds*/, 2017/*year*/); 以转换后的字符串格式返回:2

我构造了一个函数,该函数以特定日期作为输入,并以
std::chrono::millides
格式返回该日期

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 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*/);
预期输出是一个
毫秒c
,它的字符串格式是这样的日期,
2017.09.13-12.56.12
。请注意,所需的输出为
毫秒
,提供字符串格式是为了在中发言

到目前为止,我尝试的是将每个
毫秒
变量转换为
数字(
.count()
),并获得
范围
[a,b]
。但是,输出日期是不相关的:
1977.12.06-16.27.02

你能帮个忙吗

先谢谢你

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

毫秒时间戳(int-mm,int-dd,int-HH,int-mm,int-SS,int-yyy){
tm ttm=tm();
ttm.tm_year=yyyy-1900;//自1900年起的年份
ttm.tm_mon=mm-1;//自一月起的月份
ttm.tm_mday=dd;//月份的第几天[1-31]
ttm.tm_hour=HH;//一天中的小时[00-23]
ttm.tm_min=MM;
ttm.tm_sec=SS;
time\u t ttime\u t=mktime(&ttm);
系统时钟::时间点时间点结果=标准::时钟::系统时钟::从时间点开始(时间点);
毫秒现在\u ms=std::chrono::time\u point\u cast(time\u point\u result)。time\u自\u epoch()起;
现在就回来!;
}
毫秒时间戳(int mm_1,int dd_1,int HH_1,int mm_1,int SS_1,int yyy_1,
整数mm_2,整数dd_2,整数HH_2,整数mm_2,整数SS_2,整数yyy_2,整数N){
毫秒lowerRangeBound=固定时间戳(mm_1,dd_1,HH_1,mm_1,SS_1,yyyy_1);
毫秒上限=固定时间戳(mm_2,dd_2,HH_2,mm_2,SS_2,yyyy_2);
LONGLOWERRANGE_uz=LOWERRANGEBOND.count();
long upperRange_uz=upperRangeBound.count();
//长输出=rand()%(上限-下限)+下限;
//根据@Jarod42的建议,兰德()被替换。
std::默认随机引擎生成器;
标准:均匀分布(下限、上限);
长输出=配电(发电机);
std::chrono::持续时间dur(输出);
返回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;
}
#包括
#包括
#包括
#包括
样板
std::chrono::毫秒getRandomTimestamp(英文和英文),
std::chrono::毫秒低,
std::chrono::毫秒高)
{
//推断实际的整数类型。
//否则,我们将不得不在使用时猜测T是什么
//均匀分布
使用count_type=decltype(low.count());
//由此我们可以推断出正确的整数分布
使用dist_type=std::uniform_int_分布;
//创建分发生成器
auto dist=dist_类型(low.count(),high.count());
//并将其应用于随机生成器,返回
//毫秒介于低和高之间
返回std::chrono::毫秒(dist(eng));
}
int main()
{
使用名称空间std::literals;
std::随机_设备开发;
std::默认的随机引擎引擎(dev());
自动t1=10'000'000ms;
自动t2=11'000'000ms;
自动t3=获取随机时间戳(eng、t1、t2);

断言(t1这可能是因为混合考虑,你对数学感到困惑

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

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

我会写一些这样的例行公事:

#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;
}
#包括
#包括
#包括
#包括
样板
std::chrono::毫秒getRandomTimestamp(英文和英文),
std::chrono::毫秒低,
std::chrono::毫秒高)
{
//推断实际的整数类型。
//否则,我们将不得不在使用时猜测T是什么
//均匀分布
使用count_type=decltype(low.count());
//由此我们可以推断出正确的整数分布
使用dist_type=std::uniform_int_分布;
//创建分发生成器
auto dist=dist_类型(low.count(),high.count());
//并将其应用于随机生成器,返回
//毫秒介于低和高之间
返回std::chrono::毫秒(dist(eng));
}
int main()
{
使用名称空间std::literals;
std::随机_设备开发;
std::默认的随机引擎引擎(dev());
自动t1=10'000'000ms;
自动t2=11'000'000ms;
自动t3=获取随机时间戳(eng、t1、t2);

assert(t1如果你利用它,你可以使这变得更容易。语法简洁易读。例如,你可以用
date::sys\u time
创建任意精度的
chrono::time\u point
s(如
millisters

由于您的问题涉及的是
时间点
s,而不是
持续时间
s,因此从类型安全的角度来看,这是一种更好的方法。以下是编写
getRandomTimestamp
的一种方法:

#include "date/date.h"
#include <iostream>
#include <random>

date::sys_time<std::chrono::milliseconds>
getRandomTimestamp(date::sys_time<std::chrono::milliseconds> lowerBound,
                   date::sys_time<std::chrono::milliseconds> upperBound)
{
    static std::default_random_engine generator;
    using namespace std::chrono;
    std::uniform_int_distribution<milliseconds::rep> distribution{
        lowerBound.time_since_epoch().count(),
        upperBound.time_since_epoch().count()};
    return date::sys_time<milliseconds>{milliseconds{distribution(generator)}};
}
上面的
date::sys\u days
只是另一个
chrono::time\u点
,但精度为
days
,而不是
毫秒
(也可以拼写为
date::sys\u time
int
main()
{
    using namespace date::literals;
    using namespace std::chrono_literals;
    auto t1 = date::sys_days{2017_y/8/23} + 13h + 46min + 32s;
    auto t2 = date::sys_days{2017_y/10/23} + 13h + 46min + 32s;
    auto t3 = getRandomTimestamp(t1, t2);
    // ...
}
std::cout << date::format("%Y.%m.%d-%T", t3) << '\n';
2017.09.04-17:14:04.220
using namespace date;
auto t1 = sys_days{year{y}/m/d} + hours{h} + ...
sys_days sd = floor<days>(t3);
time_of_day<milliseconds> tod(t3-sd);
year_month_day ymd = sd;