Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9224767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:24:27+00:00 2026-06-18T04:24:27+00:00

I have strings containing UK week numbers (%W in strptime docs) and I can

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:24:28+00:00Added an answer on June 18, 2026 at 4:24 am

    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"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okey, I have two strings containing numbers like shown here using debugger: String 1
I have two vectors containing strings. I want to compare each string of vector1
I have a list of strings containing numbers and I cannot find a good
I have strings containing numbers with their units, e.g. 2GB, 17ft, etc. I would
I have two strings containing time, like this: 'ftime1' => string '17:44' (length=5) 'ftime2'
I have two strings containing letters and numbers separated by spaces. ex String1elza7ma wa2fa
I have two equal length strings containing 1's and 0's. Each string is 128-bits
i have multiple strings containing a link like: <A HREF=http://www.testings2>testings2</A> <A HREF=http://www.blabla>blabla</A> <A HREF=http://www.gowick>gowick</A>
I have a set of strings containing characters in a PHP script, I need
I have a lot of strings containing custom tags: hello {RT|12} world {RT|465} this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.