Add function to convert between calendar time and broken-down time for universal use
PATCH v2 1/2 Add function to convert between calendar time and broken-down time for universal use
There are many similar code in kernel for one object:
convert time between calendar time and broken-down time.
Here is some source I found:fs/ncpfs/dir.cfs/smbfs/proc.cfs/fat/misc.cfs/udf/udftime.cfs/cifs/netmisc.cnet/netfilter/xt_time.cdrivers/scsi/ips.cdrivers/input/misc/hp_sdc_rtc.cdrivers/rtc/rtc-lib.carch/ia64/hp/sim/boot/fw-emu.carch/m68k/mac/misc.carch/powerpc/kernel/time.carch/parisc/include/asm/rtc.h
...
We can make a common function for this type of conversion, At least we can get following benefit:
1: Make kernel simple and unify
2: Easy to fix bug in converting code
3: Reduce clone of code in future
For example, I'm trying to make ftrace display walltime, this patch will make me easy.
This code is based on code from glibc-2.6
---
include/linux/time.h | 9 +++ kernel/time/Makefile | 2 +- kernel/time/timeconv.c | 180 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+), 1 deletions(-)
create mode 100644 kernel/time/timeconv.c
include/linux/time.h +0 -0
@@ -151,6 +151,15 @@ extern void update_xtime_cache(u64 nsec);
struct tms;
extern void do_sys_times(struct tms *);
+extern void gmtime(__kernel_time_t totalsecs,
+ unsigned int *year, unsigned int *mon, unsigned int *mday, + unsigned int *hour, unsigned int *min, unsigned int *sec, + unsigned int *wday, unsigned int *yday); +extern void localtime(__kernel_time_t totalsecs, + unsigned int *year, unsigned int *mon, unsigned int *mday, + unsigned int *hour, unsigned int *min, unsigned int *sec, + unsigned int *wday, unsigned int *yday);
+
/**
* timespec_to_ns - Convert timespec to nanoseconds
* @ts: pointer to the timespec variable to be converted
kernel/time/Makefile +1 -1
@@ -1,4 +1,4 @@ -obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o +obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o timeconv.o
obj-$(CONFIG_GENERIC_CLOCKEVENTS_BUILD) += clockevents.o
obj-$(CONFIG_GENERIC_CLOCKEVENTS) += tick-common.o
kernel/time/timeconv.c new file +180 -0
180 lines not on this page · raw message
Signature · 1 line
1.5.5.3
PATCH v2 2/2 fs/fat: Use common localtime/gmtime in fat_time_unix2fat()
It is not necessary to write custom code for convert calendar time to broken-down time.
localtime()/gmtime() is more generic to do that.
---fs/fat/misc.c | 62 ++++++++++++++++++--------------------------------------
1 files changed, 20 insertions(+), 42 deletions(-)
fs/fat/misc.c +0 -0
@@ -9,6 +9,7 @@
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/buffer_head.h>
+#include <linux/time.h>
#include "fat.h"
/*
@@ -155,10 +156,6 @@ extern struct timezone sys_tz;
#define SECS_PER_MIN 60
#define SECS_PER_HOUR (60 * 60)
#define SECS_PER_DAY (SECS_PER_HOUR * 24)
-#define UNIX_SECS_1980 315532800L
-#if BITS_PER_LONG == 64
-#define UNIX_SECS_2108 4354819200L
-#endif
/* days between 1.1.70 and 1.1.80 (2 leap days) */
#define DAYS_DELTA (365 * 10 + 2)
/* 120 (2100 - 1980) isn't leap year */
@@ -211,58 +208,40 @@ void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts, void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec *ts,
__le16 *time, __le16 *date, u8 *time_cs)
{
- time_t second = ts->tv_sec;
- time_t day, leap_day, month, year;
-
- if (!sbi->options.tz_utc)
- second -= sys_tz.tz_minuteswest * SECS_PER_MIN;
+ unsigned int year, mon, mday, hour, min, sec;
+ if (sbi->options.tz_utc) {
+ gmtime(ts->tv_sec,
+ &year, &mon, &mday, &hour, &min, &sec, NULL, NULL);
+ } else {
+ localtime(ts->tv_sec,
+ &year, &mon, &mday, &hour, &min, &sec, NULL, NULL);
+ }
- /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
- if (second < UNIX_SECS_1980) {
+ /* FAT can only support year between 1980 to 2107 */
+ if (year < 1980 - 1900) {
*time = 0;
*date = cpu_to_le16((0 << 9) | (1 << 5) | 1);
if (time_cs)
*time_cs = 0;
return;
}
-#if BITS_PER_LONG == 64
- if (second >= UNIX_SECS_2108) {
+ if (year > 2107 - 1900) {
*time = cpu_to_le16((23 << 11) | (59 << 5) | 29);
*date = cpu_to_le16((127 << 9) | (12 << 5) | 31);
if (time_cs)
*time_cs = 199;
return;
}-#endif
- day = second / SECS_PER_DAY - DAYS_DELTA; - year = day / 365; - leap_day = (year + 3) / 4; - if (year > YEAR_2100) /* 2100 isn't leap year */
- leap_day--;
- if (year * 365 + leap_day > day)
- year--;
- leap_day = (year + 3) / 4;
- if (year > YEAR_2100) /* 2100 isn't leap year */
- leap_day--;
- day -= year * 365 + leap_day; + /* from 1900 -> from 1980 */ + year -= 80; + /* 0~11 -> 1~12 */ + mon++; + /* 0~59 -> 0~29(2sec counts) */ + sec >>= 1;
- if (IS_LEAP_YEAR(year) && day == days_in_year[3]) {
- month = 2;
- } else {
- if (IS_LEAP_YEAR(year) && day > days_in_year[3])
- day--;
- for (month = 1; month < 12; month++) {
- if (days_in_year[month + 1] > day)
- break;
- }
- }
- day -= days_in_year[month];
-
- *time = cpu_to_le16(((second / SECS_PER_HOUR) % 24) << 11
- | ((second / SECS_PER_MIN) % 60) << 5
- | (second % SECS_PER_MIN) >> 1);
- *date = cpu_to_le16((year << 9) | (month << 5) | (day + 1));
+ *time = cpu_to_le16(hour << 11 | min << 5 | sec);
+ *date = cpu_to_le16(year << 9 | mon << 5 | mday);
if (time_cs)
*time_cs = (ts->tv_sec & 1) * 100 + ts->tv_nsec / 10000000;
}
@@ -283,4 +262,3 @@ int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs)
}
return err;
}-
Signature · 1 line
1.5.5.3
Quoted 19 lines · On Tue, 14 Jul 2009 16:03:12 +0800 Zhaolei <[email protected]> wrote:
There are many similar code in kernel for one object:
convert time between calendar time and broken-down time.Here is some source I found:
fs/ncpfs/dir.cfs/smbfs/proc.cfs/fat/misc.cfs/udf/udftime.cfs/cifs/netmisc.cnet/netfilter/xt_time.cdrivers/scsi/ips.cdrivers/input/misc/hp_sdc_rtc.cdrivers/rtc/rtc-lib.carch/ia64/hp/sim/boot/fw-emu.carch/m68k/mac/misc.carch/powerpc/kernel/time.carch/parisc/include/asm/rtc.h
...
Quoted 8 lines
We can make a common function for this type of conversion, At least we can get following benefit:
1: Make kernel simple and unify
2: Easy to fix bug in converting code
3: Reduce clone of code in future
For example, I'm trying to make ftrace display walltime, this patch will make me easy.
The objective is a good one. Have you verified that these new library functions can be used by most/all of the above callers?
Quoted 16 lines
Signed-off-by Zhao Lei <[email protected]>--- include/linux/time.h | 9 +++ kernel/time/Makefile | 2 +- kernel/time/timeconv.c | 180 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+), 1 deletions(-) create mode 100644 kernel/time/timeconv.cinclude/linux/time.h +0
-0@@ -151,6 +151,15 @@ extern void update_xtime_cache(u64 nsec);struct tms ;
include/linux/time.h extern void update_xtime_cache(u64 nsec); · line 161
Quoted 10 lines
include/linux/time.h +8
-0extern void do_sys_times (struct tms *); +extern void gmtime (__kernel_time_t totalsecs, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday); +extern void localtime (__kernel_time_t totalsecs, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday);
I'd imagine that many callers would at least need some types changed - replace int' with unsigned int', etc. Not a big problem.
Quoted 23 lines
include/linux/time.h +0
-0/** * timespec_to_ns - Convert timespec to nanoseconds * @ts: pointer to the timespec variable to be converted kernel/time/Makefile +0
-1@@ -1,4 +1,4 @@ -obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.otimecompare.o
+obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o timeconv.oobj-$(CONFIG_GENERIC_CLOCKEVENTS_BUILD) += clockevents.o
obj-$(CONFIG_GENERIC_CLOCKEVENTS) += tick-common.okernel/time/timeconv.c new file +2
-0@@ -0,0 +1,180 @@ +/* +* Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation,
Quoted 1 line
Inc.
The patch is significantly wordwrapped by your email client.
The patch has no tabs in it at all - either some serious cleanup is needed of your email client is performing tab-to-space conversion.
Quoted 29 lines
+ * This file is part of the GNU C Library.
+ * Contributed by Paul Eggert ([email protected]).
+ *
+ * The GNU C Library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * The GNU C Library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with the GNU C Library; see the file COPYING.LIB. If not,
+ * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */kernel/time/timeconv.c new file +10
-0+ +/* +* Converts the calendar time to broken-down time representation +* Based on code from glibc-2.6 +* +* 2009-7-14: +* Moved from glibc-2.6 to kernel by Zhaolei<[email protected]> +*/ + +#include <linux/time.h>
kernel/time/timeconv.c line 40
Quoted 10 lines
kernel/time/timeconv.c new file +10
-0+#include <linux/module.h> + +/* +* Nonzero if YEAR is a leap year (every 4 years, +* except every 100th isn't, and every 400th is). +*/ +static inline int __isleap (unsigned int year) +{ +return (year) %4 ==0 && ((year) %100 !=0 || (year) %400 ==0 ); +}
The explicit `inline' probably isn't beneficial here.
kernel/time/timeconv.c line 52
Quoted 11 lines
kernel/time/timeconv.c new file +11
-0+/* How many days come before each month (0-12). */ +static const unsigned short __mon_yday[2 ][13 ] = + { +/* Normal years. */ + {0 ,31 ,59 ,90 ,120 ,151 ,181 ,212 ,243 ,273 ,304 ,334 ,365 }, +/* Leap years. */ + {0 ,31 ,60 ,91 ,121 ,152 ,182 ,213 ,244 ,274 ,305 ,335 ,366 } + }; + +#define SECS_PER_HOUR (60 * 60) +#define SECS_PER_DAY (SECS_PER_HOUR * 24)
I wonder if these whould be in a header. I guess not, if there aren't any sites which can use this.
Quoted 24 lines
kernel/time/timeconv.c new file +24
-0+static void __offtime (__kernel_time_t totalsecs,int offset, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday) +{ +long days, rem, y; +const unsigned short *ip; + + days = totalsecs /SECS_PER_DAY ; + rem = totalsecs %SECS_PER_DAY ; + rem += offset; +while (rem <0 ) { + rem +=SECS_PER_DAY ; + --days; + } +while (rem >=SECS_PER_DAY ) { + rem -=SECS_PER_DAY ; + ++days; + } + +if (hour) + *hour = rem /SECS_PER_HOUR ; + rem %=SECS_PER_HOUR ; +if (min)
kernel/time/timeconv.c line 87
Quoted 10 lines
kernel/time/timeconv.c new file +10
-0+ *min = rem /60 ; +if (sec) + *sec = rem %60 ; + +if (wday) { +/* January 1, 1970 was a Thursday. */ + *wday = (4 + days) %7 ; +if (*wday <0 ) + *wday +=7 ; + }
The code should all be converted to standard kernel style, please. Mainly the use of hard tabs.
kernel/time/timeconv.c line 91
Quoted 3 lines
kernel/time/timeconv.c new file +3
-0+ y =1970 ; + +#define DIV (a, b) ((a) / (b) - ((a) % (b) < 0))
hm, I wonder what that does.
It would be clearer to convert this into a regular C function, along with a comment whcih explains what it does.
Quoted 1 line
+#define
LEAPS_THRU_END_OF(y)(DIV(y, 4)-DIV(y, 100)+DIV(y, 400))
Ditto.
Quoted 52 lines
kernel/time/timeconv.c new file +44
-0+while (days <0 || days >= (__isleap (y) ?366 :365 )) { +/* Guess a corrected year, assuming 365 days per year. */ +long yg = y + days /365 - (days %365 <0 ); + +/* Adjust DAYS and Y to match the guessed year. */ + days -= ((yg - y) *365 + +LEAPS_THRU_END_OF (yg -1 ) + -LEAPS_THRU_END_OF (y -1 )); + y = yg; + } +if (year) { + *year = y -1900 ; +if (*year != y -1900 ) { +/* The year cannot be represented due to overflow. */ + *year =-1 ; + } + } + +if (yday) + *yday = days; + + ip = __mon_yday[__isleap (y)]; +for (y =11 ; days < ip[y]; y--) +continue ; + days -= ip[y]; +if (mon) + *mon = y; +if (mday) + *mday = days +1 ; +} +void gmtime (__kernel_time_t totalsecs, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday) +{ +__offtime (totalsecs,0 , year, mon, mday, hour, min, sec, wday, yday); +} +EXPORT_SYMBOL (gmtime); +void localtime (__kernel_time_t totalsecs, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday) +{ + __offtime(totalsecs, -sys_tz.tz_minuteswest * 60, year, mon, mday,hour,
kernel/time/timeconv.c line 180
Quoted 3 lines
kernel/time/timeconv.c new file +3
-0+ min, sec, wday, yday); +} +EXPORT_SYMBOL (localtime);
These are such simple wrappers around __offtime() that it might be better to make them static inlines in time.h, so callers will end up directly calling __offtime() rather than having to remarshal ten function arguments.
Quoted 33 lines · Andrew Morton
Quoted 19 lines · On Tue, 14 Jul 2009 16:03:12 +0800 Zhaolei <[email protected]> wrote:
There are many similar code in kernel for one object:
convert time between calendar time and broken-down time.Here is some source I found:
fs/ncpfs/dir.cfs/smbfs/proc.cfs/fat/misc.cfs/udf/udftime.cfs/cifs/netmisc.cnet/netfilter/xt_time.cdrivers/scsi/ips.cdrivers/input/misc/hp_sdc_rtc.cdrivers/rtc/rtc-lib.carch/ia64/hp/sim/boot/fw-emu.carch/m68k/mac/misc.carch/powerpc/kernel/time.carch/parisc/include/asm/rtc.h
...Quoted 9 lines
We can make a common function for this type of conversion, At least we can get following benefit:
1: Make kernel simple and unify
2: Easy to fix bug in converting code
3: Reduce clone of code in future
For example, I'm trying to make ftrace display walltime, this patch will make me easy.
Quoted 3 lines
The objective is a good one. Have you verified that these new library functions can be used by most/all of the above callers?
Hello, Andrew
Thanks for your review.
I checked some of them, and I think this new library can make them simple. A example is my patch for fs/fat/misc.c.
Quoted 18 lines
Quoted 18 lines
Signed-off-by Zhao Lei <[email protected]>--- include/linux/time.h | 9 +++ kernel/time/Makefile | 2 +- kernel/time/timeconv.c | 180 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+), 1 deletions(-) create mode 100644 kernel/time/timeconv.cinclude/linux/time.h +0
-0@@ -151,6 +151,15 @@ extern void update_xtime_cache(u64 nsec);struct tms ;extern void do_sys_times (struct tms *);
Quoted 13 lines
include/linux/time.h extern void update_xtime_cache(u64 nsec); · line 162
Quoted 9 lines
include/linux/time.h +8
-0+extern void gmtime (__kernel_time_t totalsecs, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday); +extern void localtime (__kernel_time_t totalsecs, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday);I'd imagine that many callers would at least need some types changed - replace
int' withunsigned int', etc. Not a big problem.
Actually, I considered to use int instead, but at last I use unsigned int to make it same with mktime() which is already in kernel.
Quoted 29 lines
Quoted 24 lines
include/linux/time.h +0
-0/** * timespec_to_ns - Convert timespec to nanoseconds * @ts: pointer to the timespec variable to be converted kernel/time/Makefile +0
-1@@ -1,4 +1,4 @@ -obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.otimecompare.o
+obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o timeconv.oobj-$(CONFIG_GENERIC_CLOCKEVENTS_BUILD) += clockevents.o
obj-$(CONFIG_GENERIC_CLOCKEVENTS) += tick-common.okernel/time/timeconv.c new file +2
-0@@ -0,0 +1,180 @@ +/* +* Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Quoted 2 lines
Inc.
The patch is significantly wordwrapped by your email client.
Quoted 3 lines
The patch has no tabs in it at all - either some serious cleanup is needed of your email client is performing tab-to-space conversion.
Sorry...I reinstalled a email client......
I'll fix it and resend this patch.
Quoted 34 lines
Quoted 34 lines
+ * This file is part of the GNU C Library.
+ * Contributed by Paul Eggert ([email protected]).
+ *
+ * The GNU C Library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * The GNU C Library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with the GNU C Library; see the file COPYING.LIB. If not,
+ * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */kernel/time/timeconv.c new file +15
-0+ +/* +* Converts the calendar time to broken-down time representation +* Based on code from glibc-2.6 +* +* 2009-7-14: +* Moved from glibc-2.6 to kernel by Zhaolei<[email protected]> +*/ + +#include <linux/time.h> +#include <linux/module.h> + +/* +* Nonzero if YEAR is a leap year (every 4 years, +* except every 100th isn't, and every 400th is).
Quoted 9 lines
kernel/time/timeconv.c line 40
Quoted 6 lines
+ */
kernel/time/timeconv.c new file +4
-0+static inline int __isleap (unsigned int year) +{ +return (year) %4 ==0 && ((year) %100 !=0 || (year) %400 ==0 ); +}The explicit `inline' probably isn't beneficial here.
OK, I'll remove it.
Quoted 17 lines
kernel/time/timeconv.c line 52
Quoted 13 lines
kernel/time/timeconv.c new file +11
-0+/* How many days come before each month (0-12). */ +static const unsigned short __mon_yday[2 ][13 ] = + { +/* Normal years. */ + {0 ,31 ,59 ,90 ,120 ,151 ,181 ,212 ,243 ,273 ,304 ,334 ,365 }, +/* Leap years. */ + {0 ,31 ,60 ,91 ,121 ,152 ,182 ,213 ,244 ,274 ,305 ,335 ,366 } + }; + +#define SECS_PER_HOUR (60 * 60) +#define SECS_PER_DAY (SECS_PER_HOUR * 24)I wonder if these whould be in a header. I guess not, if there aren't any sites which can use this.
Agree, IMHO, it is not necessary to move it into header.
Quoted 30 lines
Quoted 30 lines
kernel/time/timeconv.c new file +29
-0+static void __offtime (__kernel_time_t totalsecs,int offset, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday) +{ +long days, rem, y; +const unsigned short *ip; + + days = totalsecs /SECS_PER_DAY ; + rem = totalsecs %SECS_PER_DAY ; + rem += offset; +while (rem <0 ) { + rem +=SECS_PER_DAY ; + --days; + } +while (rem >=SECS_PER_DAY ) { + rem -=SECS_PER_DAY ; + ++days; + } + +if (hour) + *hour = rem /SECS_PER_HOUR ; + rem %=SECS_PER_HOUR ; +if (min) + *min = rem /60 ; +if (sec) + *sec = rem %60 ; + +if (wday) {
Quoted 9 lines
kernel/time/timeconv.c line 87
Quoted 6 lines
kernel/time/timeconv.c new file +5
-0+/* January 1, 1970 was a Thursday. */ + *wday = (4 + days) %7 ; +if (*wday <0 ) + *wday +=7 ; + }The code should all be converted to standard kernel style, please. Mainly the use of hard tabs.
Sorry, I'll resend this patch.
Quoted 7 lines
kernel/time/timeconv.c line 91
Quoted 4 lines
kernel/time/timeconv.c new file +3
-0+ y =1970 ; + +#define DIV (a, b) ((a) / (b) - ((a) % (b) < 0))hm, I wonder what that does.
Quoted 9 lines
It would be clearer to convert this into a regular C function, along with a comment whcih explains what it does.
Quoted 2 lines
+#define
LEAPS_THRU_END_OF(y)(DIV(y, 4)-DIV(y, 100)+DIV(y, 400))Ditto.
It is because I try to keep similar style with code in glibc so that updates in glibc can easy to applied here.
But actually it looks ugly, I'll fix it.
Quoted 47 lines
Quoted 47 lines
kernel/time/timeconv.c new file +38
-0+while (days <0 || days >= (__isleap (y) ?366 :365 )) { +/* Guess a corrected year, assuming 365 days per year. */ +long yg = y + days /365 - (days %365 <0 ); + +/* Adjust DAYS and Y to match the guessed year. */ + days -= ((yg - y) *365 + +LEAPS_THRU_END_OF (yg -1 ) + -LEAPS_THRU_END_OF (y -1 )); + y = yg; + } +if (year) { + *year = y -1900 ; +if (*year != y -1900 ) { +/* The year cannot be represented due to overflow. */ + *year =-1 ; + } + } + +if (yday) + *yday = days; + + ip = __mon_yday[__isleap (y)]; +for (y =11 ; days < ip[y]; y--) +continue ; + days -= ip[y]; +if (mon) + *mon = y; +if (mday) + *mday = days +1 ; +} +void gmtime (__kernel_time_t totalsecs, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday) +{ +__offtime (totalsecs,0 , year, mon, mday, hour, min, sec, wday, yday); +} +EXPORT_SYMBOL (gmtime);
Quoted 17 lines
kernel/time/timeconv.c line 180
Quoted 11 lines
kernel/time/timeconv.c new file +6
-0+void localtime (__kernel_time_t totalsecs, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday) +{ + __offtime(totalsecs, -sys_tz.tz_minuteswest * 60, year, mon, mday,hour,
kernel/time/timeconv.c new file +3
-0+ min, sec, wday, yday); +} +EXPORT_SYMBOL (localtime);These are such simple wrappers around
__offtime()that it might be better to make them static inlines intime.h, so callers will end up directly calling__offtime()rather than having to remarshal ten function arguments.
Good idea, will fix.
Thanks
Zhaolei
v2 cover Add function to convert between calendar time and broken-down time for universal use
In reply to Andrew MortonHello, Andrew
I modified this patch based on your review.
Here is changes:
1: Fix "no tabs" error caused by email client.
2: Remove explicit 'inline' in __isleap()
3: Move DIV() and LEAPS_THRU_END_OF(y) into regular C function along with
comment
4: Move gmtime() and localtime() into header file.
Thanks
Zhaolei
PATCH v2 1/2 Add function to convert between calendar time and broken-down time for universal use
There are many similar code in kernel for one object:
convert time between calendar time and broken-down time.
Here is some source I found:fs/ncpfs/dir.cfs/smbfs/proc.cfs/fat/misc.cfs/udf/udftime.cfs/cifs/netmisc.cnet/netfilter/xt_time.cdrivers/scsi/ips.cdrivers/input/misc/hp_sdc_rtc.cdrivers/rtc/rtc-lib.carch/ia64/hp/sim/boot/fw-emu.carch/m68k/mac/misc.carch/powerpc/kernel/time.carch/parisc/include/asm/rtc.h
...
We can make a common function for this type of convert, At least we can get following benefit:
1: Make kernel simple and unify
2: Easy to fix bug in converting code
3: Reduce clone of code in future
For example, I'm trying to make ftrace display walltime, this patch will make me easy.
This code is based on code from glibc-2.6
--- include/linux/time.h | 62 +++++++++++++++++++ kernel/time/Makefile | 2 +- kernel/time/timeconv.c | 153 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 216 insertions(+), 1 deletions(-) create mode 100644 kernel/time/timeconv.c
include/linux/time.h +62 -0
@@ -151,6 +151,68 @@ extern void update_xtime_cache(u64 nsec);struct tms ;extern void do_sys_times (struct tms *); +extern void __offtime (__kernel_time_t totalsecs,int offset, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday); + +/** +* gmtime - converts the calendar time to UTC broken-down time +* +* @totalsecs The number of seconds elapsed since 00:00:00 on January 1, 1970, +* Coordinated Universal Time (UTC). +* @year Store the number of years since 1900. +* @mon Store the number of months since January, in the range 0 to 11. +* @mday Store the day of the month, in the range 1 to 31. +* @hour Store the number of hours past midnight, in the range 0 to 23. +* @min Store the number of minutes after the hour, +* in the range 0 to 59. +* @sec Store the number of seconds after the minute, normally in the +* range 0 to 59, but can be up to 60 to allow for leap seconds. +* @wday Store the number of days since Sunday, in the range 0 to 6. +* @yday Store the number of days since January 1, in the range 0 to 365. +* +* Similar to the gmtime() in glibc, broken-down time is expressed in +* Coordinated Universal Time (UTC). +*/ +static inline void gmtime (__kernel_time_t totalsecs, +unsigned int *year,unsigned int *mon, +unsigned int *mday,unsigned int *hour, +unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday) +{ +__offtime (totalsecs,0 , year, mon, mday, hour, min, sec, wday, yday); +} + +/** +* localtime - 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). +* @year Store the number of years since 1900. +* @mon Store the number of months since January, in the range 0 to 11. +* @mday Store the day of the month, in the range 1 to 31. +* @hour Store the number of hours past midnight, in the range 0 to 23. +* @min Store the number of minutes after the hour, +* in the range 0 to 59. +* @sec Store the number of seconds after the minute, normally in the +* range 0 to 59, but can be up to 60 to allow for leap seconds. +* @wday Store the number of days since Sunday, in the range 0 to 6. +* @yday Store the number of days since January 1, in the range 0 to 365. +* +* Similar to the localtime() in glibc, broken-down time is expressed +* relative to sys_tz. +*/ +static inline void localtime (__kernel_time_t totalsecs, +unsigned int *year,unsigned int *mon, +unsigned int *mday,unsigned int *hour, +unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday) +{ +__offtime (totalsecs, -sys_tz.tz_minuteswest *60 , year, mon, mday, hour, + min, sec, wday, yday); +} +/** * timespec_to_ns - Convert timespec to nanoseconds * @ts: pointer to the timespec variable to be converted
kernel/time/Makefile +1 -1
@@ -1,4 +1,4 @@ -obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o +obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o timeconv.o obj-$(CONFIG_GENERIC_CLOCKEVENTS_BUILD) += clockevents.o obj-$(CONFIG_GENERIC_CLOCKEVENTS) += tick-common.o
kernel/time/timeconv.c new file +153 -0
153 lines not on this page · raw message
Signature · 1 line
1.5.5.3
PATCH v2 2/2 Use common localtime/gmtime in fat_time_unix2fat()
In reply to ZhaoleiIt is not necessary to write custom code for convert calendar time to broken-down time.
localtime()/gmtime() is more generic to do that.
--- fs/fat/misc.c | 62 ++++++++++++++++++-------------------------------------- 1 files changed, 20 insertions(+), 42 deletions(-)
fs/fat/misc.c +20 -42
@@ -9,6 +9,7 @@#include <linux/module.h> #include <linux/fs.h> #include <linux/buffer_head.h> +#include <linux/time.h> #include "fat.h" /*
@@ -155,10 +156,6 @@ extern struct timezone sys_tz;#define SECS_PER_MIN 60#define SECS_PER_HOUR (60 * 60)#define SECS_PER_DAY (SECS_PER_HOUR * 24) -#define UNIX_SECS_1980 315532800L -#if BITS_PER_LONG ==64 -#define UNIX_SECS_2108 4354819200L -#endif /* days between 1.1.70 and 1.1.80 (2 leap days) */ #define DAYS_DELTA (365 * 10 + 2)/* 120 (2100 - 1980) isn't leap year */
@@ -211,58 +208,40 @@ void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts,void fat_time_unix2fat (struct msdos_sb_info *sbi,struct timespec *ts,__le16 *time,__le16 *date,u8 *time_cs) { -time_t second = ts->tv_sec; -time_t day, leap_day, month, year; - -if (!sbi->options.tz_utc) - second -= sys_tz.tz_minuteswest *SECS_PER_MIN ; +unsigned int year, mon, mday, hour, min, sec; +if (sbi->options.tz_utc) { +gmtime (ts->tv_sec, + &year, &mon, &mday, &hour, &min, &sec,NULL ,NULL ); + }else { +localtime (ts->tv_sec, + &year, &mon, &mday, &hour, &min, &sec,NULL ,NULL ); + } -/* Jan 1 GMT 00:00:00 1980. But what about another time zone? */ -if (second <UNIX_SECS_1980 ) { +/* FAT can only support year between 1980 to 2107 */ +if (year <1980 -1900 ) { *time =0 ; *date =cpu_to_le16 ((0 <<9 ) | (1 <<5 ) |1 );if (time_cs) *time_cs =0 ;return ; } -#if BITS_PER_LONG ==64 -if (second >=UNIX_SECS_2108 ) { +if (year >2107 -1900 ) { *time =cpu_to_le16 ((23 <<11 ) | (59 <<5 ) |29 ); *date =cpu_to_le16 ((127 <<9 ) | (12 <<5 ) |31 );if (time_cs) *time_cs =199 ;return ; } -#endif - day = second /SECS_PER_DAY -DAYS_DELTA ; - year = day /365 ; - leap_day = (year +3 ) /4 ; -if (year >YEAR_2100 )/* 2100 isn't leap year */ - leap_day--; -if (year *365 + leap_day > day) - year--; - leap_day = (year +3 ) /4 ; -if (year >YEAR_2100 )/* 2100 isn't leap year */ - leap_day--; - day -= year *365 + leap_day; +/* from 1900 -> from 1980 */ + year -=80 ; +/* 0~11 -> 1~12 */ + mon++; +/* 0~59 -> 0~29(2sec counts) */ + sec >>=1 ; -if (IS_LEAP_YEAR (year) && day == days_in_year[3 ]) { - month =2 ; - }else { -if (IS_LEAP_YEAR (year) && day > days_in_year[3 ]) - day--; -for (month =1 ; month <12 ; month++) { -if (days_in_year[month +1 ] > day) -break ; - } - } - day -= days_in_year[month]; - - *time =cpu_to_le16 (((second /SECS_PER_HOUR ) %24 ) <<11 - | ((second /SECS_PER_MIN ) %60 ) <<5 - | (second %SECS_PER_MIN ) >>1 ); - *date =cpu_to_le16 ((year <<9 ) | (month <<5 ) | (day +1 )); + *time =cpu_to_le16 (hour <<11 | min <<5 | sec); + *date =cpu_to_le16 (year <<9 | mon <<5 | mday);if (time_cs) *time_cs = (ts->tv_sec &1 ) *100 + ts->tv_nsec /10000000 ; }
@@ -283,4 +262,3 @@ int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs) }return err; } -
Signature · 1 line
1.5.5.3
Quoted 9 lines
kernel/time/timeconv.c
Quoted 9 lines
kernel/time/timeconv.c new file +8
-0+extern void gmtime (__kernel_time_t totalsecs, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday); +extern void localtime (__kernel_time_t totalsecs, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday);
Should year/mon/.../yday be passed up as a structure?
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [PATCH 1/2] Add function to convert between calendar time andbroken-down time for universal use
* From: "Pavel Machek" <[email protected]>
Quoted 11 lines
Quoted 8 lines
kernel/time/timeconv.c
Quoted 8 lines
kernel/time/timeconv.c new file +8
-0+extern void gmtime (__kernel_time_t totalsecs, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday); +extern void localtime (__kernel_time_t totalsecs, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday);Should year/mon/.../yday be passed up as a structure?
Hello, Pavel
Thanks for your attention.
Actually, I considered to introduce a struct as your think, but finally I choose to use arguments list instead of a struct, because:
1: User can easy to call this function without define a struct 2: Get rid of adding a additional struct into kernel
In fact, I think both(use a struct or not) should be ok.
Thanks
Zhaolei ÿôèº{.nÇ+‰·Ÿ®‰†+%ŠËÿ±éݶ¥Šwÿº{.nÇ+‰·¥Š{±þG«�éÿŠ{ayºÊ‡Ú™ë,j¢f£¢·hš�ï�êÿ‘êçz_è®(階ŠÝ¢j"�ú¶m§ÿÿ¾«þG«�éÿ¢¸?™¨èÚ&£ø§~�á¶iO•æ¬z·švØ^¶m§ÿÿÃÿ¶ìÿ¢¸?–I¥
Re: [PATCH 1/2] Add function to convert between calendar time andbroken-down time for universal use
Quoted 15 lines · On Mon, 20 Jul 2009 10:56:55 +0800 "Zhaolei" <[email protected]> wrote:
* From: "Pavel Machek" <[email protected]>
Quoted 11 lines
Quoted 8 lines
kernel/time/timeconv.c
Quoted 8 lines
kernel/time/timeconv.c new file +8
-0+extern void gmtime (__kernel_time_t totalsecs, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday); +extern void localtime (__kernel_time_t totalsecs, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday);Should year/mon/.../yday be passed up as a structure?
Hello, Pavel
Quoted 8 lines
Thanks for your attention.
Actually, I considered to introduce a struct as your think, but finally I choose to use arguments list instead of a struct, because:
1: User can easy to call this function without define a struct 2: Get rid of adding a additional struct into kernelIn fact, I think both(use a struct or not) should be ok.
Using a struct will generate better code at caller sites and possibly at the called site too. The compiler doesn't need to marshal those eight pointers on the stack (or wherever the architecture puts them), possibly less registers will be consumed, etc. And it'll use less stack space.
So I do think it would be better from that point of view.
However it will probably require much more code change at the call sites. But those changes will be simple, and probably a good thing regardless.
v3 cover Add function to convert between calendar time and broken-down time for universal use
Hello, Andrew
This is Patch v3.
I decide to use a struct instead of argument list to store year/mon/.../yday.
Changelog v2->v3:
Using a struct to save year/mon/.../yday.
Changelog v1->v2:
1: Fix "no tabs" error caused by email client.
2: Remove explicit 'inline' in __isleap()
3: Move DIV() and LEAPS_THRU_END_OF(y) into regular C function along with
comment
4: Move gmtime() and localtime() into header file.
Thanks
Zhaolei
PATCH v3 1/2 Add function to convert between calendar time and broken-down time for universal use
There are many similar code in kernel for one object:
convert time between calendar time and broken-down time.
Here is some source I found:fs/ncpfs/dir.cfs/smbfs/proc.cfs/fat/misc.cfs/udf/udftime.cfs/cifs/netmisc.cnet/netfilter/xt_time.cdrivers/scsi/ips.cdrivers/input/misc/hp_sdc_rtc.cdrivers/rtc/rtc-lib.carch/ia64/hp/sim/boot/fw-emu.carch/m68k/mac/misc.carch/powerpc/kernel/time.carch/parisc/include/asm/rtc.h
...
We can make a common function for this type of convert, At least we can get following benefit:
1: Make kernel simple and unify
2: Easy to fix bug in converting code
3: Reduce clone of code in future
For example, I'm trying to make ftrace display walltime, this patch will make me easy.
This code is based on code from glibc-2.6
--- include/linux/time.h | 66 +++++++++++++++++++++++ kernel/time/Makefile | 2 +- kernel/time/timeconv.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 205 insertions(+), 1 deletions(-) create mode 100644 kernel/time/timeconv.c
include/linux/time.h +66 -0
@@ -151,6 +151,72 @@ extern void update_xtime_cache(u64 nsec);struct tms ;extern void do_sys_times (struct tms *); +/* +* Similar to the struct tm in userspace <time.h>, but it needs to be here so +* that the kernel source is self contained. +*/ +struct tm { +/* +* the number of seconds after the minute, normally in the range +* 0 to 59, but can be up to 60 to allow for leap seconds +*/ +int tm_sec; +/* the number of minutes after the hour, in the range 0 to 59*/ +int tm_min; +/* the number of hours past midnight, in the range 0 to 23 */ +int tm_hour; +/* the day of the month, in the range 1 to 31 */ +int tm_mday; +/* the number of months since January, in the range 0 to 11 */ +int tm_mon; +/* the number of years since 1900 */ +int tm_year; +/* the number of days since Sunday, in the range 0 to 6 */ +int tm_wday; +/* the number of days since January 1, in the range 0 to 365 */ +int tm_yday; +}; + +extern struct tm *__offtime (__kernel_time_t totalsecs,int offset, +struct tm *result); + +/** +* gmtime_r - converts the calendar time to UTC broken-down time +* +* @totalsecs the number of seconds elapsed since 00:00:00 on January 1, 1970, +* Coordinated Universal Time (UTC). +* @result pointer to struct tm variable to receive broken-down time +* +* Return a pointer to the broken-down time result on success, +* and NULL on when the year does not fit into an integer. +* +* Similar to the gmtime_r() in glibc, broken-down time is expressed in +* Coordinated Universal Time (UTC). +*/ +static inline struct tm *gmtime_r (__kernel_time_t totalsecs,struct tm *result) +{ +return __offtime (totalsecs,0 , result); +} + +/** +* localtime_r - 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). +* @result pointer to struct tm variable to receive broken-down time +* +* Return a pointer to the broken-down time result on success, +* and NULL on when the year does not fit into an integer. +* +* Similar to the localtime_r() in glibc, broken-down time is expressed +* relative to sys_tz. +*/ +static inline struct tm *localtime_r (__kernel_time_t totalsecs, +struct tm *result) +{ +return __offtime (totalsecs, -sys_tz.tz_minuteswest *60 , result); +} +/** * timespec_to_ns - Convert timespec to nanoseconds * @ts: pointer to the timespec variable to be converted
kernel/time/Makefile +1 -1
@@ -1,4 +1,4 @@ -obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o +obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o timeconv.o obj-$(CONFIG_GENERIC_CLOCKEVENTS_BUILD) += clockevents.o obj-$(CONFIG_GENERIC_CLOCKEVENTS) += tick-common.o
kernel/time/timeconv.c new file +138 -0
138 lines not on this page · raw message
Signature · 1 line
1.5.5.3
OGAWA Hirofumi
maintainerQuoted 10 lines · Zhaolei
include/linux/time.h +10
-0+/* +* Similar to the struct tm in userspace <time.h>, but it needs to be here so +* that the kernel source is self contained. +*/ +struct tm { +/* +* the number of seconds after the minute, normally in the range +* 0 to 59, but can be up to 60 to allow for leap seconds +*/ +int tm_sec;
include/linux/time.h extern void update_xtime_cache(u64 nsec); · line 173
Quoted 10 lines
include/linux/time.h +10
-0+/* the number of minutes after the hour, in the range 0 to 59*/ +int tm_min; +/* the number of hours past midnight, in the range 0 to 23 */ +int tm_hour; +/* the day of the month, in the range 1 to 31 */ +int tm_mday; +/* the number of months since January, in the range 0 to 11 */ +int tm_mon; +/* the number of years since 1900 */ +int tm_year;
Why isn't this "long"? "int" can overflow.
include/linux/time.h extern void update_xtime_cache(u64 nsec); · line 178
Quoted 5 lines
include/linux/time.h +5
-0+/* the number of days since Sunday, in the range 0 to 6 */ +int tm_wday; +/* the number of days since January 1, in the range 0 to 365 */ +int tm_yday; +};
Those are needed?
include/linux/time.h extern void update_xtime_cache(u64 nsec); · line 181
Quoted 3 lines
include/linux/time.h +3
-0+ +extern struct tm *__offtime (__kernel_time_t totalsecs,int offset, +struct tm *result);
Why isn't time_t simply, not __kernel_time_t?
Quoted 26 lines
include/linux/time.h +26
-0+/** +* gmtime_r - converts the calendar time to UTC broken-down time +* +* @totalsecs the number of seconds elapsed since 00:00:00 on January 1, 1970, +* Coordinated Universal Time (UTC). +* @result pointer to struct tm variable to receive broken-down time +* +* Return a pointer to the broken-down time result on success, +* and NULL on when the year does not fit into an integer. +* +* Similar to the gmtime_r() in glibc, broken-down time is expressed in +* Coordinated Universal Time (UTC). +*/ +static inline struct tm *gmtime_r (__kernel_time_t totalsecs,struct tm *result) +{ +return __offtime (totalsecs,0 , result); +} + +/** +* localtime_r - 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). +* @result pointer to struct tm variable to receive broken-down time +* +* Return a pointer to the broken-down time result on success,
include/linux/time.h extern void update_xtime_cache(u64 nsec); · line 218
Quoted 10 lines
include/linux/time.h +10
-0+* and NULL on when the year does not fit into an integer. +* +* Similar to the localtime_r() in glibc, broken-down time is expressed +* relative to sys_tz. +*/ +static inline struct tm *localtime_r (__kernel_time_t totalsecs, +struct tm *result) +{ +return __offtime (totalsecs, -sys_tz.tz_minuteswest *60 , result); +}
I think those are confusable. The real function of those needs to handle timezone database. Especially, sys_tz.tz_minuteswest in localtime_r() is known as wrong.
Are you going to fix it? Otherwise I don't think it would not be good to use it easily as generic function like this.
kernel/time/timeconv.c line 37
Quoted 5 lines
kernel/time/timeconv.c new file +5
-0+/* +* Nonzero if YEAR is a leap year (every 4 years, +* except every 100th isn't, and every 400th is). +*/ +static int __isleap (unsigned int year)
long year. This breaks negative time_t.
Quoted 9 lines
kernel/time/timeconv.c new file +8
-0+{ +return (year) %4 ==0 && ((year) %100 !=0 || (year) %400 ==0 ); +} +/** +* __offtime - 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).
kernel/time/timeconv.c line 83
Quoted 10 lines
kernel/time/timeconv.c new file +10
-0+* @offset offset seconds adding to totalsecs. +* @result pointer to struct tm variable to receive broken-down time +* +* Return a pointer to the broken-down time result on success, +* and NULL on when the year does not fit into an integer. +* +* This function is for internal use, call gmtime_r() and localtime_r() instead. +*/ +struct tm *__offtime (__kernel_time_t totalsecs,int offset,struct tm *result) +{
So, I suggest to consolidate this code only, and don't provide gmtime_r()/localtime_r(), and use more good function name for __offtime() (I'm not sure, however, personally I feel __offtime is not obvious what's doing).
Thanks.
Signature · 1 line
OGAWA Hirofumi <[email protected]>
OGAWA Hirofumi
maintainer[resend: sorry if you got this twice]
Quoted 2 lines · OGAWA Hirofumi
Are you going to fix it? Otherwise I don't think it would not be good to use it easily as generic function like this.
s/I don't think/I think/
Signature · 1 line
OGAWA Hirofumi <[email protected]>
OGAWA Hirofumi
maintainerQuoted 2 lines · OGAWA Hirofumi
Are you going to fix it? Otherwise I don't think it would not be good to use it easily as generic function like this.
s/I don't think/I think/
Signature · 1 line
OGAWA Hirofumi <[email protected]>
Quoted 16 lines · OGAWA Hirofumi
Quoted 14 lines · Zhaolei
include/linux/time.h +14
-0+/* +* Similar to the struct tm in userspace <time.h>, but it needs to be here so +* that the kernel source is self contained. +*/ +struct tm { +/* +* the number of seconds after the minute, normally in the range +* 0 to 59, but can be up to 60 to allow for leap seconds +*/ +int tm_sec; +/* the number of minutes after the hour, in the range 0 to 59*/ +int tm_min; +/* the number of hours past midnight, in the range 0 to 23 */ +int tm_hour;
Quoted 8 lines
include/linux/time.h extern void update_xtime_cache(u64 nsec); · line 173
Quoted 6 lines
include/linux/time.h +6
-0+/* the day of the month, in the range 1 to 31 */ +int tm_mday; +/* the number of months since January, in the range 0 to 11 */ +int tm_mon; +/* the number of years since 1900 */ +int tm_year;Why isn't this "long"? "int" can overflow.
Hello, OGAWA-san:
Thanks for your review.
I selected int to make it same with user-space struct tm.
In 32-bit machine, long have same length with int, and still can overflow, It we want to avoid it in all platform, we should use long long, and make division complex.
Maybe make it same with user-space struct tm is ok.
Quoted 7 lines
include/linux/time.h extern void update_xtime_cache(u64 nsec); · line 178
Quoted 5 lines
include/linux/time.h +5
-0+/* the number of days since Sunday, in the range 0 to 6 */ +int tm_wday; +/* the number of days since January 1, in the range 0 to 365 */ +int tm_yday; +};Those are needed?
Those are not needed by FAT-fs's code, but as a library, I keep them for generic use and keep same with user-space struct tm.
Quoted 5 lines
include/linux/time.h extern void update_xtime_cache(u64 nsec); · line 181
Quoted 3 lines
include/linux/time.h +3
-0+ +extern struct tm *__offtime (__kernel_time_t totalsecs,int offset, +struct tm *result);Why isn't
time_tsimply, not__kernel_time_t?
Yes, thanks.
It should be time_t.
I will fix it.
Quoted 41 lines
Quoted 26 lines
include/linux/time.h +26
-0+/** +* gmtime_r - converts the calendar time to UTC broken-down time +* +* @totalsecs the number of seconds elapsed since 00:00:00 on January 1, 1970, +* Coordinated Universal Time (UTC). +* @result pointer to struct tm variable to receive broken-down time +* +* Return a pointer to the broken-down time result on success, +* and NULL on when the year does not fit into an integer. +* +* Similar to the gmtime_r() in glibc, broken-down time is expressed in +* Coordinated Universal Time (UTC). +*/ +static inline struct tm *gmtime_r (__kernel_time_t totalsecs,struct tm *result) +{ +return __offtime (totalsecs,0 , result); +} + +/** +* localtime_r - 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). +* @result pointer to struct tm variable to receive broken-down time +* +* Return a pointer to the broken-down time result on success, include/linux/time.h extern void update_xtime_cache(u64 nsec); · line 218
Quoted 10 lines
include/linux/time.h +10
-0+* and NULL on when the year does not fit into an integer. +* +* Similar to the localtime_r() in glibc, broken-down time is expressed +* relative to sys_tz. +*/ +static inline struct tm *localtime_r (__kernel_time_t totalsecs, +struct tm *result) +{ +return __offtime (totalsecs, -sys_tz.tz_minuteswest *60 , result); +}I think those are confusable. The real function of those needs to handle timezone database. Especially, sys_tz.tz_minuteswest in
localtime_r()is known as wrong.
Quoted 2 lines
Are you going to fix it? Otherwise I don't think it would not be good to use it easily as generic function like this.
Actually, it is hard to select.
I don't schedule to introduce complex timezone database into kernel, but as you said, localtime() without timezone database is not complete. But localtime_r is easy to use and understood, it is similar with userspace same-name function...
Quoted 24 lines
kernel/time/timeconv.c line 37
Quoted 5 lines
kernel/time/timeconv.c new file +5
-0+/* +* Nonzero if YEAR is a leap year (every 4 years, +* except every 100th isn't, and every 400th is). +*/ +static int __isleap (unsigned int year)long year. This breaks negative
time_t.Quoted 16 lines
kernel/time/timeconv.c new file +15
-0+{ +return (year) %4 ==0 && ((year) %100 !=0 || (year) %400 ==0 ); +} +/** +* __offtime - 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 +* +* Return a pointer to the broken-down time result on success, +* and NULL on when the year does not fit into an integer. +* +* This function is for internal use, call gmtime_r() and localtime_r() instead.
Quoted 8 lines
kernel/time/timeconv.c line 83
Quoted 3 lines
+ */
kernel/time/timeconv.c new file +2
-0+struct tm *__offtime (__kernel_time_t totalsecs,int offset,struct tm *result) +{So, I suggest to consolidate this code only, and don't provide gmtime_r()/localtime_r(), and use more good function name for
__offtime()(I'm not sure, however, personally I feel__offtimeis not obvious what's doing).
What about just use gmtime_r(rename __offtime->gmtime_r)?
In fact I think both way(hode original localtime/gmtime or delete them) have merit and demerit.
Hode them will make it easy to use, delete them will make function more exact.
Hi, Andrew
What is your opinion?
Thanks
Zhaolei
OGAWA Hirofumi
maintainerQuoted 5 lines · Zhaolei
Quoted 4 lines
include/linux/time.h extern void update_xtime_cache(u64 nsec); · line 173
Quoted 2 lines
include/linux/time.h +2
-0+/* the number of years since 1900 */ +int tm_year;Why isn't this "long"? "int" can overflow.
Quoted 7 lines
I selected int to make it same with user-space struct tm.
In 32-bit machine, long have same length with int, and still can overflow, It we want to avoid it in all platform, we should use long long, and make division complex.
Maybe make it same with user-space struct tm is ok.
time_t is also "long" on 32-bit machine, it does overflow?
Quoted 10 lines
Quoted 7 lines
include/linux/time.h extern void update_xtime_cache(u64 nsec); · line 178
Quoted 5 lines
include/linux/time.h +5
-0+/* the number of days since Sunday, in the range 0 to 6 */ +int tm_wday; +/* the number of days since January 1, in the range 0 to 365 */ +int tm_yday; +};Those are needed?
Those are not needed by FAT-fs's code, but as a library, I keep them for generic use and keep same with user-space struct tm.
Yes. But, if there is no users, why need? I guess it makes slow thing, and it is slow than FAT's one (because FAT's one is optimized for FAT's timestamp range more or less).
Quoted 13 lines
Quoted 10 lines
include/linux/time.h extern void update_xtime_cache(u64 nsec); · line 218
Quoted 5 lines
include/linux/time.h +5
-0+static inline struct tm *localtime_r (__kernel_time_t totalsecs, +struct tm *result) +{ +return __offtime (totalsecs, -sys_tz.tz_minuteswest *60 , result); +}I think those are confusable. The real function of those needs to handle timezone database. Especially, sys_tz.tz_minuteswest in
localtime_r()is known as wrong.Quoted 2 lines
Are you going to fix it? Otherwise I don't think it would not be good to use it easily as generic function like this.
Quoted 6 lines
Actually, it is hard to select.
I don't schedule to introduce complex timezone database into kernel, but as you said,
localtime()without timezone database is not complete. Butlocaltime_ris easy to use and understood, it is similar with userspace same-name function...
Actually it is both (gmtime() is also needed to handle timezone).
I worry someone uses it and display/export to userland. After that, the user will report the bug of it.
Quoted 7 lines
Quoted 7 lines
kernel/time/timeconv.c line 37
Quoted 5 lines
kernel/time/timeconv.c new file +5
-0+/* +* Nonzero if YEAR is a leap year (every 4 years, +* except every 100th isn't, and every 400th is). +*/ +static int __isleap (unsigned int year)long year. This breaks negative
time_t.
Please "long year".
Quoted 9 lines
Quoted 7 lines
kernel/time/timeconv.c line 83
Quoted 2 lines
kernel/time/timeconv.c new file +2
-0+struct tm *__offtime (__kernel_time_t totalsecs,int offset,struct tm *result) +{So, I suggest to consolidate this code only, and don't provide gmtime_r()/localtime_r(), and use more good function name for
__offtime()(I'm not sure, however, personally I feel__offtimeis not obvious what's doing).What about just use
gmtime_r(rename __offtime->gmtime_r)?
gmtime() also need to handle timezone actually.
Quoted 3 lines
In fact I think both way(hode original localtime/gmtime or delete them) have merit and demerit.
Hode them will make it easy to use, delete them will make function more exact.
Yes. But, how do you handle bug report?
Thanks.
Signature · 1 line
OGAWA Hirofumi <[email protected]>
Quoted 7 lines · OGAWA Hirofumi
Quoted 5 lines · Zhaolei
Quoted 3 lines
include/linux/time.h extern void update_xtime_cache(u64 nsec); · line 173
Quoted 2 lines
include/linux/time.h +2
-0+/* the number of years since 1900 */ +int tm_year;Why isn't this "long"? "int" can overflow.
I selected int to make it same with user-space struct tm.
Quoted 7 lines
Quoted 5 lines
In 32-bit machine, long have same length with int, and still can overflow, It we want to avoid it in all platform, we should use long long, and make division complex.
Maybe make it same with user-space struct tm is ok.
time_tis also "long" on 32-bit machine, it does overflow?
Hello, Ogawa-san:
CC: Andrew, ingo:
Yes, time_t on 32-bit machine will overflow on year-2038. So tm.tm_year will NEVER overflow on 32-bit machine.
And in 64-bit machine, int type of tm.tm_year will overflow in year 2,147,485,548, Is that enough?
I also like your suggestion to use long for year, so gmtime/localtime will never return false on both 32 and 64 bit machine.
(btw, I don't understand why time_t is long, it have different length(limit) in different arch, and make disunity)
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
include/linux/time.h extern void update_xtime_cache(u64 nsec); · line 178
Quoted 5 lines
include/linux/time.h +5
-0+/* the number of days since Sunday, in the range 0 to 6 */ +int tm_wday; +/* the number of days since January 1, in the range 0 to 365 */ +int tm_yday; +};Those are needed?
Quoted 6 lines
Quoted 2 lines
Those are not needed by FAT-fs's code, but as a library, I keep them for generic use and keep same with user-space struct tm.
Yes. But, if there is no users, why need?
I guess it makes slow thing, and it is slow than FAT's one (because FAT's one is optimized for FAT's timestamp range more or less).
There is no users of printk("%pf"), why not remove this? And at least, I found one: drivers/char/efirtc.c If I continue searching, maybe more.
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
include/linux/time.h extern void update_xtime_cache(u64 nsec); · line 218
Quoted 5 lines
include/linux/time.h +5
-0+static inline struct tm *localtime_r (__kernel_time_t totalsecs, +struct tm *result) +{ +return __offtime (totalsecs, -sys_tz.tz_minuteswest *60 , result); +}I think those are confusable. The real function of those needs to handle timezone database. Especially, sys_tz.tz_minuteswest in
localtime_r()is known as wrong.Quoted 8 lines
Quoted 2 lines
Are you going to fix it? Otherwise I don't think it would not be good to use it easily as generic function like this.
Actually, it is hard to select.
I don't schedule to introduce complex timezone database into kernel, but as you said,
localtime()without timezone database is not complete. Butlocaltime_ris easy to use and understood, it is similar with userspace same-name function...
Quoted 4 lines
Actually it is both (
gmtime()is also needed to handle timezone).I worry someone uses it and display/export to userland. After that, the user will report the bug of it.
IHMO, user of these functions should understand what these functions is and use these functions for right way.
If we give user a cdrom, it is user's responsibility not using is as cup stand.
At least, there function can make following source a fairy:fs/ncpfs/dir.cfs/smbfs/proc.cfs/fat/misc.cfs/udf/udftime.cfs/cifs/netmisc.cnet/netfilter/xt_time.cdrivers/scsi/ips.c
...
It is different with user-land just because it lakes of large-complex locale database.
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
kernel/time/timeconv.c line 37
Quoted 5 lines
kernel/time/timeconv.c new file +5
-0+/* +* Nonzero if YEAR is a leap year (every 4 years, +* except every 100th isn't, and every 400th is). +*/ +static int __isleap (unsigned int year)long year. This breaks negative
time_t.Please "long year".
Ok, or int year(after we get conclusion of long/int).
Quoted 9 lines
Quoted 7 lines
Quoted 6 lines
kernel/time/timeconv.c line 83
Quoted 2 lines
kernel/time/timeconv.c new file +2
-0+struct tm *__offtime (__kernel_time_t totalsecs,int offset,struct tm *result) +{So, I suggest to consolidate this code only, and don't provide gmtime_r()/localtime_r(), and use more good function name for
__offtime()(I'm not sure, however, personally I feel__offtimeis not obvious what's doing).What about just use
gmtime_r(rename __offtime->gmtime_r)?
gmtime()also need to handle timezone actually.
So, maybe another function name as unmktime?
Quoted 5 lines
Quoted 3 lines
In fact I think both way(hode original localtime/gmtime or delete them) have merit and demerit.
Hode them will make it easy to use, delete them will make function more exact.Yes. But, how do you handle bug report?
I will thanks you for attention and reporting first, then discuss on LKML about detail of this problem, and get a conclusion and fix.
Now we are in step2: discussing.
We need to get a best way to fix this to make users happy.
Thanks
Zhaolei
OGAWA Hirofumi
maintainerQuoted 1 line · Zhaolei
There is no users of
printk("%pf"), why not remove this?
IMO, because it doesn't make slow thing.
Quoted 2 lines
And at least, I found one:
drivers/char/efirtc.c
If I continue searching, maybe more.
If there is a user, I think it's ok.
Quoted 14 lines
IHMO, user of these functions should understand what these functions is and use these functions for right way.
If we give user a cdrom, it is user's responsibility not using is as cup stand.At least, there function can make following source a fairy:
fs/ncpfs/dir.cfs/smbfs/proc.cfs/fat/misc.cfs/udf/udftime.cfs/cifs/netmisc.cnet/netfilter/xt_time.cdrivers/scsi/ips.c
...
Quoted 2 lines
It is different with user-land just because it lakes of large-complex locale database.
Yes, and I'm understanding FAT has the bug, and there is actual bug report (bug FAT requires this as on-disk format). However, my point is why we increase the known bugs.
Quoted 3 lines
Quoted 1 line
gmtime()also need to handle timezone actually.So, maybe another function name as unmktime?
Maybe. Um..., or time_to_tm()? I'm not sure.
Thanks.
Signature · 1 line
OGAWA Hirofumi <[email protected]>
v4 cover Add function to convert between calendar time and broken-down time for universal use
Hello, Andrew
This is Patch v4.
I updated this patch bases on Ogawa-san's suggestion.
Changelog v3->v4:
1: Use long instead of int for tm.tm_year to avoid overflow 2: Remove localtime() and gmtime() because they can't support timezone
database as user-more. And rename __offtime() to time_to_tm()
3: Use time_t instead of __kernel_time_t as function's argument type 4: __isleap(unsigned int year)->__isleap(long year) to avoid overflow
Changelog v2->v3:
Using a struct to save year/mon/.../yday.
Changelog v1->v2:
1: Fix "no tabs" error caused by email client.
2: Remove explicit 'inline' in __isleap()
3: Move DIV() and LEAPS_THRU_END_OF(y) into regular C function along with
comment
4: Move gmtime() and localtime() into header file.
Thanks
Zhaolei
PATCH v4 1/2 Add function to convert between calendar time and broken-down time for universal use
There are many similar code in kernel for one object:
convert time between calendar time and broken-down time.
Here is some source I found:fs/ncpfs/dir.cfs/smbfs/proc.cfs/fat/misc.cfs/udf/udftime.cfs/cifs/netmisc.cnet/netfilter/xt_time.cdrivers/scsi/ips.cdrivers/input/misc/hp_sdc_rtc.cdrivers/rtc/rtc-lib.carch/ia64/hp/sim/boot/fw-emu.carch/m68k/mac/misc.carch/powerpc/kernel/time.carch/parisc/include/asm/rtc.h
...
We can make a common function for this type of convert, At least we can get following benefit:
1: Make kernel simple and unify
2: Easy to fix bug in converting code
3: Reduce clone of code in future
For example, I'm trying to make ftrace display walltime, this patch will make me easy.
This code is based on code from glibc-2.6
--- include/linux/time.h | 28 +++++++++++ kernel/time/Makefile | 2 +- kernel/time/timeconv.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 156 insertions(+), 1 deletions(-) create mode 100644 kernel/time/timeconv.c
include/linux/time.h +28 -0
@@ -151,6 +151,34 @@ extern void update_xtime_cache(u64 nsec);struct tms ;extern void do_sys_times (struct tms *); +/* +* Similar to the struct tm in userspace <time.h>, but it needs to be here so +* that the kernel source is self contained. +*/ +struct tm { +/* +* the number of seconds after the minute, normally in the range +* 0 to 59, but can be up to 60 to allow for leap seconds +*/ +int tm_sec; +/* the number of minutes after the hour, in the range 0 to 59*/ +int tm_min; +/* the number of hours past midnight, in the range 0 to 23 */ +int tm_hour; +/* the day of the month, in the range 1 to 31 */ +int tm_mday; +/* the number of months since January, in the range 0 to 11 */ +int tm_mon; +/* the number of years since 1900 */ +long tm_year; +/* the number of days since Sunday, in the range 0 to 6 */ +int tm_wday; +/* the number of days since January 1, in the range 0 to 365 */ +int tm_yday; +}; + +void time_to_tm (time_t totalsecs,int offset,struct tm *result); +/** * timespec_to_ns - Convert timespec to nanoseconds * @ts: pointer to the timespec variable to be converted
kernel/time/Makefile +1 -1
@@ -1,4 +1,4 @@ -obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o +obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o timeconv.o obj-$(CONFIG_GENERIC_CLOCKEVENTS_BUILD) += clockevents.o obj-$(CONFIG_GENERIC_CLOCKEVENTS) += tick-common.o
kernel/time/timeconv.c new file +127 -0
127 lines not on this page · raw message
Signature · 1 line
1.5.5.3
PATCH v4 2/2 Use common time_to_tm in fat_time_unix2fat()
In reply to ZhaoleiIt is not necessary to write custom code for convert calendar time to broken-down time.time_to_tm() is more generic to do that.
--- fs/fat/misc.c | 57 +++++++++++++++------------------------------------------ 1 files changed, 15 insertions(+), 42 deletions(-)
fs/fat/misc.c +15 -42
@@ -9,6 +9,7 @@#include <linux/module.h> #include <linux/fs.h> #include <linux/buffer_head.h> +#include <linux/time.h> #include "fat.h" /*
@@ -155,10 +156,6 @@ extern struct timezone sys_tz;#define SECS_PER_MIN 60#define SECS_PER_HOUR (60 * 60)#define SECS_PER_DAY (SECS_PER_HOUR * 24) -#define UNIX_SECS_1980 315532800L -#if BITS_PER_LONG ==64 -#define UNIX_SECS_2108 4354819200L -#endif /* days between 1.1.70 and 1.1.80 (2 leap days) */ #define DAYS_DELTA (365 * 10 + 2)/* 120 (2100 - 1980) isn't leap year */
@@ -211,58 +208,35 @@ void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts,void fat_time_unix2fat (struct msdos_sb_info *sbi,struct timespec *ts,__le16 *time,__le16 *date,u8 *time_cs) { -time_t second = ts->tv_sec; -time_t day, leap_day, month, year; +struct tm tm; +time_to_tm (ts->tv_sec, sbi->options.tz_utc ?0 : + -sys_tz.tz_minuteswest *60 , &tm); -if (!sbi->options.tz_utc) - second -= sys_tz.tz_minuteswest *SECS_PER_MIN ; - -/* Jan 1 GMT 00:00:00 1980. But what about another time zone? */ -if (second <UNIX_SECS_1980 ) { +/* FAT can only support year between 1980 to 2107 */ +if (tm.tm_year <1980 -1900 ) { *time =0 ; *date =cpu_to_le16 ((0 <<9 ) | (1 <<5 ) |1 );if (time_cs) *time_cs =0 ;return ; } -#if BITS_PER_LONG ==64 -if (second >=UNIX_SECS_2108 ) { +if (tm.tm_year >2107 -1900 ) { *time =cpu_to_le16 ((23 <<11 ) | (59 <<5 ) |29 ); *date =cpu_to_le16 ((127 <<9 ) | (12 <<5 ) |31 );if (time_cs) *time_cs =199 ;return ; } -#endif - day = second /SECS_PER_DAY -DAYS_DELTA ; - year = day /365 ; - leap_day = (year +3 ) /4 ; -if (year >YEAR_2100 )/* 2100 isn't leap year */ - leap_day--; -if (year *365 + leap_day > day) - year--; - leap_day = (year +3 ) /4 ; -if (year >YEAR_2100 )/* 2100 isn't leap year */ - leap_day--; - day -= year *365 + leap_day; - -if (IS_LEAP_YEAR (year) && day == days_in_year[3 ]) { - month =2 ; - }else { -if (IS_LEAP_YEAR (year) && day > days_in_year[3 ]) - day--; -for (month =1 ; month <12 ; month++) { -if (days_in_year[month +1 ] > day) -break ; - } - } - day -= days_in_year[month]; +/* from 1900 -> from 1980 */ + tm.tm_year -=80 ; +/* 0~11 -> 1~12 */ + tm.tm_mon++; +/* 0~59 -> 0~29(2sec counts) */ + tm.tm_sec >>=1 ; - *time =cpu_to_le16 (((second /SECS_PER_HOUR ) %24 ) <<11 - | ((second /SECS_PER_MIN ) %60 ) <<5 - | (second %SECS_PER_MIN ) >>1 ); - *date =cpu_to_le16 ((year <<9 ) | (month <<5 ) | (day +1 )); + *time =cpu_to_le16 (tm.tm_hour <<11 | tm.tm_min <<5 | tm.tm_sec); + *date =cpu_to_le16 (tm.tm_year <<9 | tm.tm_mon <<5 | tm.tm_mday);if (time_cs) *time_cs = (ts->tv_sec &1 ) *100 + ts->tv_nsec /10000000 ; }
@@ -283,4 +257,3 @@ int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs) }return err; } -
Signature · 1 line
1.5.5.3
Quoted 16 lines · OGAWA Hirofumi
Quoted 14 lines · Zhaolei
include/linux/time.h +14
-0+/* +* Similar to the struct tm in userspace <time.h>, but it needs to be here so +* that the kernel source is self contained. +*/ +struct tm { +/* +* the number of seconds after the minute, normally in the range +* 0 to 59, but can be up to 60 to allow for leap seconds +*/ +int tm_sec; +/* the number of minutes after the hour, in the range 0 to 59*/ +int tm_min; +/* the number of hours past midnight, in the range 0 to 23 */ +int tm_hour;
Quoted 8 lines
include/linux/time.h extern void update_xtime_cache(u64 nsec); · line 173
Quoted 6 lines
include/linux/time.h +6
-0+/* the day of the month, in the range 1 to 31 */ +int tm_mday; +/* the number of months since January, in the range 0 to 11 */ +int tm_mon; +/* the number of years since 1900 */ +int tm_year;Why isn't this "long"? "int" can overflow.
Overflow? Year?
I'm not sure how far ahead you are looking, but support for year 2000000000 (2e9) seems perfectly okay to me. (Our friendly star here, sun, is going to die at cca year 5e9, for comparison. )
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
OGAWA Hirofumi
maintainerQuoted 7 lines · Pavel Machek
Quoted 1 line
Why isn't this "long"? "int" can overflow.
Overflow? Year?
I'm not sure how far ahead you are looking, but support for year 2000000000 (2e9) seems perfectly okay to me. (Our friendly star here, sun, is going to die at cca year 5e9, for comparison. )
Yes. If source is guaranteed as only xtime, but I'd like to think all case. Especially, because this is library.
Thanks.
Signature · 1 line
OGAWA Hirofumi <[email protected]>
PATCH v3 2/2 Use common localtime/gmtime in fat_time_unix2fat()
In reply to ZhaoleiIt is not necessary to write custom code for convert calendar time to broken-down time.
localtime()/gmtime() is more generic to do that.
--- fs/fat/misc.c | 60 +++++++++++++++++--------------------------------------- 1 files changed, 18 insertions(+), 42 deletions(-)
fs/fat/misc.c +18 -42
@@ -9,6 +9,7 @@#include <linux/module.h> #include <linux/fs.h> #include <linux/buffer_head.h> +#include <linux/time.h> #include "fat.h" /*
@@ -155,10 +156,6 @@ extern struct timezone sys_tz;#define SECS_PER_MIN 60#define SECS_PER_HOUR (60 * 60)#define SECS_PER_DAY (SECS_PER_HOUR * 24) -#define UNIX_SECS_1980 315532800L -#if BITS_PER_LONG ==64 -#define UNIX_SECS_2108 4354819200L -#endif /* days between 1.1.70 and 1.1.80 (2 leap days) */ #define DAYS_DELTA (365 * 10 + 2)/* 120 (2100 - 1980) isn't leap year */
@@ -211,58 +208,38 @@ void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts,void fat_time_unix2fat (struct msdos_sb_info *sbi,struct timespec *ts,__le16 *time,__le16 *date,u8 *time_cs) { -time_t second = ts->tv_sec; -time_t day, leap_day, month, year; - -if (!sbi->options.tz_utc) - second -= sys_tz.tz_minuteswest *SECS_PER_MIN ; +struct tm tm; +if (sbi->options.tz_utc) { +gmtime_r (ts->tv_sec, &tm); + }else { +localtime_r (ts->tv_sec, &tm); + } -/* Jan 1 GMT 00:00:00 1980. But what about another time zone? */ -if (second <UNIX_SECS_1980 ) { +/* FAT can only support year between 1980 to 2107 */ +if (tm.tm_year <1980 -1900 ) { *time =0 ; *date =cpu_to_le16 ((0 <<9 ) | (1 <<5 ) |1 );if (time_cs) *time_cs =0 ;return ; } -#if BITS_PER_LONG ==64 -if (second >=UNIX_SECS_2108 ) { +if (tm.tm_year >2107 -1900 ) { *time =cpu_to_le16 ((23 <<11 ) | (59 <<5 ) |29 ); *date =cpu_to_le16 ((127 <<9 ) | (12 <<5 ) |31 );if (time_cs) *time_cs =199 ;return ; } -#endif - day = second /SECS_PER_DAY -DAYS_DELTA ; - year = day /365 ; - leap_day = (year +3 ) /4 ; -if (year >YEAR_2100 )/* 2100 isn't leap year */ - leap_day--; -if (year *365 + leap_day > day) - year--; - leap_day = (year +3 ) /4 ; -if (year >YEAR_2100 )/* 2100 isn't leap year */ - leap_day--; - day -= year *365 + leap_day; +/* from 1900 -> from 1980 */ + tm.tm_year -=80 ; +/* 0~11 -> 1~12 */ + tm.tm_mon++; +/* 0~59 -> 0~29(2sec counts) */ + tm.tm_sec >>=1 ; -if (IS_LEAP_YEAR (year) && day == days_in_year[3 ]) { - month =2 ; - }else { -if (IS_LEAP_YEAR (year) && day > days_in_year[3 ]) - day--; -for (month =1 ; month <12 ; month++) { -if (days_in_year[month +1 ] > day) -break ; - } - } - day -= days_in_year[month]; - - *time =cpu_to_le16 (((second /SECS_PER_HOUR ) %24 ) <<11 - | ((second /SECS_PER_MIN ) %60 ) <<5 - | (second %SECS_PER_MIN ) >>1 ); - *date =cpu_to_le16 ((year <<9 ) | (month <<5 ) | (day +1 )); + *time =cpu_to_le16 (tm.tm_hour <<11 | tm.tm_min <<5 | tm.tm_sec); + *date =cpu_to_le16 (tm.tm_year <<9 | tm.tm_mon <<5 | tm.tm_mday);if (time_cs) *time_cs = (ts->tv_sec &1 ) *100 + ts->tv_nsec /10000000 ; }
@@ -283,4 +260,3 @@ int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs) }return err; } -
Signature · 1 line
1.5.5.3
Re: [PATCH v3 2/2] Use common localtime/gmtime in fat_time_unix2fat()
Quoted 5 lines · Zhaolei
It is not necessary to write custom code for convert calendar time to broken-down time.
localtime()/gmtime() is more generic to do that.Signed-off-by Zhao Lei <[email protected]>
Ack.
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
OGAWA Hirofumi
maintainerRe: [PATCH v3 2/2] Use common localtime/gmtime in fat_time_unix2fat()
In reply to Zhaoleifs/fat/misc.c void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts, · line 216
Quoted 5 lines · Zhaolei
fs/fat/misc.c +5
-0+if (sbi->options.tz_utc) { +gmtime_r (ts->tv_sec, &tm); + }else { +localtime_r (ts->tv_sec, &tm); + }
Missing error check.
Quoted 10 lines
fs/fat/misc.c +2
-2-/* Jan 1 GMT 00:00:00 1980. But what about another time zone? */ -if (second <UNIX_SECS_1980 ) { +/* FAT can only support year between 1980 to 2107 */ +if (tm.tm_year <1980 -1900 ) { *time =0 ; *date =cpu_to_le16 ((0 <<9 ) | (1 <<5 ) |1 );if (time_cs) *time_cs =0 ;return ; }
fs/fat/misc.c void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts, · line 236
Quoted 10 lines
fs/fat/misc.c +1
-3-#if BITS_PER_LONG ==64 -if (second >=UNIX_SECS_2108 ) { +if (tm.tm_year >2107 -1900 ) { *time =cpu_to_le16 ((23 <<11 ) | (59 <<5 ) |29 ); *date =cpu_to_le16 ((127 <<9 ) | (12 <<5 ) |31 );if (time_cs) *time_cs =199 ;return ; } -#endif
I think tm.tm_year is undefine in case of the error.
Thanks.
Signature · 1 line
OGAWA Hirofumi <[email protected]>
Re: Re: [PATCH v3 2/2] Use common localtime/gmtime in fat_time_unix2fat()
Quoted 24 lines · OGAWA Hirofumi
fs/fat/misc.c void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts, · line 216
Quoted 5 lines · Zhaolei
fs/fat/misc.c +5
-0+if (sbi->options.tz_utc) { +gmtime_r (ts->tv_sec, &tm); + }else { +localtime_r (ts->tv_sec, &tm); + }Missing error check.
Quoted 14 lines
fs/fat/misc.c +3
-4-/* Jan 1 GMT 00:00:00 1980. But what about another time zone? */ -if (second <UNIX_SECS_1980 ) { +/* FAT can only support year between 1980 to 2107 */ +if (tm.tm_year <1980 -1900 ) { *time =0 ; *date =cpu_to_le16 ((0 <<9 ) | (1 <<5 ) |1 );if (time_cs) *time_cs =0 ;return ; } -#if BITS_PER_LONG ==64 -if (second >=UNIX_SECS_2108 ) { +if (tm.tm_year >2107 -1900 ) { *time =cpu_to_le16 ((23 <<11 ) | (59 <<5 ) |29 );
Quoted 8 lines
fs/fat/misc.c void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec *ts, · line 236
Quoted 6 lines
fs/fat/misc.c +0
-1*date =cpu_to_le16 ((127 <<9 ) | (12 <<5 ) |31 );if (time_cs) *time_cs =199 ;return ; } -#endif I think tm.tm_year is undefine in case of the error.
Hello, Ogawa-san:
Thanks for your review.
I'll fix them
Thanks
Zhaolei
Quoted 191 lines · Zhaolei
kernel/time/timeconv.c new file +2
-0@@ -0,0 +1,180 @@ +/* +* Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
+ * This file is part of the GNU C Library.
+ * Contributed by Paul Eggert ([email protected]).
+ *
+ * The GNU C Library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * The GNU C Library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with the GNU C Library; see the file COPYING.LIB. If not,
+ * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */kernel/time/timeconv.c new file +108
-0+ +/* +* Converts the calendar time to broken-down time representation +* Based on code from glibc-2.6 +* +* 2009-7-14: +* Moved from glibc-2.6 to kernel by Zhaolei<[email protected]> +*/ + +#include <linux/time.h> +#include <linux/module.h> + +/* +* Nonzero if YEAR is a leap year (every 4 years, +* except every 100th isn't, and every 400th is). +*/ +static inline int __isleap (unsigned int year) +{ +return (year) %4 ==0 && ((year) %100 !=0 || (year) %400 ==0 ); +} + +/* How many days come before each month (0-12). */ +static const unsigned short __mon_yday[2 ][13 ] = + { +/* Normal years. */ + {0 ,31 ,59 ,90 ,120 ,151 ,181 ,212 ,243 ,273 ,304 ,334 ,365 }, +/* Leap years. */ + {0 ,31 ,60 ,91 ,121 ,152 ,182 ,213 ,244 ,274 ,305 ,335 ,366 } + }; + +#define SECS_PER_HOUR (60 * 60) +#define SECS_PER_DAY (SECS_PER_HOUR * 24) + +static void __offtime (__kernel_time_t totalsecs,int offset, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday) +{ +long days, rem, y; +const unsigned short *ip; + + days = totalsecs /SECS_PER_DAY ; + rem = totalsecs %SECS_PER_DAY ; + rem += offset; +while (rem <0 ) { + rem +=SECS_PER_DAY ; + --days; + } +while (rem >=SECS_PER_DAY ) { + rem -=SECS_PER_DAY ; + ++days; + } + +if (hour) + *hour = rem /SECS_PER_HOUR ; + rem %=SECS_PER_HOUR ; +if (min) + *min = rem /60 ; +if (sec) + *sec = rem %60 ; + +if (wday) { +/* January 1, 1970 was a Thursday. */ + *wday = (4 + days) %7 ; +if (*wday <0 ) + *wday +=7 ; + } + + y =1970 ; + +#define DIV (a, b) ((a) / (b) - ((a) % (b) < 0)) +#define LEAPS_THRU_END_OF (y) (DIV(y, 4) - DIV(y, 100) + DIV(y, 400)) + +while (days <0 || days >= (__isleap (y) ?366 :365 )) { +/* Guess a corrected year, assuming 365 days per year. */ +long yg = y + days /365 - (days %365 <0 ); + +/* Adjust DAYS and Y to match the guessed year. */ + days -= ((yg - y) *365 + +LEAPS_THRU_END_OF (yg -1 ) + -LEAPS_THRU_END_OF (y -1 )); + y = yg; + } +if (year) { + *year = y -1900 ; +if (*year != y -1900 ) { +/* The year cannot be represented due to overflow. */ + *year =-1 ; + } + } + +if (yday) + *yday = days; + + ip = __mon_yday[__isleap (y)]; +for (y =11 ; days < ip[y]; y--) +continue ; + days -= ip[y]; +if (mon) + *mon = y; +if (mday) + *mday = days +1 ; +} + +/** +* gmtime - converts the calendar time to UTC broken-down time +* +* @totalsecs The number of seconds elapsed since 00:00:00 on January 1, 1970,
+ * Coordinated Universal Time (UTC).
+ * @year Store the number of years since 1900.
+ * @mon Store the number of months since January, in the range 0
to 11.
+ * @mday Store the day of the month, in the range 1 to 31.
+ * @hour Store the number of hours past midnight, in the range 0 to 23.
+ * @min Store the number of minutes after the hour,
+ * in the range 0 to 59.
+ * @sec Store the number of seconds after the minute, normally
in the
+ * range 0 to 59, but can be up to 60 to allow for leap seconds.
+ * @wday Store the number of days since Sunday, in the range 0 to 6.
+ * @yday Store the number of days since January 1, in the range 0 to
365.
+ *
+ * Similar to thegmtime()in glibc, broken-down time is expressed in
+ * Coordinated Universal Time (UTC).
+ */kernel/time/timeconv.c new file +13
-0+void gmtime (__kernel_time_t totalsecs, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday) +{ +__offtime (totalsecs,0 , year, mon, mday, hour, min, sec, wday, yday); +} +EXPORT_SYMBOL (gmtime); + +/** +* localtime - 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).
+ * @year Store the number of years since 1900.
+ * @mon Store the number of months since January, in the range 0
to 11.
+ * @mday Store the day of the month, in the range 1 to 31.
+ * @hour Store the number of hours past midnight, in the range 0 to 23.
+ * @min Store the number of minutes after the hour,
+ * in the range 0 to 59.
+ * @sec Store the number of seconds after the minute, normally
in the
+ * range 0 to 59, but can be up to 60 to allow for leap seconds.
+ * @wday Store the number of days since Sunday, in the range 0 to 6.
+ * @yday Store the number of days since January 1, in the range 0 to
365.
+ *
+ * Similar to thelocaltime()in glibc, broken-down time is expressed
+ * relative tosys_tz.
+ */kernel/time/timeconv.c new file +6
-0+void localtime (__kernel_time_t totalsecs, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday) +{ + __offtime(totalsecs, -sys_tz.tz_minuteswest * 60, year, mon, mday,hour,
kernel/time/timeconv.c line 180
Quoted 3 lines
kernel/time/timeconv.c new file +3
-0+ min, sec, wday, yday); +} +EXPORT_SYMBOL (localtime);
Makes sense - but this code should be converted to the kernel coding style. See other code in kernel/time/*.c for examples.
I'm also wondering whether including LGPL glibc code in the kernel like this is license-appropriate - there seems to be few precedents of it. It _should_ be OK, but i've Cc:-ed involved folks just to make sure everyone agrees ...
Since you'll modify it anyway to match up the style, the license itself could be restricted to the kernel's GPLv2 subset, that's OK as per LGPL.
Ingo
Quoted 3 lines · Ingo Molnar
I'm also wondering whether including LGPL glibc code in the kernel like this is license-appropriate - there seems to be few precedents of it. It _should_ be OK, but i've Cc:-ed involved folks just to make sure everyone agrees ...
Doesn't seem any more odd to me than BSD/GPL dual-licensing, which we already have tons of.
-hpa
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Quoted 2 lines · Ingo Molnar
I'm also wondering whether including LGPL glibc code in the kernel like this is license-appropriate
You can declare the modified code GPLed, no problem.
- --
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖ -----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
iEYEARECAAYFAkphwuUACgkQ2ijCOnn/RHTUFACcD0BZA8AakHERAjnkFOAbYDE0 A7YAnRUrer6z+mRmwXgBBT3/jMUGDBV9
=t0FC
-----END PGP SIGNATURE-----
kernel/time/timeconv.c line 62
Quoted 10 lines · Zhaolei
kernel/time/timeconv.c new file +10
-0+ +static void __offtime (__kernel_time_t totalsecs,int offset, +unsigned int *year,unsigned int *mon,unsigned int *mday, +unsigned int *hour,unsigned int *min,unsigned int *sec, +unsigned int *wday,unsigned int *yday) +{ +long days, rem, y; +const unsigned short *ip; + + days = totalsecs /SECS_PER_DAY ;
Does this handle leap seconds? Doesn't seem to.
-Andi
Signature · 1 line
[email protected] -- Speaking for myself only.
* From: "Andi Kleen" <[email protected]>
Quoted 4 lines
Quoted 4 lines
kernel/time/timeconv.c new file +4
-0+ +static void __offtime(__kernel_time_t totalsecs, int offset, + unsigned int *year, unsigned int *mon, unsigned int *mday, + unsigned int *hour, unsigned int *min, unsigned int *sec,
Quoted 8 lines
kernel/time/timeconv.c line 62
Quoted 6 lines
kernel/time/timeconv.c new file +6
-0+ unsigned int *wday, unsigned int *yday) +{ + long days, rem, y; +const unsigned short *ip; + + days = totalsecs /SECS_PER_DAY ;Does this handle leap seconds? Doesn't seem to.
Hello, Andi
Thank for your attention.
It seems that we don't need to deal with leap seconds in conversion because unix time is not counting leap seconds..
There is some detail in http://en.wikipedia.org/wiki/Unix_time
Thanks
Zhaolei ÿôèº{.nÇ+‰·Ÿ®‰†+%ŠËÿ±éݶ¥Šwÿº{.nÇ+‰·¥Š{±þG«�éÿŠ{ayºÊ‡Ú™ë,j¢f£¢·hš�ï�êÿ‘êçz_è®(階ŠÝ¢j"�ú¶m§ÿÿ¾«þG«�éÿ¢¸?™¨èÚ&£ø§~�á¶iO•æ¬z·švØ^¶m§ÿÿÃÿ¶ìÿ¢¸?–I¥