0

Linux kernel provides time_to_tm() (see here):

/**
 * time_to_tm - converts the calendar time to local broken-down time
 *
 * @totalsecs   the number of seconds elapsed since 00:00:00 on January 1, 1970,
 *              Coordinated Universal Time (UTC).
 * @offset      offset seconds adding to totalsecs.
 * @result      pointer to struct tm variable to receive broken-down time
 */
void time_to_tm(time_t totalsecs, int offset, struct tm *result)

According to description, tm will be local broken-down time. Thus I understand tm will respect my local time zone and DST. If this correct, I don't see it in the code.

Maybe argument offset should be used to "provide" local time zone and DST?

UPDATE

Following this question, thus using sys_tz in conjunction with time_to_tm() we can get "true" local time? AFAIK, localtime notation belongs to userland. For example DST is defined in specially compiled configuration files per time zone.

I'm confused. What is the meaning of sys_tz in kernel than?

Community
  • 1
  • 1
dimba
  • 26,717
  • 34
  • 141
  • 196

2 Answers2

3

The kernel doesn't know or care about timezones or DST, everything it does is in terms of seconds since the epoch. Timezones and DST are handled by libraries in user mode, which check your environment variables and can scan the timezone files.

This function is not callable by the end user -- there's no system call interface to it. It's just used internally within the kernel. If you look in the cross-reference (http://lxr.free-electrons.com/ident?v=2.6.33;i=time_to_tm), the only place it's currently called from is the FAT filesystem driver. It is indeed used to adjust for timezone; it was done to support the tzoff mount option.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • This is what I'm aware of too, but comment say different :) What offset argument should be used for? – dimba Sep 02 '12 at 16:50
  • According to http://stackoverflow.com/questions/5077192/how-to-get-current-hour-time-of-day-in-linux-kernel-space we can local time from with kernel. – dimba Sep 02 '12 at 18:55
1

Userspace can call settimeofday() to pass local time and timezone into the kernel. The timezone is stored in sys_tz (see do_sys_settimeofday() in kernel/time.c). The kernel mainly uses sys_tz to return local time back to userspace through gettimeofday() etc., and there are a few places like fs/fat that want to use the timezone too.

Roland
  • 6,227
  • 23
  • 29