Skip to content

define TZ environment variable to match current Intl.DateTimeFormat#16517

Open
arsnyder16 wants to merge 17 commits intoemscripten-core:mainfrom
arsnyder16:asnyder/timezone
Open

define TZ environment variable to match current Intl.DateTimeFormat#16517
arsnyder16 wants to merge 17 commits intoemscripten-core:mainfrom
arsnyder16:asnyder/timezone

Conversation

@arsnyder16
Copy link
Contributor

In order to facilitate timezone detection there are a few standard ways of doing that "TZ" environment variable and etc/localtime.

This will allow libraries like icu4c to properly detect the current system timezone, which look for IANA time zone names in a few locations

The TZ environment variable should work out of the box for icu4c
The etc/localtime symlink requires icu4c to be built with CHECK_LOCALTIME_LINK which is defined if any of these are true

Copy link
Collaborator

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

__attribute__((__weak__)) int daylight = 0;
__attribute__((__weak__)) char *tzname[2] = { 0, 0 };
thread_local char buffer[128];
thread_local char timezone_buffer[128];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can do inside the emscripten_get_timezone function.

Copy link
Contributor Author

@arsnyder16 arsnyder16 Mar 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/root/cache/sysroot/include/threads.h:12:22: note: expanded from macro 'thread_local'
#define thread_local _Thread_local
                     ^
/root/project/system/lib/libc/emscripten_time.c:33:10: error: address of stack memory associated with local variable 'buffer' returned [-Werror,-Wreturn-stack-address]
  return buffer;
         ^~~~~~
2 errors generated.

https://app.circleci.com/pipelines/github/emscripten-core/emscripten/20045/workflows/dca85328-f660-46d7-93eb-4e1d1013cca5/jobs/517220

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you also need static then?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it does imply static, but the error is related to it being a "stack" variable which seems to be a little off, is there a reason you prefer static but local function variable vs just thread_local as global?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

threads.h

#ifdef __cplusplus
extern "C" {
typedef unsigned long thrd_t;
#else
typedef struct __pthread *thrd_t;
#define thread_local _Thread_local
#endif

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbc100 How do you want me to proceed now?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is C code you must explicitly add the static specifier. So use static thread_local

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear the error might be a little misleading: variables must have global storage. Here "global storage" also includes function-local statics.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh makes sense. Thanks @sbc100

Copy link
Collaborator

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! (after one last comment)

@sbc100 sbc100 marked this pull request as ready for review March 25, 2022 23:16
@sbc100
Copy link
Collaborator

sbc100 commented Mar 25, 2022

I think if you rebase (or merge) the current failures will go away.

@sbc100
Copy link
Collaborator

sbc100 commented Mar 25, 2022

Otherwise I think this is ready to land

void _emscripten_get_timezone_js(char* buffer, int length);

const char* emscripten_get_timezone() {
static thread_local char buffer[128];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does 128 come from? Might be worth a comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call, looked deeper into the Tz database to properly size, also added a comment

@sbc100 sbc100 enabled auto-merge (squash) March 25, 2022 23:32
auto-merge was automatically disabled March 26, 2022 18:51

Head branch was pushed to by a user without write access

@sbc100
Copy link
Collaborator

sbc100 commented May 2, 2022

Can you update the PR title and description to match what the change actually does? Or comment here with a proposal for the working on the commit message?

@arsnyder16 arsnyder16 changed the title proposal for better timezone detection define TZ environment variable to match current Intl.DateTimeFormat May 2, 2022
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

Successfully merging this pull request may close these issues.

2 participants