Live data from Hacker News

How setting the TZ environment variable avoids thousands of system calls

blog.packagecloud.io

31–40 of 148 posts

Re: How setting the TZ environment variable avoids thousands of system calls

#31

Does this affect FreeBSD as well?

Experimentally, no. The example program calls localtime(3) 10 times but only accesses the file once, per truss:

    write(1,"Greetings!n",11)			 = 11 (0xb)
    access("/etc/localtime",R_OK)			 = 0 (0x0)
    open("/etc/localtime",O_RDONLY,037777777600)	 = 3 (0x3)
    fstat(3,{ mode=-r--r--r-- ,inode=11316113,size=2819,blksize=32768 }) = 0 (0x0)
    read(3,"TZif20000000000000"...,41448) = 2819 (0xb03)
    close(3)					 = 0 (0x0)
    issetugid()					 = 0 (0x0)
    open("/usr/share/zoneinfo/posixrules",O_RDONLY,00) = 3 (0x3)
    fstat(3,{ mode=-r--r--r-- ,inode=327579,size=3519,blksize=32768 }) = 0 (0x0)
    read(3,"TZif20000000000000"...,41448) = 3519 (0xdbf)
    close(3)					 = 0 (0x0)
    write(1,"Godspeed, dear friend!n",23)		 = 23 (0x17)
(FreeBSD caches the database on the first call: https://svnweb.freebsd.org/base/head/contrib/tzcode/stdtime/... )

Re: How setting the TZ environment variable avoids thousands of system calls

#33

It would be highly inconvenient to have to set this variable if you live in a country where you change the timezone twice a year due to summertime.

Are you talking about DST? You don't change your timezone at DST switch, the timezone data knows how to calculate the correct time based on your location (roughly) and the time of year. Mountain standard time and mountain daylight time are both part of mountain time, for example.

Re: How setting the TZ environment variable avoids thousands of system calls

#34

It would be highly inconvenient to have to set this variable if you live in a country where you change the timezone twice a year due to summertime.

If TZ is set properly then you don't need to change it twice a year.

(man tzset for more info and examples of different values TZ can be set to. Mine is set to "Europe/London" which handles the DST switches automatically.)

Re: How setting the TZ environment variable avoids thousands of system calls

#35

It would be highly inconvenient to have to set this variable if you live in a country where you change the timezone twice a year due to summertime.

Are you talking about DST? You don't change your timezone at DST switch, the timezone data knows how to calculate the correct time based on your location (roughly) and the time of year. Mountain standard time and mountain daylight time are both part of mountain time, for example.

What I meant was: Currently my timezone is CET. In a month's time it will be CEST. If I have to set TZ to explicitly mirror that it will be a burden.

Re: How setting the TZ environment variable avoids thousands of system calls

#36
post #34

It would be highly inconvenient to have to set this variable if you live in a country where you change the timezone twice a year due to summertime.

If TZ is set properly then you don't need to change it twice a year. (man tzset for more info and examples of different values TZ can be set to. Mine is set to "Europe/London" which handles the DST switches automatically.)

Ah yes, thanks for pointing that out!

Re: How setting the TZ environment variable avoids thousands of system calls

#37
post #34

It would be highly inconvenient to have to set this variable if you live in a country where you change the timezone twice a year due to summertime.

If TZ is set properly then you don't need to change it twice a year. (man tzset for more info and examples of different values TZ can be set to. Mine is set to "Europe/London" which handles the DST switches automatically.)

Hm…, now that I read TZSET(3): Wouldn't that be

    TZ=:Europe/London
It doesn't seem tzset() will accept the format

    TZ=Europe/London

?

Re: How setting the TZ environment variable avoids thousands of system calls

#39

Earlier quoted context omitted.

Are you talking about DST? You don't change your timezone at DST switch, the timezone data knows how to calculate the correct time based on your location (roughly) and the time of year. Mountain standard time and mountain daylight time are both part of mountain time, for example.

What I meant was: Currently my timezone is CET. In a month's time it will be CEST. If I have to set TZ to explicitly mirror that it will be a burden.

TZ may be set like this:

   TZ=:Europe/Copenhagen
Replace Europe/Copenhagen with the appropriate entry from the list: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

Usually, /etc/localtime is a symlink, as on my laptop:

  /etc/localtime -> /usr/share/zoneinfo/Europe/Copenhagen
so TZ=:/etc/localtime has the same result.

You can demonstrate that it takes account of changes to timezones:

Normal time for London:

  $ TZ=:Europe/London date -d '1995-12-30 12:00 UTC' -R
  Sat, 30 Dec 1995 12:00:00 +0000
British Summer Time:

  $ TZ=:Europe/London date -d '1995-06-30 12:00 UTC' -R
  Fri, 30 Jun 1995 13:00:00 +0100
'Double British Summer Time', used for a period during World War 2:

  $ TZ=:Europe/London date -d '1945-06-30 12:00 UTC' -R
  Sat, 30 Jun 1945 14:00:00 +0200
Before London's time was standardized to Greenwich:

  $ TZ=:Europe/London date -d '1845-06-30 12:00 UTC' -R
  Mon, 30 Jun 1845 11:58:45 -0001

Re: How setting the TZ environment variable avoids thousands of system calls

#40

It would be highly inconvenient to have to set this variable if you live in a country where you change the timezone twice a year due to summertime.

TZs should be defined in the form "Europe/London", "Asia/Beirut", "Pacific/Auckland", etc.

Although the hour offset can change at extremely short notice (e.g. discontinuing Daylight Savings during Ramadan [1]), the timezone declaration (e.g. "Africa/Casablanca"), shouldn't need to change, just the underlying timezone database.

[1] https://en.wikipedia.org/wiki/Daylight_saving_time_in_Morocc...

Post reply on HN