如何在 DBM 数据库中存储时间戳?

发布于 2024-07-17 05:54:29 字数 764 浏览 4 评论 0原文

我正在实现简单的文件共享服务。 目前我使用基于文件的数据库(因为它适合我的需要)。

   # somewhere in my cgi script
   sub first_run
    {
      my $dbh = DBI->connect('dbi:DBM:');
      $dbh = DBI->connect("dbi:DBM:", "", "",{ AutoCommit => 1, RaiseError => 1, f_dir=>$DATABASE_DIR} );
      unless ($dbh)
      {
        print "<br>Cannot connect: $DBI::errstr";
        return undef;
      }
      $dbh->do("CREATE TABLE uploads( user_name TEXT,
        file_name TEXT, 
        upload_date TEXT ) ");
      $dbh->disconnect( );
    }

正如您所看到的,我建议将上传时间戳存储为字符串,因为目前我只需要显示它(假设使用 localtime() 以人类可读格式获取时间戳)。 但这在我看来有些不好。 如果稍后我想要显示某个时期的上传内容等该怎么办?

在 DBM 数据库中存储时间戳而不使用第三方 CPAN 模块的常见方法是什么? 我怎样才能稍后拉出它们并将它们显示给用户(在我的情况下,转换回字符串)?

I am implemening simple file-sharing service. Currently I use a file-based database (as it suits my needs).

   # somewhere in my cgi script
   sub first_run
    {
      my $dbh = DBI->connect('dbi:DBM:');
      $dbh = DBI->connect("dbi:DBM:", "", "",{ AutoCommit => 1, RaiseError => 1, f_dir=>$DATABASE_DIR} );
      unless ($dbh)
      {
        print "<br>Cannot connect: $DBI::errstr";
        return undef;
      }
      $dbh->do("CREATE TABLE uploads( user_name TEXT,
        file_name TEXT, 
        upload_date TEXT ) ");
      $dbh->disconnect( );
    }

As you can see I propose to store the upload timestamp as a string, since currently I only have to display it (suppose to use localtime() to get timestamp in a human readable format).
But this seems to me somewhat bad. What if later I'll want to show uploads from some period, etc.

What is the common way of storing timestamps in a DBM database without using third-party, CPAN modules? How can I pull them later and show them to the user (in my situation, convert back to string)?

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

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

发布评论

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

评论(2

吻安 2024-07-24 05:54:30

请考虑存储 UTC 时间比存储本地时间更明确。 如果您在伦敦的计算机上保存时间并将文件发送给我在香港,当我在您的程序中加载该文件时,我会看到您实际保存的时间吗?

考虑到在存储 UTC 时间时,您(至少)有两个选择:格式化的时间字符串,或自特定时间(“纪元”)以来的(秒/毫秒/其他)计数。 据推测,这两种格式与另一种格式是同构的,因为存在从一种格式到另一种格式的明确定义的双向转换。

Consider that storing UTC time is less ambiguous than storing local time. If you save a time at your computer in London and send me the file in Hong Kong, will I when I load the file in your program see the time you actually saved?

Consider that in storing UTC time you have (at least) two choices: the formatted time string, or a count of numbers of (seconds/milliseconds/whatever) since a particular time ("the epoch"). Presumably either format is isomorphic with the other, as there is a well-defined bi-directional conversion rom one to the other.

穿越时光隧道 2024-07-24 05:54:30

我只存储纪元,然后很容易用 *localtime* 或 *strftime* 显示它。

如果您确实希望能够浏览数据库的内容而不必转换回纪元,则可以同时存储数值和 *localtime* 字符串 (" `1238693517 - Thu Apr 2 19:31:57 2009` ”)。 这使得数据变得冗余和非规范化,但也使其易于检查,同时仍然能够处理数值。 你的来电。

I would just store the epoch, it's easy to then display it with *localtime* or *strftime*.

If you really want to be able to browse the content of the DB without having to convert back the epoch, you could store both the numerical value and the *localtime* string (" `1238693517 - Thu Apr 2 19:31:57 2009`"). This makes the data redundant and denormalized, but also makes it easy to inspect, while still being able to process the numerical value. Your call.

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