Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UA_print() using UA_DateTime does not use time zone properly #6022

Open
3 of 7 tasks
dirk-zimoch opened this issue Sep 25, 2023 · 4 comments
Open
3 of 7 tasks

UA_print() using UA_DateTime does not use time zone properly #6022

dirk-zimoch opened this issue Sep 25, 2023 · 4 comments

Comments

@dirk-zimoch
Copy link

Description

When printing UA_DateTime with UA_Print(), it prints the time in UTC time zone but indicates the local time zone.
This is quite confusing. Usually, the indicated time zone (e.g. +0200) is calculated into the printed time, see for example the Linux date command.

Background Information / Reproduction Steps

Example code:

#include <stdlib.h>
#include "open62541/client.h"

int main() {
    system("date --iso=seconds");
    UA_String out = UA_STRING_NULL;
    UA_DateTime currentTime = UA_DateTime_now();
    UA_print(&currentTime, &UA_TYPES[UA_TYPES_DATETIME], &out);
    printf("%.*s\n", (int)out.length, out.data);
    return 0;
}

Running it prints for example:

2023-09-25T15:08:32+02:00
2023-09-25 13:08:32.237 (UTC+0200)

The first line (printed by date) indicates time zone +02:00 and includes it in the printed time. The printed time is local time.
The second line (printed by UA_print) indicates time zone UTC+0200 but does not include it in the printed time. The printed time is UTC, not UTC+0200 as indicated.

I suggest to modify printDateTime() as follows:

 static UA_StatusCode
 printDateTime(UA_PrintContext *ctx, const UA_DateTime *p, const UA_DataType *_) {
     UA_Int64 tOffset = UA_DateTime_localTimeUtcOffset();
-    UA_DateTimeStruct dts = UA_DateTime_toStruct(*p);
+    UA_DateTime localtime = *p + tOffset;
+    UA_DateTimeStruct dts = UA_DateTime_toStruct(localtime);
     char dateString[100];
     UA_snprintf((char*)dateString, 100,
                 "%04u-%02u-%02u %02u:%02u:%02u.%03u (UTC%+05d)",
                 dts.year, dts.month, dts.day, dts.hour, dts.min,
                 dts.sec, dts.milliSec,
             (int)(tOffset / UA_DATETIME_SEC / 36));
     return UA_PrintContext_addString(ctx, dateString);
 }

Used CMake options:

cmake ..

Checklist

Please provide the following information:

  • open62541 Version (release number or git tag): v1.3.6
  • Other OPC UA SDKs used (client or server):
  • Operating system: Red Hat Enterprise Linux 8.8
  • Logs (with UA_LOGLEVEL set as low as necessary) attached
  • Wireshark network dump attached
  • Self-contained code example attached
  • Critical issue
@jpfr
Copy link
Member

jpfr commented Sep 25, 2023

We already use the better encoding for JSON.
We can use the same for the normal _print.

This list gives a good overview on sane date formats:
https://ijmacd.github.io/rfc3339-iso8601/

@jpfr
Copy link
Member

jpfr commented Sep 25, 2023

On the master branch, we we already pretty-print via the JSON encoding.
So the only place that needs to be adjusted is in the logging:

printf("[%04u-%02u-%02u %02u:%02u:%02u.%03u (UTC%+05d)] %s/%s" ANSI_COLOR_RESET "\t",

@dirk-zimoch
Copy link
Author

I have noticed that UA_print has disappeared from the master branch. That‘s one of the reasons why I prefer the 1.3 branch.

@jpfr
Copy link
Member

jpfr commented Sep 25, 2023

No, it has not disappeared. Right here:

UA_print(const void *p, const UA_DataType *type, UA_String *output);

Just we reuse the JSON encoding now also for pretty-printing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants