【问题标题】:R conversion of week number (UK) to POSIXct issueR将周数(英国)转换为POSIXct问题
【发布时间】:2013-02-05 18:48:35
【问题描述】:

我有包含英国周数的字符串(strptime 文档中的 %W),我可以将包含相同的字符串转换为 POSIXct

# create dummy data in June
x1 <- as.POSIXct('2012-06-01 01:00', format='%Y-%m-%d %H:%M', tz='UT')
(x2 <- format(x1, '%Y %W %a %H %M'))
[1] "2012 22 Fri 01 00"
as.POSIXct(x2, format='%Y %W %a %H %M', tz='UT')
[1] "2012-06-01 01:00:00"

所以这很好用......但是,如果我想要 2012 年 1 月的第一天,它就行不通 - 我只是得到一个 NA

x1 <- as.POSIXct('2012-01-01 01:00', format='%Y-%m-%d %H:%M', tz='UT')
(x2 <- format(x1, '%Y %W %a %H %M'))
[1] "2012 00 Sun 01 00"
as.POSIXct(x2, format='%Y %W %a %H %M', tz='UT')
[1] NA

如何解决这个问题,以便将年初的这些日期转换为 POSIXct?

更新

下面的 C 代码为我在 libc 2.15 (ubuntu 12.04 LTS) 上的机器演示了真正的问题是底层的 libc

#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
  int
main (void)
{
  struct tm tm;
  char buf[255];

  memset (&tm, 0, sizeof (struct tm));
  // ok what week number is the 1st of January 2012?
  // according to this it is week 01 in %U format 
  // and 00 in %W format...
  strptime ("2012-01-01 01:00", "%Y-%m-$d %H:%M", &tm);
  strftime (buf, sizeof (buf), "%Y-%m-%d %U %W %a %b %H:%M", &tm);
  puts("To demonstrate the different week numbers in %U %W");
  puts("Using format %Y-%m-%d %U %W %a %b %H:%M");
  puts (buf);
  // to demonstrate it works
  strptime ("2012 02 Sun 01 00", "%Y %W %a %H %M", &tm);
  strftime (buf, sizeof (buf), "%Y-%m-%d %a %b %H %M", &tm);
  puts("\nUsing format %Y-%m-%d %a %b %H%M for 2012 02 Sun 01 00");
  puts (buf);
  // but then the potential bug...
  strptime ("2012 01 Sun 01 00", "%Y %W %a %H %M", &tm);
  strftime (buf, sizeof (buf), "%Y-%m-%d %a %b %H %M", &tm);
  puts("\nUsing format %Y-%m-%d %a %b %H%M for 2012 01 Sun 01 00");
  puts("and this is wrong...");
  puts (buf);
  strptime ("2012 00 Sun 01 00", "%Y %W %a %H %M", &tm);
  strftime (buf, sizeof (buf), "%Y-%m-%d %a %b %H %M", &tm);
  puts("\nUsing format %Y-%m-%d %a %b %H %M for 2012 00 Sun 01 00");
  puts("and this is VERY wrong...");
  puts (buf);
  exit (EXIT_SUCCESS);
}

它给出以下输出

To demonstrate the different week numbers in %U %W
Using format %Y-%m-%d %U %W %a %b %H:%M
2012-01-00 01 00 Sun Jan 00:00

Using format %Y-%m-%d %a %b %H%M for 2012 02 Sun 01 00
2012-01-08 Sun Jan 01 00

Using format %Y-%m-%d %a %b %H%M for 2012 01 Sun 01 00
and this is wrong...
2012-01-01 Sun Jan 01 00

Using format %Y-%m-%d %a %b %H %M for 2012 00 Sun 01 00
and this is VERY wrong...
2012-00--371 Sun Saturday 01 00

【问题讨论】:

    标签: r posixct


    【解决方案1】:

    看起来像 00 造成了问题。

    x3 <- gsub(' 00 ' , ' 01 ',x2)             ## dirty workaround 00 -> 01
    > as.POSIXct(x3, format='%Y %W %a %H %M')
    [1] "2012-01-01 01:00:00 CET"
    > x1
    [1] "2012-01-01 01:00:00 CET"
    

    编辑

    使用 %U 代替 %W 我明白了:

    x1 <- as.POSIXct('2012-01-01 01:00', format='%Y-%m-%d %H:%M')
    > (x2 <- format(x1, '%Y %U %a %H %M'))
    [1] "2012 01 dim. 01 00"
    > as.POSIXct(x2, format='%Y %U %a %H %M')
    [1] "2012-01-01 01:00:00 CET"
    
    x1 <- as.POSIXct('2012-01-08 01:00', format='%Y-%m-%d %H:%M')
    > (x2 <- format(x1, '%Y %U %a %H %M'))
    [1] "2012 02 dim. 01 00"
    > as.POSIXct(x2, format='%Y %U %a %H %M')
    [1] "2012-01-08 01:00:00 CET"
    

    【讨论】:

    • 这是部分解决问题 - 但 '2012-01-08 01:00' 变为 "2012 01 Sun 01 00" - 转换回 "2012-01-01 01:00" 为你已经指出了。
    • 恐怕这无济于事,因为数据是用 %W 编写的——我认为这是 strptime() 的底层 GNU 例程中的一个错误——gnu.org/software/libc/manual/html_node/… 的文档说 % W 没有“完全实现”!!!
    • 事实证明我的 ubuntu 12.04 在 libc 版本 2.15 上,最新的稳定版本在 2.17 (2012-12-25) - 我无法更新到最前沿检查一下。
    • bugs.r-project.org/bugzilla3/show_bug.cgi?id=15195 R 的“错误”报告,我要求提供文档以指出底层 libc 的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    相关资源
    最近更新 更多