1 /*------------------------------------------------------------------------------
2 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
3 *
4 * Distributable under the terms of either the Apache License (Version 2.0) or
5 * the GNU Lesser General Public License, as specified in the COPYING file.
6 ------------------------------------------------------------------------------*/
7 #ifndef _lucene_document_DateTools_
8 #define _lucene_document_DateTools_
9 
10 #ifdef _CL_TIME_WITH_SYS_TIME
11 # include <sys/time.h>
12 # include <time.h>
13 #else
14 # if defined(_CL_HAVE_SYS_TIME_H)
15 #  include <sys/time.h>
16 # else
17 #  include <time.h>
18 # endif
19 #endif
20 
21 #ifdef _CL_HAVE_SYS_TIMEB_H
22 # include <sys/timeb.h>
23 #endif
24 
CL_NS_DEF(document)25 CL_NS_DEF(document)
26 
27 class CLUCENE_EXPORT DateTools {
28 private:
29   static void strCatDate(TCHAR* buf, int zeroes, int value);
30 public:
31 
32   enum Resolution {
33           NO_RESOLUTION,
34           YEAR_FORMAT,		// yyyy
35           MONTH_FORMAT,		// yyyyMM
36           DAY_FORMAT,			// yyyyMMdd
37           HOUR_FORMAT,		// yyyyMMddHH
38           MINUTE_FORMAT,		// yyyyMMddHHmm
39           SECOND_FORMAT,		// yyyyMMddHHmmss
40           MILLISECOND_FORMAT	// yyyyMMddHHmmssSSS
41   };
42 
43   /**
44   * Converts a millisecond time to a string suitable for indexing.
45   *
46   * @param time the date expressed as milliseconds since January 1, 1970, 00:00:00 GMT
47   * @param resolution the desired resolution, see {@link #Resolution}
48   * @return a string in format <code>yyyyMMddHHmmssSSS</code> or shorter,
49   *  depeding on <code>resolution</code>; using UTC as timezone
50   */
51   static TCHAR* timeToString(const int64_t time, Resolution resolution = MILLISECOND_FORMAT);
52 
53   static void timeToString(const int64_t time, Resolution resolution, TCHAR* buf, size_t bufLength);
54 
55   /**
56   * Converts a string produced by <code>timeToString</code> or
57   * <code>dateToString</code> back to a time, represented as the
58   * number of milliseconds since January 1, 1970, 00:00:00 GMT.
59   *
60   * @param dateString the date string to be converted
61   * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT
62   * @throws ParseException if <code>dateString</code> is not in the
63   *  expected format
64   */
65   static int64_t stringToTime(const TCHAR* dateString);
66 
67   static tm* stringToDate(const TCHAR* dateString);
68 
69   /****
70 
71   *   CLucene specific methods
72 
73   *****/
74 
75   /**
76   * Returns a 64bit time value based on the parameters passed
77   */
78   static int64_t getTime(unsigned short year, uint8_t month, uint8_t mday, uint8_t hours = 0,
79       uint8_t minutes = 0, uint8_t seconds = 0, unsigned short ms = 0);
80 
81   /**
82   * Returns a 64bit time value which is inclusive of the whole last day.
83   */
84   static int64_t timeMakeInclusive(const int64_t time);
85 
86   inline static int64_t getDifferenceFromGMT();
87 
88   static TCHAR* getISOFormat(const int64_t time);
89   static TCHAR* getISOFormat(unsigned short year, uint8_t month, uint8_t mday, uint8_t hours = 0,
90       uint8_t minutes = 0, uint8_t seconds = 0, unsigned short ms = 0);
91 
92   virtual ~DateTools();
93 
94 };
95 CL_NS_END
96 #endif
97