/* Portions are Copyright (C) 2011 Google Inc */ /* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is the Netscape Portable Runtime (NSPR). * * The Initial Developer of the Original Code is * Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 1998-2000 * the Initial Developer. All Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ /* * prtime.cc -- * NOTE: The original nspr file name is prtime.c * * NSPR date and time functions * * CVS revision 3.37 */ /* * The following functions were copied from the NSPR prtime.c file. * PR_ParseTimeString * We inlined the new PR_ParseTimeStringToExplodedTime function to avoid * copying PR_ExplodeTime and PR_LocalTimeParameters. (The PR_ExplodeTime * and PR_ImplodeTime calls cancel each other out.) * PR_NormalizeTime * PR_GMTParameters * PR_ImplodeTime * Upstream implementation from * http://lxr.mozilla.org/nspr/source/pr/src/misc/prtime.c#221 * All types and macros are defined in the base/third_party/prtime.h file. * These have been copied from the following nspr files. We have only copied * over the types we need. * 1. prtime.h * 2. prtypes.h * 3. prlong.h * * Unit tests are in base/time/pr_time_unittest.cc. */ #include "base/third_party/nspr/prtime.h" #include "base/check.h" #include "build/build_config.h" #include <ctype.h> #include <errno.h> /* for EINVAL */ #include <limits.h> #include <stddef.h> #include <string.h> #include <time.h> /* * The COUNT_LEAPS macro counts the number of leap years passed by * till the start of the given year Y. At the start of the year 4 * A.D. the number of leap years passed by is 0, while at the start of * the year 5 A.D. this count is 1. The number of years divisible by * 100 but not divisible by 400 (the non-leap years) is deducted from * the count to get the correct number of leap years. * * The COUNT_DAYS macro counts the number of days since 01/01/01 till the * start of the given year Y. The number of days at the start of the year * 1 is 0 while the number of days at the start of the year 2 is 365 * (which is ((2)-1) * 365) and so on. The reference point is 01/01/01 * midnight 00:00:00. */ #define COUNT_LEAPS(Y) … #define COUNT_DAYS(Y) … #define DAYS_BETWEEN_YEARS(A, B) … /* Implements the Unix localtime_r() function for windows */ #if BUILDFLAG(IS_WIN) static void localtime_r(const time_t* secs, struct tm* time) { (void) localtime_s(time, secs); } #endif /* * Static variables used by functions in this file */ /* * The following array contains the day of year for the last day of * each month, where index 1 is January, and day 0 is January 1. */ static const int lastDayOfMonth[2][13] = …; /* * The number of days in a month */ static const PRInt8 nDays[2][12] = …; /* *------------------------------------------------------------------------ * * PR_ImplodeTime -- * * Cf. time_t mktime(struct tm *tp) * Note that 1 year has < 2^25 seconds. So an PRInt32 is large enough. * *------------------------------------------------------------------------ */ PRTime PR_ImplodeTime(const PRExplodedTime *exploded) { … } /* *------------------------------------------------------------------------- * * IsLeapYear -- * * Returns 1 if the year is a leap year, 0 otherwise. * *------------------------------------------------------------------------- */ static int IsLeapYear(PRInt16 year) { … } /* * 'secOffset' should be less than 86400 (i.e., a day). * 'time' should point to a normalized PRExplodedTime. */ static void ApplySecOffset(PRExplodedTime *time, PRInt32 secOffset) { … } void PR_NormalizeTime(PRExplodedTime *time, PRTimeParamFn params) { … } /* *------------------------------------------------------------------------ * * PR_GMTParameters -- * * Returns the PRTimeParameters for Greenwich Mean Time. * Trivially, both the tp_gmt_offset and tp_dst_offset fields are 0. * *------------------------------------------------------------------------ */ PRTimeParameters PR_GMTParameters(const PRExplodedTime *gmt) { … } /* * The following code implements PR_ParseTimeString(). It is based on * ns/lib/xp/xp_time.c, revision 1.25, by Jamie Zawinski <[email protected]>. */ /* * We only recognize the abbreviations of a small subset of time zones * in North America, Europe, and Japan. * * PST/PDT: Pacific Standard/Daylight Time * MST/MDT: Mountain Standard/Daylight Time * CST/CDT: Central Standard/Daylight Time * EST/EDT: Eastern Standard/Daylight Time * AST: Atlantic Standard Time * NST: Newfoundland Standard Time * GMT: Greenwich Mean Time * BST: British Summer Time * MET: Middle Europe Time * EET: Eastern Europe Time * JST: Japan Standard Time */ TIME_TOKEN; /* * This parses a time/date string into a PRTime * (microseconds after "1-Jan-1970 00:00:00 GMT"). * It returns PR_SUCCESS on success, and PR_FAILURE * if the time/date string can't be parsed. * * Many formats are handled, including: * * 14 Apr 89 03:20:12 * 14 Apr 89 03:20 GMT * Fri, 17 Mar 89 4:01:33 * Fri, 17 Mar 89 4:01 GMT * Mon Jan 16 16:12 PDT 1989 * Mon Jan 16 16:12 +0130 1989 * 6 May 1992 16:41-JST (Wednesday) * 22-AUG-1993 10:59:12.82 * 22-AUG-1993 10:59pm * 22-AUG-1993 12:59am * 22-AUG-1993 12:59 PM * Friday, August 04, 1995 3:54 PM * 06/21/95 04:24:34 PM * 20/06/95 21:07 * 95-06-08 19:32:48 EDT * 1995-06-17T23:11:25.342156Z * * If the input string doesn't contain a description of the timezone, * we consult the `default_to_gmt' to decide whether the string should * be interpreted relative to the local time zone (PR_FALSE) or GMT (PR_TRUE). * The correct value for this argument depends on what standard specified * the time string which you are parsing. */ PRStatus PR_ParseTimeString( const char *string, PRBool default_to_gmt, PRTime *result_imploded) { … }