UTC time handling using strptime() - problem


Topic author
garyrevell
Active Contributor
Posts: 38
Joined: Thu Nov 19, 2020 7:15 am
Reputation: 0
Location: Basingstoke, UK
Status: Offline
Contact:

UTC time handling using strptime() - problem

Post by garyrevell » Fri Apr 08, 2022 3:52 am

Morning all,

We've got a requirement to process UTC format dates from an external system e.g. 2022-04-02T11:23:05Z and want to use the strptime() function using the format mask "%FT%T%z".

I found a small example program on the net here https://ioncannon.net/programming/33/us ... imestamps/ and modified it for my own testing.

However, I can get it to compile & run OK on Windows WSL2 Debian but NOT on OpenVMS V8.4-2L1. I've raised a ticket with VSI but wanted to see if anyone else could see the problem, or have suggestions as to how to do what we want to do?

The example program is here:

Code: Select all

#include <string.h>
#include <stdio.h>
#include <time.h>

static const int date_buff_size = 64;

void convert_iso8601(const char *time_string, int ts_len, struct tm *tm_data)
{
  char* ptr = NULL;
  int iptr = 0;
  char temp[64];
  struct tm ctime;

  memset(temp, 0, sizeof(temp));
  tzset();

  strncpy(temp, time_string, ts_len);

  memset(&ctime, 0, sizeof(struct tm));
  iptr = strptime(temp, "%FT%T%z", &ctime);

  const unsigned long ts = mktime(&ctime) - timezone;
  localtime_r(&ts, tm_data);
}

int main()
{
/*  char date[] = "2006-03-28T16:49:29.000Z";   */
  char date[date_buff_size];
  struct tm tm;

  while (1)
  {
    printf("Enter UTC date (e.g. 2006-03-26T12:34:56.000Z): ");
    scanf("%s",date );

    memset(&tm, 0, sizeof(struct tm));
    convert_iso8601(date, sizeof(date), &tm);

    char buf[date_buff_size];
    strftime(buf, sizeof(buf), "Date: %a, %d %b %Y %H:%M:%S %Z", &tm);
    printf("Convert from %s\n", date );
    printf("Result is    %s\n", buf);

  }
}
Output on Debian is as follows:

Code: Select all

garyr@BD10:~/dev/C$ ./utcTest
Enter UTC date (e.g. 2006-03-26T12:34:56.000Z): 2006-03-26T12:34:56.000Z
Convert from 2006-03-26T12:34:56.000Z
Result is    Date: Sun, 26 Mar 2006 13:34:56 BST
Enter UTC date (e.g. 2006-03-26T12:34:56.000Z): 2006-03-26T12:34:56.000Z
Convert from 2006-03-26T12:34:56.000Z
Result is    Date: Sun, 26 Mar 2006 13:34:56 BST
Enter UTC date (e.g. 2006-03-26T12:34:56.000Z): 2010-11-25T00:02:33.000Z
Convert from 2010-11-25T00:02:33.000Z
Result is    Date: Thu, 25 Nov 2010 00:02:33 GMT
Enter UTC date (e.g. 2006-03-26T12:34:56.000Z): 2022-03-27T00:30:00.000Z
Convert from 2022-03-27T00:30:00.000Z
Result is    Date: Sun, 27 Mar 2022 00:30:00 GMT
Enter UTC date (e.g. 2006-03-26T12:34:56.000Z): 2022-03-27T01:01:00.000Z
Convert from 2022-03-27T01:01:00.000Z
Result is    Date: Sun, 27 Mar 2022 02:01:00 BST
Enter UTC date (e.g. 2006-03-26T12:34:56.000Z): 2021-05-03T12:00:20Z
Convert from 2021-05-03T12:00:20Z
Result is    Date: Mon, 03 May 2021 13:00:20 BST
Enter UTC date (e.g. 2006-03-26T12:34:56.000Z): 2019-12-25T10:11:44Z
Convert from 2019-12-25T10:11:44Z
Result is    Date: Wed, 25 Dec 2019 10:11:44 GMT
Enter UTC date (e.g. 2006-03-26T12:34:56.000Z): 2019-09-30T15:00:27Z
Convert from 2019-09-30T15:00:27Z
Result is    Date: Mon, 30 Sep 2019 16:00:27 BST
Enter UTC date (e.g. 2006-03-26T12:34:56.000Z): ^C
garyr@BD10:~/dev/C$
Output on OpenVMS is as follows:

Code: Select all

(LAB6) $ run UTCTEST/nodeb
Enter UTC date (e.g. 2006-03-26T12:34:56.000Z): 2006-03-26T12:34:56.000Z
Convert from 2006-03-26T12:34:56.000Z
Result is    Date: Sun, 07 Feb 2106 06:28:15 GMT
Enter UTC date (e.g. 2006-03-26T12:34:56.000Z): 2021-12-23T11:00:07.003Z
Convert from 2021-12-23T11:00:07.003Z
Result is    Date: Sun, 07 Feb 2106 06:28:15 GMT
Enter UTC date (e.g. 2006-03-26T12:34:56.000Z): 2022-04-08T08:57:43Z
Convert from 2022-04-08T08:57:43Z
Result is    Date: Sun, 07 Feb 2106 06:28:15 GMT
Enter UTC date (e.g. 2006-03-26T12:34:56.000Z): 2015-10-31T22:17:09.000Z
Convert from 2015-10-31T22:17:09.000Z
Result is    Date: Sun, 07 Feb 2106 06:28:15 GMT
Enter UTC date (e.g. 2006-03-26T12:34:56.000Z):
 Interrupt

(LAB6) $

At least the result coming back is consistent..... Sun, 07 Feb 2106 06:28:15 GMT ?!

Thanks in advance

Gary


Topic author
garyrevell
Active Contributor
Posts: 38
Joined: Thu Nov 19, 2020 7:15 am
Reputation: 0
Location: Basingstoke, UK
Status: Offline
Contact:

Re: UTC time handling using strptime() - problem

Post by garyrevell » Tue Apr 12, 2022 4:33 am

Thanks sms, I'll have a look at the examples on antinode

Post Reply