首页 \ 问答 \ 使用Nodatime排除期间的日期(Excluding dates from period using Nodatime)

使用Nodatime排除期间的日期(Excluding dates from period using Nodatime)

我正在尝试研究两个LocalDateTime值之间的时间量并排除特定日期(在本例中,它是银行假日)。

var bankHolidays = new[] { new LocalDate(2013, 12, 25), new LocalDate(2013, 12, 26) };
var localDateTime1 = new LocalDateTime(2013, 11, 18, 10, 30);
var localDateTime2 = new LocalDateTime(2013, 12, 29, 10, 15);

var differenceBetween = Period.Between(localDateTime1, localDateTime2, PeriodUnits.Days | PeriodUnits.HourMinuteSecond);

differenceBetween值显示两个日期之间的天数/小时/分钟/秒,正如您所期望的那样。

我可以从开始日期检查每一天,看看bankHolidays集合是否包含该日期,例如

var bankHolidays = new[] { new LocalDate(2013, 12, 25), new LocalDate(2013, 12, 26) };
var localDateTime1 = new LocalDateTime(2013, 11, 18, 10, 30);
var localDateTime2 = new LocalDateTime(2013, 12, 29, 10, 15);

var differenceBetween = Period.Between(localDateTime1, localDateTime2, PeriodUnits.Days | PeriodUnits.HourMinuteSecond);

var london = DateTimeZoneProviders.Tzdb["Europe/London"];

for (var i = 1; i < differenceBetween.Days; ++i)
{
    var x = localDateTime1.InZoneStrictly(london) + Duration.FromStandardDays(i);

    if (bankHolidays.Any(date => date == x.Date))
    {
        //subtract one day for the period.
    }
}

我觉得我错过了一些明显的,应该有一个更简单的方法,是否有更简单的方法来找到两个日期之间的时间段,但不包括某些日期?

我也需要在这个例外中包括周末,显而易见的方法似乎是在检查银行假期时检查周末的一周,这似乎不是处理它的最佳/正确方法。


I'm trying to workout the amount of time between two LocalDateTime values and exclude specific dates (in this example, it's bank holidays).

var bankHolidays = new[] { new LocalDate(2013, 12, 25), new LocalDate(2013, 12, 26) };
var localDateTime1 = new LocalDateTime(2013, 11, 18, 10, 30);
var localDateTime2 = new LocalDateTime(2013, 12, 29, 10, 15);

var differenceBetween = Period.Between(localDateTime1, localDateTime2, PeriodUnits.Days | PeriodUnits.HourMinuteSecond);

The differenceBetween value shows the number of days/hours/minutes/seconds between the two dates, as you would expect.

I could check every single day from the start date and see if the bankHolidays collection contains that date e.g.

var bankHolidays = new[] { new LocalDate(2013, 12, 25), new LocalDate(2013, 12, 26) };
var localDateTime1 = new LocalDateTime(2013, 11, 18, 10, 30);
var localDateTime2 = new LocalDateTime(2013, 12, 29, 10, 15);

var differenceBetween = Period.Between(localDateTime1, localDateTime2, PeriodUnits.Days | PeriodUnits.HourMinuteSecond);

var london = DateTimeZoneProviders.Tzdb["Europe/London"];

for (var i = 1; i < differenceBetween.Days; ++i)
{
    var x = localDateTime1.InZoneStrictly(london) + Duration.FromStandardDays(i);

    if (bankHolidays.Any(date => date == x.Date))
    {
        //subtract one day for the period.
    }
}

I feel like I'm missing some obvious and there should be an easier method, is there a simpler way to find a period between two dates whilst excluding certain dates?

I also need to include weekends in this exclusion too, the obvious way seems to be to check the day of the week for weekends whilst checking bank holidays, this just doesn't seem like the best/correct way of handling it though.


原文:https://stackoverflow.com/questions/20045730
更新时间:2022-06-25 16:06

最满意答案

看起来这个错误实际上是在两个版本中。 格式代码H查找十六进制数字,正如在PHP错误中指出的那样,它没有找到(合法的)数字。 罪魁祸首似乎是这样的表达:

chr(16 - ($len % 16))

