AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 77888362
Accepted
con
con
Asked: 2024-01-27 02:34:26 +0800 CST2024-01-27 02:34:26 +0800 CST 2024-01-27 02:34:26 +0800 CST

Perl 的日期时间未返回正确的时间差异

  • 772

受https://perlmaven.com/datetime的启发,我试图找到 2024-01-03T19:00:00 和 2024-01-07T16:00:00 (93 小时)之间的差异小时数。

#!/usr/bin/env perl

use 5.038;
use warnings FATAL => 'all';
use autodie ':default';
use DDP;
use Devel::Confess 'color';
use DateTime;

sub str_to_date ($date) {
    if ($date =~ m/^(\d+) # year
    \-(\d{1,2})           # month
    \-(\d{1,2})           # day
    T
    (\d+)                        # hour
    :
    (\d+)                        # minutes
    :
    (\d+)                        # seconds                  
    /x) {
        return DateTime -> new(
        year  => $1,
        month => $2,
        day   => $3,
        hour    => $4,
        minute=> $5,
        second=> $6,
    );
    } else {
        die "$date failed regex.";
    }
} # later dates are "greater"
my $wed_date = str_to_date('2024-01-03T19:00:00');
say $wed_date->day;
my $sun_date = str_to_date('2024-01-07T16:00:00');
my $diff = $sun_date - $wed_date; # returns a duration object

p $diff;
say $diff->clock_duration->in_units('hours'); # 21, but should be 93
say $diff->in_units('hours'); # 21 again
say $diff->hours; # 21 again
say $sun_date->delta_days($wed_date)->in_units('hours'); # 0
p $diff->deltas;

我从https://metacpan.org/pod/DateTime::Format::Duration找到的所有方法都给出了错误的答案。

或者,如果我删除解析,

#!/usr/bin/env perl
use 5.038;
use warnings FATAL => 'all';
use autodie ':default';
use DDP;
use Devel::Confess 'color';
use DateTime;

my $wed_date = DateTime->new(
    year => 2024,
    month=>1,
    day=>3,
    hour=>19
);#str_to_date('2024-01-03T19:00:00');
my $sun_date = DateTime->new(
    year=>2024,
    month=>1,
    day=>7,
    hour=>16#my $sun_date = str_to_date('2024-01-07T16:00:00');
);#
say $wed_date->day;

my $diff = $sun_date - $wed_date;

p $diff;
say $diff->clock_duration->in_units('hours'); # 21, but should be 93
say $diff->in_units('hours'); # 21 again
say $diff->hours; # 21 again
say $sun_date->delta_days($wed_date)->in_units('hours'); # 0
p $diff->deltas; # 10

答案是相同的。

DateTime::Duration 对象似乎忽略了中间的天数,而只关注小时之间的差异。

如何获得两个日期之间正确的小时数?

datetime
  • 2 2 个回答
  • 57 Views

2 个回答

  • Voted
  1. con
    2024-01-27T03:09:09+08:002024-01-27T03:09:09+08:00

    答案,根据https://metacpan.org/pod/DateTime#How-DateTime-Math-Works

    $sun_date->subtract_datetime_absolute($wed_date)->seconds;
    

    这是一个 DateTime::Duration 对象:

    DateTime::Duration  {
        public methods (34): add, add_duration, calendar_duration, clock_duration, clone, compare, days, delta_days, delta_minutes, delta_months, delta_nanoseconds, delta_seconds, deltas, end_of_month_mode, hours, in_units, inverse, is_limit_mode, is_negative, is_positive, is_preserve_mode, is_wrap_mode, is_zero, MAX_NANOSECONDS, minutes, months, multiply, nanoseconds, new, seconds, subtract, subtract_duration, weeks, years
        private methods (8): _add_overload, _compare_overload, _duration_object_from_args, _has_negative, _has_positive, _multiply_overload, _normalize_nanoseconds, _subtract_overload
        overloads: *, +, -, <=>, cmp
        internals: {
            days           0,
            end_of_month   "wrap",
            minutes        0,
            months         0,
            nanoseconds    0,
            seconds        334800
        }
    }
    

    秒数 334800 等于 93 小时,这是正确答案。

    感谢评论者。

    • 1
  2. Best Answer
    ikegami
    2024-01-27T04:53:58+08:002024-01-27T04:53:58+08:00

    使用->delta_ms而不是->subtract_datetime(通过减法调用的方法)。


    以天为单位的持续时间无法转换为小时,因为并非所有天都有相同的小时数。事实上,只有这些转换是安全的:

    • 周 ⇔ 天
    • 小时 ⇔ 分钟
    • 秒 ⇔ 纳秒

    因此,->in_units( 'hours' )仅将持续时间的小时和分钟部分转换为小时。

    $sun_date - $wed_date返回 3 天 1260 分钟的持续时间。
    ->in_units( 'hours' )返回 int( 0 + 1260/60 ) = 21 小时。

    解决方案是创建由小时和/或分钟而不是天组成的持续时间。

    $sun_date->delta_ms( $wed_date )返回 5580 分钟的持续时间。
    ->in_units( 'hours' )返回 int( 0 + 5580/60 ) = 93 小时。

    • 1

相关问题

  • 如何将 SAS 中的字符时间戳转换为有效的日期时间值

  • 为 systemd-timesyncd 配置 NTP 服务器和端口 [关闭]

  • dataweave 转换 DateTime:无法从 TemporalAccessor 获取 ZonedDateTime

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行?

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    何时应使用 std::inplace_vector 而不是 std::vector?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Martin Hope
    Aleksandr Dubinsky 为什么 InetAddress 上的 switch 模式匹配会失败,并出现“未涵盖所有可能的输入值”? 2024-12-23 06:56:21 +0800 CST
  • Martin Hope
    Phillip Borge 为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行? 2024-12-12 20:46:46 +0800 CST
  • Martin Hope
    Oodini 具有指定基础类型但没有枚举器的“枚举类”的用途是什么? 2024-12-12 06:27:11 +0800 CST
  • Martin Hope
    sleeptightAnsiC `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它? 2024-11-09 07:18:53 +0800 CST
  • Martin Hope
    The Mad Gamer 何时应使用 std::inplace_vector 而不是 std::vector? 2024-10-29 23:01:00 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST
  • Martin Hope
    MarkB 为什么 GCC 生成有条件执行 SIMD 实现的代码? 2024-02-17 06:17:14 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve