【问题标题】:which libc time gives me the time of day?哪个libc时间给了我一天中的时间?
【发布时间】:2021-04-01 21:43:56
【问题描述】:

Fedora 33 乐

我想用 NativeCall 与 lic.so.6 谈谈我正在写的一个例子。我找到了

但我不知道哪一个只会给我一天中的时间 (21:02:03)

挠头,

【问题讨论】:

  • 添加了标签。我认为这实际上不仅仅与 Raku 有关。
  • 几乎所有 C 函数都返回一个 tm 结构,其中包括完整日期和时间的字段(有时这些字段被忽略,但从技术上讲它们总是存在)。获取后需要单独格式化。如果您想将时间传递给 NativeCall 子程序,您可能需要在应用了 is repr('CStruct') 特征的 Raku 类中镜像 tm 结构。
  • 几乎所有的 C time 函数,我的意思是

标签: c glibc raku


【解决方案1】:

要让 libc.so.6 获取本地时间,您首先必须从 time 获取原始系统时间,然后输入 localtime 原始系统时间以获取您的小时、分钟等。

例如:

#!/usr/bin/env raku
use NativeCall;


class tm is repr('CStruct') { 
   has int32   $.tm_sec;              # Seconds.    [0-60] (1 leap second)
   has int32   $.tm_min;            # Minutes.  [0-59] 
   has int32   $.tm_hour;       # Hours.    [0-23]
   has int32   $.tm_mday;       # Day.      [1-31]
   has int32   $.tm_mon;            # Month.    [0-11]
   has int32   $.tm_year;       # Year  - 1900.   Add 1900 to this to get a four digit year
   has int32   $.tm_wday;       # Day of week.  [0-6]
   has int32   $.tm_yday;         # Days in year.[0-365]
   has int32   $.tm_isdst;    # DST.        [-1/0/1]
   has int64   $.tm_gmtoff;     # Seconds east of UTC.
   has byte    $.tm_zone;    # Timezone abbreviation.
   
   # note: the mysterious "time_t" structure is a "long, long int" or int64
  
     
   # /usr/lib64/libc.so.6    `'c',v6` converts to "libc.so.6"
   # C: time (&rawtime);     /* "&rawtime" is the address of "rawtime" */
   sub  time( int64 is rw ) is native( 'c', v6 ) { * };    # "is rw" makes it a pointer
   
   # C: timeinfo = localtime (&rawtime);   /* "&rawtime" is the address of "rawtime" */
   sub  localtime( int64 is rw ) returns tm is native( 'c', v6 ) { * };  # "is rw" makes it a pointer
   

   method GetRawTime( --> int64 ) {
      my int64 $rawtime;
      time( $rawtime );
      return $rawtime;
   }
          
   method GetLocalTime() returns tm  {
      # my Str $SubName = &?ROUTINE.name;
      my $time = tm.new;
      my int64 $rawtime = 0;
      
      $rawtime = tm.GetRawTime();      
      $time = localtime( $rawtime );
      return $time;
   }    
   
}

my int64 $RawTime = 0;
$RawTime = tm.GetRawTime();
print "raw time = <" ~ $RawTime ~ ">\n";

my $SysTime = tm.new;
$SysTime    = tm.GetLocalTime();
print "Local Time HH:MM:SS <" ~ $SysTime.tm_hour ~ ":" ~ 
      $SysTime.tm_min ~ ":" ~ $SysTime.tm_sec ~ ">\n";
print "year = <" ~ $SysTime.tm_year + 1900 ~ ">\n";

示例输出:

$ LinSysTime.pl6
raw time = <1608970172>
Local Time HH:MM:SS <0:9:32>
year = <2020>

【讨论】:

    【解决方案2】:

    原则上我不应该试图写一个答案。[1]但是有些人说不耐烦和/或傲慢可能是一种美德......

    给我一​​天中的时间(21:02:03)

    您链接的页面仅列出了介绍性文档中所说的“与人们通常认为...时间的方式无关”的功能。[3]


    我查看了 Håkon 链接的模块,以及它使用的模块:P5localtime。它包括这一行:

    my sub get-ctime(int64 is rw --> Str) is native is symbol<ctime> {*}
    

    这将调用 C 函数 ctime,该函数记录在您链接的网站的 Formatting Calendar Time 页面上。

    我的猜测是你想调用ctime_r 函数或类似函数。


    我记得最近阅读了你和了解此类事情的人(ugexe?)之间的交流,关于如何具体选择 glibc.so.6 而不是其他版本,所以我会跳过这个皱纹。

    脚注

    [1] 很高兴为您提供一天中的时间,尽管它可能不是您想要的时间。我对相关成分几乎一无所知[2]。我回答你的问题的唯一借口是,我有时有足够的时间以及那种傲慢和不耐烦来回答关于我一无所知的主题的问题,这不是拉里的美德。

    [2] 我不记得使用 C 来编写任何生产代码。我不知道 libc 时间函数。我只使用 NativeCall 做一些琐碎的事情,比如我记得在阅读 this blog post 时所做的一些“hello world”测试。

    [3] 您正在查看的页面 -- Getting the time -- 对您来说可能听起来很有希望,但如果您在网站你会来到一个标题为Time Basics 的页面,它强烈建议我使用Formatting Calendar Time 页面或Broken-down Time 页面中的函数。[4]

    [4] 虽然我确实有疑问。首先,我不知道我在说什么。[2] 其次,每个函数都有这样的注释,令人不安:

    AS-不安全的堆锁

    您可以自行阅读解释这些术语的 POSIX Safety Concepts 文档,以便您进行相应调整。

    【讨论】:

    • 我特意从glibc中抽出时间来补和如何使用面向对象的类来表示结构的例子。就想要时间而言,raku 拥有 OUT THE NOSE 的时间功能。目标就是榜样。我确实在我原来的问题中说过
    • 我特别需要使用 time.h (libc.so.6) 中的函数。我需要知道艰难的道路。我认为是 /usr/include/bits/types/time_t.h /* 如果 TIMER 不为 NULL,则返回当前时间并将其放入 *TIMER。 */ extern time_t time (time_t *__timer) __THROW;
    • "示例如何使用面向对象的类来表示结构。"那你为什么不说呢? “目标就是例子。我在最初的问题中确实说明了这一点”但是您没有提供有关目标的详细信息,因此读者必须猜测。这不适合 SO 问题。更一般地说,这是the XY problem 的经典示例。在 SO 上首次询问 Q 时,避免 XY 问题,这项工作很重要。
    • “我特别需要使用 time.h (libc.so.6) 中的函数。我需要知道困难的方法。”那你为什么不从一开始就在你的 Q 中,而不是等到我浪费时间回答你,在对我的回答的评论中提及这个至关重要的细节?我怎么知道你知道还有其他更合适的函数(也在time.h),它们是使用 Raku 和 NativeCall 的好例子,但你故意选择忽略那些更简单的函数,而支持一种更难的做事方式,当你没有这么说的时候?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 2016-06-04
    • 2016-08-19
    相关资源
    最近更新 更多