1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-08-13 19:37:10 +02:00
Files
bin
module
vendor
CPR
ConcurrentQueue
Fmt
MaxmindDB
POCO
ApacheConnector
CppParser
CppUnit
Crypto
Data
Encodings
Foundation
cmake
include
Poco
Dynamic
ASCIIEncoding.h
AbstractCache.h
AbstractDelegate.h
AbstractEvent.h
AbstractObserver.h
AbstractPriorityDelegate.h
AbstractStrategy.h
AccessExpirationDecorator.h
AccessExpireCache.h
AccessExpireLRUCache.h
AccessExpireStrategy.h
ActiveDispatcher.h
ActiveMethod.h
ActiveResult.h
ActiveRunnable.h
ActiveStarter.h
Activity.h
Alignment.h
Any.h
ArchiveStrategy.h
Array.h
Ascii.h
AsyncChannel.h
AtomicCounter.h
AtomicFlag.h
AutoPtr.h
AutoReleasePool.h
Base32Decoder.h
Base32Encoder.h
Base64Decoder.h
Base64Encoder.h
BasicEvent.h
BinaryReader.h
BinaryWriter.h
Buffer.h
BufferAllocator.h
BufferedBidirectionalStreamBuf.h
BufferedStreamBuf.h
Bugcheck.h
ByteOrder.h
Channel.h
Checksum.h
ClassLibrary.h
ClassLoader.h
Clock.h
Condition.h
Config.h
Configurable.h
ConsoleChannel.h
CountingStream.h
DataURIStream.h
DataURIStreamFactory.h
DateTime.h
DateTimeFormat.h
DateTimeFormatter.h
DateTimeParser.h
Debugger.h
DefaultStrategy.h
DeflatingStream.h
Delegate.h
DigestEngine.h
DigestStream.h
DirectoryIterator.h
DirectoryIteratorStrategy.h
DirectoryIterator_UNIX.h
DirectoryIterator_WIN32U.h
DirectoryWatcher.h
DynamicAny.h
DynamicAnyHolder.h
DynamicFactory.h
DynamicStruct.h
Environment.h
Environment_UNIX.h
Environment_VX.h
Environment_WIN32U.h
Environment_WINCE.h
Error.h
ErrorHandler.h
Event.h
EventArgs.h
EventChannel.h
EventLogChannel.h
Event_POSIX.h
Event_VX.h
Event_WIN32.h
Exception.h
ExpirationDecorator.h
Expire.h
ExpireCache.h
ExpireLRUCache.h
ExpireStrategy.h
FIFOBuffer.h
FIFOBufferStream.h
FIFOEvent.h
FIFOStrategy.h
FPEnvironment.h
FPEnvironment_C99.h
FPEnvironment_DEC.h
FPEnvironment_DUMMY.h
FPEnvironment_QNX.h
FPEnvironment_SUN.h
FPEnvironment_WIN32.h
File.h
FileChannel.h
FileStream.h
FileStreamFactory.h
FileStream_POSIX.h
FileStream_WIN32.h
File_UNIX.h
File_VX.h
File_WIN32U.h
File_WINCE.h
Format.h
Formatter.h
FormattingChannel.h
Foundation.h
FunctionDelegate.h
FunctionPriorityDelegate.h
Glob.h
HMACEngine.h
Hash.h
HashFunction.h
HashMap.h
HashSet.h
HashStatistic.h
HashTable.h
HexBinaryDecoder.h
HexBinaryEncoder.h
InflatingStream.h
Instantiator.h
JSONString.h
KeyValueArgs.h
LRUCache.h
LRUStrategy.h
Latin1Encoding.h
Latin2Encoding.h
Latin9Encoding.h
LineEndingConverter.h
LinearHashTable.h
ListMap.h
LocalDateTime.h
LogFile.h
LogFile_STD.h
LogFile_WIN32U.h
LogStream.h
Logger.h
LoggingFactory.h
LoggingRegistry.h
MD4Engine.h
MD5Engine.h
Manifest.h
MemoryPool.h
MemoryStream.h
Message.h
MetaObject.h
MetaProgramming.h
Mutex.h
Mutex_POSIX.h
Mutex_VX.h
Mutex_WIN32.h
Mutex_WINCE.h
NObserver.h
NamedEvent.h
NamedEvent_Android.h
NamedEvent_UNIX.h
NamedEvent_WIN32U.h
NamedMutex.h
NamedMutex_Android.h
NamedMutex_UNIX.h
NamedMutex_WIN32U.h
NamedTuple.h
NestedDiagnosticContext.h
Notification.h
NotificationCenter.h
NotificationQueue.h
NotificationStrategy.h
NullChannel.h
NullStream.h
Nullable.h
NumberFormatter.h
NumberParser.h
NumericString.h
ObjectPool.h
Observer.h
Optional.h
OrderedMap.h
OrderedSet.h
PBKDF2Engine.h
Path.h
Path_UNIX.h
Path_WIN32U.h
Path_WINCE.h
PatternFormatter.h
Pipe.h
PipeImpl.h
PipeImpl_DUMMY.h
PipeImpl_POSIX.h
PipeImpl_WIN32.h
PipeStream.h
Platform.h
Platform_POSIX.h
Platform_VX.h
Platform_WIN32.h
Poco.h
PriorityDelegate.h
PriorityEvent.h
PriorityExpire.h
PriorityNotificationQueue.h
PriorityStrategy.h
Process.h
Process_UNIX.h
Process_VX.h
Process_WIN32U.h
Process_WINCE.h
PurgeStrategy.h
RWLock.h
RWLock_Android.h
RWLock_POSIX.h
RWLock_VX.h
RWLock_WIN32.h
RWLock_WINCE.h
Random.h
RandomStream.h
RecursiveDirectoryIterator.h
RecursiveDirectoryIteratorImpl.h
RefCountedObject.h
RegularExpression.h
RotateStrategy.h
Runnable.h
RunnableAdapter.h
SHA1Engine.h
SHA2Engine.h
ScopedLock.h
ScopedUnlock.h
Semaphore.h
Semaphore_POSIX.h
Semaphore_VX.h
Semaphore_WIN32.h
SharedLibrary.h
SharedLibrary_HPUX.h
SharedLibrary_UNIX.h
SharedLibrary_VX.h
SharedLibrary_WIN32U.h
SharedMemory.h
SharedMemory_DUMMY.h
SharedMemory_POSIX.h
SharedMemory_WIN32.h
SharedPtr.h
SignalHandler.h
SimpleFileChannel.h
SimpleHashTable.h
SingletonHolder.h
SortedDirectoryIterator.h
SplitterChannel.h
Stopwatch.h
StrategyCollection.h
StreamChannel.h
StreamConverter.h
StreamCopier.h
StreamTokenizer.h
StreamUtil.h
String.h
StringTokenizer.h
SynchronizedObject.h
SyslogChannel.h
Task.h
TaskManager.h
TaskNotification.h
TeeStream.h
TemporaryFile.h
TextBufferIterator.h
TextConverter.h
TextEncoding.h
TextIterator.h
Thread.h
ThreadLocal.h
ThreadPool.h
ThreadTarget.h
Thread_POSIX.h
Thread_VX.h
Thread_WIN32.h
Thread_WINCE.h
TimedNotificationQueue.h
Timer.h
Timespan.h
Timestamp.h
Timezone.h
Token.h
Tuple.h
TypeList.h
Types.h
URI.h
URIStreamFactory.h
URIStreamOpener.h
UTF16Encoding.h
UTF32Encoding.h
UTF8Encoding.h
UTF8String.h
UTFString.h
UUID.h
UUIDGenerator.h
UnWindows.h
UnbufferedStreamBuf.h
Unicode.h
UnicodeConverter.h
UniqueAccessExpireCache.h
UniqueAccessExpireLRUCache.h
UniqueAccessExpireStrategy.h
UniqueExpireCache.h
UniqueExpireLRUCache.h
UniqueExpireStrategy.h
ValidArgs.h
Version.h
Void.h
Windows1250Encoding.h
Windows1251Encoding.h
Windows1252Encoding.h
WindowsConsoleChannel.h
ordered_hash.h
ordered_map.h
ordered_set.h
zconf.h
zlib.h
samples
src
testsuite
wcelibcex-1.0
CMakeLists.txt
Foundation_vs140.sln
Foundation_vs140.vcxproj
Foundation_vs140.vcxproj.filters
Foundation_vs150.sln
Foundation_vs150.vcxproj
Foundation_vs150.vcxproj.filters
Foundation_vs160.sln
Foundation_vs160.vcxproj
Foundation_vs160.vcxproj.filters
Makefile
extradirs
JSON
JWT
MongoDB
Net
NetSSL_OpenSSL
NetSSL_Win
PDF
PageCompiler
PocoDoc
ProGen
Redis
SevenZip
Util
XML
Zip
appveyor
build
cmake
contrib
doc
packaging
patches
release
travis
.gitattributes
.gitignore
.gitmodules
.travis.yml
CHANGELOG
CMakeLists.txt
CODE_OF_CONDUCT.md
CONTRIBUTING.md
CONTRIBUTORS
LICENSE
Makefile
NEWS
README
README.md
VERSION
appveyor.yml
build_cmake.cmd
build_cmake.sh
build_vs140.cmd
build_vs150.cmd
build_vs160.cmd
buildwin.cmd
buildwin.ps1
components
configure
cppignore.lnx
cppignore.win
env.bat
env.sh
libversion
SimpleIni
Squirrel
TinyDir
CMakeLists.txt
.gitignore
.gitmodules
CMakeLists.txt
LICENSE
README.md
SqMod/vendor/POCO/Foundation/include/Poco/DateTime.h
Sandu Liviu Catalin 4a6bfc086c Major plugin refactor and cleanup.
Switched to POCO library for unified platform/library interface.
Deprecated the external module API. It was creating more problems than solving.
Removed most built-in libraries in favor of system libraries for easier maintenance.
Cleaned and secured code with help from static analyzers.
2021-01-30 08:51:39 +02:00