Perl的版本并不抱怨,因为Perl的版本的包会转换字符,不管它是否是十六进制数字(这可能不是你想要的)。 文档更详细地介绍了实际发生的情况。

为防止出现错误,请尝试以下操作:

sprintf('%x', 16 - ($len % 16))

注意:尽管这样可以解决您遇到的错误,但我不知道它是否可接受的解决方案,因为我不知道Perl代码的原始作者的确切意图。


It looks like the error is actually in both versions. The format code H looks for a hex digit, and as noted in the PHP error, it isn't finding (a legal) one. The culprit appears to be this expression:

chr(16 - ($len % 16))

The Perl version isn't complaining because Perl's version of pack will convert the character regardless of whether or not it is a hex digit (which may not be what you want). The documentation goes into more detail about what actually happens.

To prevent the error, try this instead:

sprintf('%x', 16 - ($len % 16))

Note: While this should fix the error you are getting, I don't know if it's an acceptable solution because I don't know the exact intent of the original author of the Perl code.

相关问答

更多
  • 在.NET中,结果不是字符串,而是字节数组。 字符串由16位char值组成,因此不便于表示8位数据。 使用这样的方法来获取整数的“网络”表示: public static byte[] ToNetwork(int value) { byte[] data = BitConverter.GetBytes(value); if (BitConverter.IsLittleEndian) { Array.Reverse(data); } return data; } 将字节数组写入内存流 ...
  • 我的Perl非常生疏,但在看了man perlfunc我想这可能是你想要的: List items = ...; ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream stream = new DataOutputStream(baos); stream.write(items.size()); for (Integer item : items) { stream.writeInt(i ...
  • 看起来这个错误实际上是在两个版本中。 格式代码H查找十六进制数字,正如在PHP错误中指出的那样,它没有找到(合法的)数字。 罪魁祸首似乎是这样的表达: chr(16 - ($len % 16)) Perl的版本并不抱怨,因为Perl的版本的包会转换字符,不管它是否是十六进制数字(这可能不是你想要的)。 文档更详细地介绍了实际发生的情况。 为防止出现错误,请尝试以下操作: sprintf('%x', 16 - ($len % 16)) 注意:尽管这样可以解决您遇到的错误,但我不知道它是否可接受的解决方案, ...
  • 'l>'选项适用于我(注意,虽然没有调用hex )。 此外, N作为模板工作: perl -e 'print pack $_, 0x12345678 for qw( l> N )' | xxd 0000000: 1234 5678 1234 5678 The 'l>' option works for me (note there's no call to hex, though). Also, N as the template works: perl -e 'print pack $_, 0x1234 ...
  • HTML :: TreeBuilder和朋友(例如HTML :: TreeBuilder :: XPath)。 ::如果你需要速度, LibXML很好。 HTML::TreeBuilder and friends (such as HTML::TreeBuilder::XPath). ::LibXML is good if you need speed.
  • 为了支持Perl的包装定义,我实现了定义的预处理。 To support perl like pack definitions I implement preprocessing of the definition.
  • 你可能是Const::Fast模块。 use Const::Fast; const my $foo => 'a scalar value'; print "X${foo}X\n"; You may you Const::Fast module. use Const::Fast; const my $foo => 'a scalar value'; print "X${foo}X\n";
  • 您可以打开“打包”警告类别并使其致命。 然后溢出会导致可能被捕获的异常。 例如: for my $val (127, 128) { print "$val -> "; if (eval { use warnings FATAL => qw(pack); pack("c", $val); }) { print "no overflow"; } else { print "overflow ($@)"; } ...
  • pack ("N*", $ip)从$ ip中取出一个整数并将其置于网络字节顺序中。 你想要的是将IP地址的4个十进制八位字节打包为二进制。 无需使用字节顺序,因为IP地址字符串已经是大端(最高位字节已经在字符串的开头)。 我还将*改为4 ,IP地址总是4个八位字节长: $ip = "192.168.1.1"; $bi = pack "C4", split('\.', $ip); print join('.', unpack("C4",$bi)), "\n"; pack ("N*", $ip) ta ...
  • 如果你没有为它提供第二个参数, unpack()默认使用$_ ; 另一方面, pack()没有 。 unpack() uses $_ by default if you do not provide it with a second parameter; pack() on the other hand, does not.

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)