Skip to content

Conversation

@akosthekiss
Copy link
Member

In the non-Windows code paths:

  • New approach to compute TZA without the need for GNU-specific
    struct tm.tm_gmtoff.
  • Always using usleep to sleep. (No real need for nanosleep as
    port API has sleep granularity of milliseconds.)
  • Not checking for "time.h" at build configuration time as that
    header is mandated by the C standard.
  • Not checking for "unistd.h" at build configuration time as that
    header is mandated by the POSIX standard (the default port is
    targeting POSIX systems -- and Windows).
  • Fixing some macro guards.

JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu

@akosthekiss akosthekiss added the jerry-port Related to the port API or the default port implementation label Jan 19, 2021
@akosthekiss
Copy link
Member Author

I have to admit that I'm not sure whether existing test cases cover these routines properly. I've partially validated the TZA computation with the C code below:

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

double tza_old(time_t time_utc) {
  tzset(); // should be executed only once?

  // time in broken-down structure, expressed in local time zone
  struct tm tm_local;
  localtime_r(&time_utc, &tm_local);

  // seconds east of UTC (from GNU-specific structure field)
  return (double)tm_local.tm_gmtoff;
}

double tza_new(time_t time_utc) {
  // time in broken-down structure, expressed as UTC time
  struct tm tm_utc;
  gmtime_r(&time_utc, &tm_utc);

  // time converted back to seconds since Epoch UTC
  tm_utc.tm_isdst = -1; // if not overridden, DST will not be taken into account
  time_t time_local = mktime(&tm_utc);

  // seconds east of UTC (from simple time differences)
  return difftime(time_utc, time_local);
}

int main() {
  time_t now_utc = time(NULL); // seconds since Epoch UTC
  time_t past_utc = now_utc - 182L * 24 * 60 * 60; // cca. half a year ago

  printf("tza-old now: %lf\n", tza_old(now_utc));
  printf("tza-old past: %lf\n", tza_old(past_utc));

  printf("tza-new now: %lf\n", tza_new(now_utc));
  printf("tza-new past: %lf\n", tza_new(past_utc));

  return 0;
}

Built and ran on Ubuntu 18.04.5 LTS (Linux 4.15.0-130-generic x86_64) as:

$ gcc tza.c -o tza
$ ./tza 
tza-old now: 3600.000000
tza-old past: 7200.000000
tza-new now: 3600.000000
tza-new past: 7200.000000

Copy link
Contributor

@lygstate lygstate left a comment

Choose a reason for hiding this comment

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

LGTM

@lygstate
Copy link
Contributor

@akosthekiss Does this what you want?

lygstate@DESKTOP-94PU0GB:/mnt/c/work/study/languages/typescript/jerryscript$ ./a.out
tza-old now: -28800.000000
tza-old past: -25200.000000
tza-new now: -28800.000000
tza-new past: -25200.000000

@akosthekiss
Copy link
Member Author

@lygstate Thanks. Depends on your actual time zone. Looks like GMT-8: PST? Validates part of the code. But the is_utc=false logic of the TZA port function is still hard to check whether it works OK. (The above C code does not validate that at all.)

@lygstate
Copy link
Contributor

Yeap, it's GMT-8

@lygstate
Copy link
Contributor

@lygstate Thanks. Depends on your actual time zone. Looks like GMT-8: PST? Validates part of the code. But the is_utc=false logic of the TZA port function is still hard to check whether it works OK. (The above C code does not validate that at all.)

If I read the code correctly, the is_utc option didn't considerated on _WIN32

@lygstate
Copy link
Contributor

I have a question, If the OS provide what's the current time zone is, can we use the timezone info directly implement jerry_port_get_local_time_zone_adjustment ?

Copy link
Member

@rerobika rerobika 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 the style fix

(void) is_utc; /* unused */
return 0.0;
#endif /* HAVE_TM_GMTOFF */
#endif
Copy link
Member

Choose a reason for hiding this comment

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

Please add the missing comments to preprocessor directives. (here and below)

In the non-Windows code paths:
- New approach to compute TZA without the need for GNU-specific
  `struct tm.tm_gmtoff`.
- Always using `usleep` to sleep. (No real need for `nanosleep` as
  port API has sleep granularity of milliseconds.)
- Not checking for "time.h" at build configuration time as that
  header is mandated by the C standard.
- Not checking for "unistd.h" at build configuration time as that
  header is mandated by the POSIX standard (the default port is
  targeting POSIX systems -- and Windows).
- Fixing some macro guards.

JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
@akosthekiss
Copy link
Member Author

Added the pre-processor directive comments. PR updated. Thanks for the review.

Copy link
Member

@zherczeg zherczeg left a comment

Choose a reason for hiding this comment

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

LGTM

@akosthekiss akosthekiss merged commit 87d30b8 into jerryscript-project:master Jan 29, 2021
@akosthekiss akosthekiss deleted the port-default-time branch January 29, 2021 09:45
rerobika pushed a commit to rerobika/jerryscript that referenced this pull request Feb 17, 2021
PR jerryscript-project#4513 caused a measurable slowdown by removing the GNU specific TZA caclutation.
This patch reverts this removal but also keeps the general implemtation introduced in jerryscript-project#4513

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik robert.fancsik@h-lab.eu
rerobika pushed a commit to rerobika/jerryscript that referenced this pull request Feb 17, 2021
PR jerryscript-project#4513 caused a measurable slowdown by removing the GNU specific TZA caclutation.
This patch reverts this removal but also keeps the general implemtation introduced in jerryscript-project#4513

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik robert.fancsik@h-lab.eu
rerobika pushed a commit to rerobika/jerryscript that referenced this pull request Feb 17, 2021
PR jerryscript-project#4513 caused a measurable slowdown by removing the GNU specific TZA calculation.
This patch reverts this removal but also keeps the general implementation introduced in jerryscript-project#4513

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik robert.fancsik@h-lab.eu
rerobika pushed a commit to rerobika/jerryscript that referenced this pull request Feb 17, 2021
PR jerryscript-project#4513 caused a measurable slowdown by removing the GNU specific TZA calculation.
This patch reverts this removal but also keeps the general implementation introduced in jerryscript-project#4513

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik robert.fancsik@h-lab.eu
dbatyai pushed a commit that referenced this pull request Feb 17, 2021
PR #4513 caused a measurable slowdown by removing the GNU specific TZA calculation.
This patch reverts this removal but also keeps the general implementation introduced in #4513

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik robert.fancsik@h-lab.eu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jerry-port Related to the port API or the default port implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants