1 /* Some of the source code in this file came from "linux/fs/fat/misc.c". */
5 * Written 1992,1993 by Werner Almesberger
6 * 22/11/2000 - Fixed fat_date_unix2dos for dates earlier than 01/01/1980
7 * and date_dos2unix for date==0 by Igor Zhbanov(bsg@uniyar.ac.ru)
11 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <linux/semaphore.h>
29 #include <linux/time.h>
31 #include "exfat_config.h"
32 #include "exfat_global.h"
33 #include "exfat_api.h"
34 #include "exfat_oal.h"
38 INT32 sm_init(struct semaphore *sm)
44 INT32 sm_P(struct semaphore *sm)
50 void sm_V(struct semaphore *sm)
55 extern struct timezone sys_tz;
57 #define UNIX_SECS_1980 315532800L
59 #if BITS_PER_LONG == 64
60 #define UNIX_SECS_2108 4354819200L
63 #define DAYS_DELTA_DECADE (365 * 10 + 2)
64 #define NO_LEAP_YEAR_2100 (120)
65 #define IS_LEAP_YEAR(y) (!((y) & 3) && (y) != NO_LEAP_YEAR_2100)
67 #define SECS_PER_MIN (60)
68 #define SECS_PER_HOUR (60 * SECS_PER_MIN)
69 #define SECS_PER_DAY (24 * SECS_PER_HOUR)
71 #define MAKE_LEAP_YEAR(leap_year, year) \
73 if (unlikely(year > NO_LEAP_YEAR_2100)) \
74 leap_year = ((year + 3) / 4) - 1; \
76 leap_year = ((year + 3) / 4); \
81 static time_t accum_days_in_year[] = {
82 0, 0, 31, 59, 90,120,151,181,212,243,273,304,334, 0, 0, 0,
86 TIMESTAMP_T *tm_current(TIMESTAMP_T *tp, UINT8 tz_utc)
88 struct timespec ts = CURRENT_TIME_SEC;
89 time_t second = ts.tv_sec;
90 time_t day, leap_day, month, year;
93 second -= sys_tz.tz_minuteswest * SECS_PER_MIN;
95 if (second < UNIX_SECS_1980) {
104 #if BITS_PER_LONG == 64
105 if (second >= UNIX_SECS_2108) {
116 day = second / SECS_PER_DAY - DAYS_DELTA_DECADE;
119 MAKE_LEAP_YEAR(leap_day, year);
120 if (year * 365 + leap_day > day)
123 MAKE_LEAP_YEAR(leap_day, year);
125 day -= year * 365 + leap_day;
127 if (IS_LEAP_YEAR(year) && day == accum_days_in_year[3]) {
130 if (IS_LEAP_YEAR(year) && day > accum_days_in_year[3])
132 for (month = 1; month < 12; month++) {
133 if (accum_days_in_year[month + 1] > day)
137 day -= accum_days_in_year[month];
139 tp->sec = second % SECS_PER_MIN;
140 tp->min = (second / SECS_PER_MIN) % 60;
141 tp->hour = (second / SECS_PER_HOUR) % 24;