如何使用 +- 10 秒缓冲区获取日期时间?

发布于 2024-10-23 11:41:40 字数 203 浏览 3 评论 0原文

我有:

jobElement.CreationDate = jobElement.CreationDate + TimeSpan.FromHours(24.0);

我想要的不是严格的 24 小时,而是 +- 10 秒的缓冲区。像 23.59.10 和 00.00.10 一样,

用 c# 可以达到这个目的吗?

i have:

jobElement.CreationDate = jobElement.CreationDate + TimeSpan.FromHours(24.0);

i would like to have not strictly 24 hours, but with +- 10 seconds Buffer. like 23.59.10 and 00.00.10

hot to reach that with c#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

江湖彼岸 2024-10-30 11:41:40

这将以相同的概率生成 CreationDate + 23:50 和 CreationDate + 24:10:

Random random = new Random(); 
TimeSpan buffer = TimeSpan.FromSeconds(10);

TimeSpan span = TimeSpan.FromHours(24.0);

// 50% of the time do this
if(random.Next() % 2 == 0)
{
    span += buffer;
}
// The rest of the time do this
else
{
    span -= buffer;
}

jobElement.CreationDate = jobElement.CreationDate + span;

This will generate CreationDate + 23:50 and CreationDate + 24:10 with equal probability:

Random random = new Random(); 
TimeSpan buffer = TimeSpan.FromSeconds(10);

TimeSpan span = TimeSpan.FromHours(24.0);

// 50% of the time do this
if(random.Next() % 2 == 0)
{
    span += buffer;
}
// The rest of the time do this
else
{
    span -= buffer;
}

jobElement.CreationDate = jobElement.CreationDate + span;
皓月长歌 2024-10-30 11:41:40

你需要做什么?

如果您需要进行任何比较,请创建带有覆盖相等运算符的自定义类。

What do you need to do with that?

If you need to any comparison create custom class with overwritten equality operators.

柠檬色的秋千 2024-10-30 11:41:40

我不是 100% 确定你想要什么,但我会尝试一下。

DateTime dt1 = DateTime.Now;
DateTime dt2 = DateTime.Now.AddDays(1).AddSeconds(8);

这两个时间现在相隔 24 小时零 8 秒。

然后,如果您想查看它们是否“几乎”24 小时开放,您可以执行以下操作:

if( Math.Abs((dt1-dt2.AddDays(-1))) < 10 ){
  //dt2 is 24 after dt1 +- 10 seconds 
}else{
  //they are not
}

I'm not 100% sure what you want here but I'll give it a shot

DateTime dt1 = DateTime.Now;
DateTime dt2 = DateTime.Now.AddDays(1).AddSeconds(8);

These two are now 24 hours and 8 seconds apart.

Then if you want to see if they are "almost" 24 hour appart, you can do something like this:

if( Math.Abs((dt1-dt2.AddDays(-1))) < 10 ){
  //dt2 is 24 after dt1 +- 10 seconds 
}else{
  //they are not
}
白色秋天 2024-10-30 11:41:40

当前日期的第一次 (00.00.00) -/+ 10 秒将是:

DateTime dateFrom = jobElement.CreationDate.Date.AddSeconds(-10);
DateTime dateTo = jobElement.CreationDate.Date.AddSeconds(10);

是这样吗?

First time (00.00.00) of current date -/+ 10 secs would be:

DateTime dateFrom = jobElement.CreationDate.Date.AddSeconds(-10);
DateTime dateTo = jobElement.CreationDate.Date.AddSeconds(10);

Is that it?

抚笙 2024-10-30 11:41:40

我将添加这个变体。它与其他的不同,因为它不是“基于秒”而是基于“刻度”(刻度是 TimeSpan/DateTime 可以计算的最小时间)。

const int sec = 10; // +/- seconds of the "buffer"
const int ticksSec = 10000000; // There are 10000000 Ticks in a second

Random r = new Random();
int rng = r.Next(-sec * ticksSec, sec * ticksSec + 1); // r.Next is upper-bound exclusive
var ts = TimeSpan.FromHours(24) + TimeSpan.FromTicks(rng);
jobElement.CreationDate = jobElement.CreationDate + ts;

Random 类中存在限制(它可以不会生成一个 long,并且生成一个“约束”long(maxValue = x 的 long)仅基于 Random 类是不平凡的,因此这将工作长达 3 分钟和一些“缓冲”(214 秒)更准确地说)。

I'll add this variant. It's different from others because it isn't "second based" but "tick" based (the tick is the smallest time that a TimeSpan/DateTime can compute)

const int sec = 10; // +/- seconds of the "buffer"
const int ticksSec = 10000000; // There are 10000000 Ticks in a second

Random r = new Random();
int rng = r.Next(-sec * ticksSec, sec * ticksSec + 1); // r.Next is upper-bound exclusive
var ts = TimeSpan.FromHours(24) + TimeSpan.FromTicks(rng);
jobElement.CreationDate = jobElement.CreationDate + ts;

There are limits in the Random class (it can't generate a long, and generating a "constrained" long (a long with maxValue = x) is non-trivial based only on the Random class, so this will work for up to 3 minutes and something of "buffer" (214 seconds to be more exact).

ヤ经典坏疍 2024-10-30 11:41:40

如果您想要 +/- 10 之间的所有数字

Random r = new Random();
int x = r.Next(-10, 11);
var ts =  TimeSpan.FromHours(24).Add(TimeSpan.FromSeconds((double)x));

jobElement.CreationDate = jobElement.CreationDate + ts;

If you want +/- 10 with all numbers between

Random r = new Random();
int x = r.Next(-10, 11);
var ts =  TimeSpan.FromHours(24).Add(TimeSpan.FromSeconds((double)x));

jobElement.CreationDate = jobElement.CreationDate + ts;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文