444 lines
11 KiB
C++

//
// DateTime.h
//
// Library: Foundation
// Package: DateTime
// Module: DateTime
//
// Definition of the DateTime class.
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Foundation_DateTime_INCLUDED
#define Foundation_DateTime_INCLUDED
#include "Poco/Foundation.h"
#include "Poco/Timestamp.h"
#include "Poco/Timespan.h"
struct tm;
namespace Poco {
class Foundation_API DateTime
/// This class represents an instant in time, expressed
/// in years, months, days, hours, minutes, seconds
/// and milliseconds based on the Gregorian calendar.
/// The class is mainly useful for conversions between
/// UTC, Julian day and Gregorian calendar dates.
///
/// The date and time stored in a DateTime is always in UTC
/// (Coordinated Universal Time) and thus independent of the
/// timezone in effect on the system.
///
/// Conversion calculations are based on algorithms
/// collected and described by Peter Baum at
/// http://vsg.cape.com/~pbaum/date/date0.htm
///
/// Internally, this class stores a date/time in two
/// forms (UTC and broken down) for performance reasons. Only use
/// this class for conversions between date/time representations.
/// Use the Timestamp class for everything else.
///
/// Notes:
/// * Zero is a valid year (in accordance with ISO 8601 and astronomical year numbering)
/// * Year zero (0) is a leap year
/// * Negative years (years preceding 1 BC) are not supported
///
/// For more information, please see:
/// * http://en.wikipedia.org/wiki/Gregorian_Calendar
/// * http://en.wikipedia.org/wiki/Julian_day
/// * http://en.wikipedia.org/wiki/UTC
/// * http://en.wikipedia.org/wiki/ISO_8601
{
public:
enum Months
/// Symbolic names for month numbers (1 to 12).
{
JANUARY = 1,
FEBRUARY,
MARCH,
APRIL,
MAY,
JUNE,
JULY,
AUGUST,
SEPTEMBER,
OCTOBER,
NOVEMBER,
DECEMBER
};
enum DaysOfWeek
/// Symbolic names for week day numbers (0 to 6).
{
SUNDAY = 0,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
};
DateTime();
/// Creates a DateTime for the current date and time.
DateTime(const tm& tmStruct);
/// Creates a DateTime from tm struct.
DateTime(const Timestamp& timestamp);
/// Creates a DateTime for the date and time given in
/// a Timestamp.
DateTime(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0);
/// Creates a DateTime for the given Gregorian date and time.
/// * year is from 0 to 9999.
/// * month is from 1 to 12.
/// * day is from 1 to 31.
/// * hour is from 0 to 23.
/// * minute is from 0 to 59.
/// * second is from 0 to 60.
/// * millisecond is from 0 to 999.
/// * microsecond is from 0 to 999.
///
/// Throws an InvalidArgumentException if an argument date is out of range.
DateTime(double julianDay);
/// Creates a DateTime for the given Julian day.
DateTime(Timestamp::UtcTimeVal utcTime, Timestamp::TimeDiff diff);
/// Creates a DateTime from an UtcTimeVal and a TimeDiff.
///
/// Mainly used internally by DateTime and friends.
DateTime(const DateTime& dateTime);
/// Copy constructor. Creates the DateTime from another one.
~DateTime();
/// Destroys the DateTime.
DateTime& operator = (const DateTime& dateTime);
/// Assigns another DateTime.
DateTime& operator = (const Timestamp& timestamp);
/// Assigns a Timestamp.
DateTime& operator = (double julianDay);
/// Assigns a Julian day.
DateTime& assign(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microseconds = 0);
/// Assigns a Gregorian date and time.
/// * year is from 0 to 9999.
/// * month is from 1 to 12.
/// * day is from 1 to 31.
/// * hour is from 0 to 23.
/// * minute is from 0 to 59.
/// * second is from 0 to 60.
/// * millisecond is from 0 to 999.
/// * microsecond is from 0 to 999.
///
/// Throws an InvalidArgumentException if an argument date is out of range.
void swap(DateTime& dateTime);
/// Swaps the DateTime with another one.
int year() const;
/// Returns the year.
int month() const;
/// Returns the month (1 to 12).
int week(int firstDayOfWeek = MONDAY) const;
/// Returns the week number within the year.
/// FirstDayOfWeek should be either SUNDAY (0) or MONDAY (1).
/// The returned week number will be from 0 to 53. Week number 1 is the week
/// containing January 4. This is in accordance to ISO 8601.
///
/// The following example assumes that firstDayOfWeek is MONDAY. For 2005, which started
/// on a Saturday, week 1 will be the week starting on Monday, January 3.
/// January 1 and 2 will fall within week 0 (or the last week of the previous year).
///
/// For 2007, which starts on a Monday, week 1 will be the week starting on Monday, January 1.
/// There will be no week 0 in 2007.
int day() const;
/// Returns the day within the month (1 to 31).
int dayOfWeek() const;
/// Returns the weekday (0 to 6, where
/// 0 = Sunday, 1 = Monday, ..., 6 = Saturday).
int dayOfYear() const;
/// Returns the number of the day in the year.
/// January 1 is 1, February 1 is 32, etc.
int hour() const;
/// Returns the hour (0 to 23).
int hourAMPM() const;
/// Returns the hour (0 to 12).
bool isAM() const;
/// Returns true if hour < 12;
bool isPM() const;
/// Returns true if hour >= 12.
int minute() const;
/// Returns the minute (0 to 59).
int second() const;
/// Returns the second (0 to 59).
int millisecond() const;
/// Returns the millisecond (0 to 999)
int microsecond() const;
/// Returns the microsecond (0 to 999)
double julianDay() const;
/// Returns the julian day for the date and time.
Timestamp timestamp() const;
/// Returns the date and time expressed as a Timestamp.
Timestamp::UtcTimeVal utcTime() const;
/// Returns the date and time expressed in UTC-based
/// time. UTC base time is midnight, October 15, 1582.
/// Resolution is 100 nanoseconds.
bool operator == (const DateTime& dateTime) const;
bool operator != (const DateTime& dateTime) const;
bool operator < (const DateTime& dateTime) const;
bool operator <= (const DateTime& dateTime) const;
bool operator > (const DateTime& dateTime) const;
bool operator >= (const DateTime& dateTime) const;
DateTime operator + (const Timespan& span) const;
DateTime operator - (const Timespan& span) const;
Timespan operator - (const DateTime& dateTime) const;
DateTime& operator += (const Timespan& span);
DateTime& operator -= (const Timespan& span);
tm makeTM() const;
/// Converts DateTime to tm struct.
void makeUTC(int tzd);
/// Converts a local time into UTC, by applying the given time zone differential.
void makeLocal(int tzd);
/// Converts a UTC time into a local time, by applying the given time zone differential.
static bool isLeapYear(int year);
/// Returns true if the given year is a leap year;
/// false otherwise.
static int daysOfMonth(int year, int month);
/// Returns the number of days in the given month
/// and year. Month is from 1 to 12.
static bool isValid(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0);
/// Checks if the given date and time is valid
/// (all arguments are within a proper range).
///
/// Returns true if all arguments are valid, false otherwise.
protected:
static double toJulianDay(Timestamp::UtcTimeVal utcTime);
/// Computes the Julian day for an UTC time.
static double toJulianDay(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0);
/// Computes the Julian day for a Gregorian calendar date and time.
/// See <http://vsg.cape.com/~pbaum/date/jdimp.htm>, section 2.3.1 for the algorithm.
static Timestamp::UtcTimeVal toUtcTime(double julianDay);
/// Computes the UTC time for a Julian day.
void computeGregorian(double julianDay);
/// Computes the Gregorian date for the given Julian day.
/// See <http://vsg.cape.com/~pbaum/date/injdimp.htm>, section 3.3.1 for the algorithm.
void computeDaytime();
/// Extracts the daytime (hours, minutes, seconds, etc.) from the stored utcTime.
private:
void checkLimit(short& lower, short& higher, short limit);
void normalize();
///utility functions used to correct the overflow in computeGregorian
Timestamp::UtcTimeVal _utcTime;
short _year;
short _month;
short _day;
short _hour;
short _minute;
short _second;
short _millisecond;
short _microsecond;
};
//
// inlines
//
inline double DateTime::toJulianDay(Timestamp::UtcTimeVal utcTime)
{
double utcDays = double(utcTime)/864000000000.0;
return utcDays + 2299160.5; // first day of Gregorian reform (Oct 15 1582)
}
inline Timestamp::UtcTimeVal DateTime::toUtcTime(double julianDay)
{
return Timestamp::UtcTimeVal((julianDay - 2299160.5)*864000000000.0);
}
inline Timestamp DateTime::timestamp() const
{
return Timestamp::fromUtcTime(_utcTime);
}
inline Timestamp::UtcTimeVal DateTime::utcTime() const
{
return _utcTime;
}
inline int DateTime::year() const
{
return _year;
}
inline int DateTime::month() const
{
return _month;
}
inline int DateTime::day() const
{
return _day;
}
inline int DateTime::hour() const
{
return _hour;
}
inline int DateTime::hourAMPM() const
{
if (_hour < 1)
return 12;
else if (_hour > 12)
return _hour - 12;
else
return _hour;
}
inline bool DateTime::isAM() const
{
return _hour < 12;
}
inline bool DateTime::isPM() const
{
return _hour >= 12;
}
inline int DateTime::minute() const
{
return _minute;
}
inline int DateTime::second() const
{
return _second;
}
inline int DateTime::millisecond() const
{
return _millisecond;
}
inline int DateTime::microsecond() const
{
return _microsecond;
}
inline bool DateTime::operator == (const DateTime& dateTime) const
{
return _utcTime == dateTime._utcTime;
}
inline bool DateTime::operator != (const DateTime& dateTime) const
{
return _utcTime != dateTime._utcTime;
}
inline bool DateTime::operator < (const DateTime& dateTime) const
{
return _utcTime < dateTime._utcTime;
}
inline bool DateTime::operator <= (const DateTime& dateTime) const
{
return _utcTime <= dateTime._utcTime;
}
inline bool DateTime::operator > (const DateTime& dateTime) const
{
return _utcTime > dateTime._utcTime;
}
inline bool DateTime::operator >= (const DateTime& dateTime) const
{
return _utcTime >= dateTime._utcTime;
}
inline bool DateTime::isLeapYear(int year)
{
return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0);
}
inline void swap(DateTime& d1, DateTime& d2)
{
d1.swap(d2);
}
} // namespace Poco
#endif // Foundation_DateTime_INCLUDED