【问题标题】:DateTime compare issues with InflateColumn::DateTimeDateTime 与 InflateColumn::DateTime 比较问题
【发布时间】:2014-08-20 03:27:27
【问题描述】:

我无法比较 Catalylst 中的 DateTime 对象。我有一个 end_date 列,它被DBIx::Class::InflateColumn::DateTime 夸大了,我正在用我的时区夸大它:

__PACKAGE__->add_columns(
    end_date => { data_type => 'datetime', time_zone => 'America/Chicago' },
);

我有一个函数应该告诉我我的事件是否已关闭,这是在我的 Schema 中为此类定义的:

sub closed { 
    my ($self) = @_; 
    my $now = DateTime->now(time_zone => 'America/Chicago');
    warn DateTime->compare($now, $self->end_date);
    warn $now;
    warn $self->end_date;
    return DateTime->compare($now, $self->end_date) == 1;
}

但是,它无法正常工作。它告诉我事件在实际结束之前已经结束。这是警告的示例输出:

1
2014-06-29T12:20:48
2014-06-29T12:20:50

如您所见,第一个日期大于 end_date,即使不是。我一直无法弄清楚这是为什么。但是,每当我转换它们并创建新的 DateTime 对象时:

sub closed { 
    my ($self) = @_; 
    my $now = DateTime::Format::ISO8601->parse_datetime(DateTime->now(time_zone => 'America/Chicago'));
    my $end_date = DateTime::Format::ISO8601->parse_datetime($self->end_date);
    return DateTime->compare($now, $end_date) == 1;
}

然后他们正确比较,比较返回-1。有谁知道为什么会这样?

【问题讨论】:

    标签: perl datetime catalyst dbix-class


    【解决方案1】:

    您的调试信息没有用,因为您没有包含时区偏移量(例如,通过使用 ->strftime('%FT%T%z'))。如果你这样做了,我敢打赌你会发现第一个日期确实比结束日期大,而且我敢打赌它使用 UTC 作为你的膨胀列。

    查看文档,时区将由timezone 属性提供,但您使用了time_zone

    { data_type => 'datetime', timezone => "America/Chicago", locale => "de_DE" }
    

    (对于 D::C::IC::DT 来说,这是一个糟糕而令人困惑的选择。)

    【讨论】:

    • 谢谢。你是对的,它没有被夸大到同一个时区。因此,当我将其保存到数据库中时,我的数据库设置将确定2014-06-29 12:20:48 等字符串的时区,对吧?因为现在当它被转换时,它实际上被转换为我当前时区的错误时间。
    猜你喜欢
    • 1970-01-01
    • 2014-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2011-01-11
    • 2016-11-01
    • 2011-09-30
    相关资源
    最近更新 更多