使用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
最满意答案
看起来这个错误实际上是在两个版本中。 格式代码
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)) 注意:尽管这样可以解决您遇到的错误,但我不知道它是否可接受的解决方案, ...
-
Perl包整数(Perl pack integer)[2022-07-25]
'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.