R conversion of week number (UK) to POSIXct issue

ぐ巨炮叔叔 提交于 2019-12-10 11:47:26

问题


I have strings containing UK week numbers (%W in strptime docs) and I can convert a string containing same into 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"

So this works fine... However if I want the first of January 2012 it doesn't work - I just get an 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

How do I fix this so that I can convert these dates at the start of the year to POSIXct ?

UPDATE

The following C code demonstrates for my machine on libc 2.15 (ubuntu 12.04 LTS) that the real problem is the underlying 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);
}

it gives the following output

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

回答1:


It looks like the 00 which create the problem.

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"

EDIT

using %U in place of %W I get this :

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"


来源:https://stackoverflow.com/questions/14646406/r-conversion-of-week-number-uk-to-posixct-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!