my old draft of wxDateTime
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@4715 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
parent
acb8423c2b
commit
0979c96223
1148
include/wx/datetime.h
Normal file
1148
include/wx/datetime.h
Normal file
File diff suppressed because it is too large
Load Diff
146
src/common/datetime.cpp
Normal file
146
src/common/datetime.cpp
Normal file
@ -0,0 +1,146 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Name: wx/datetime.h
|
||||
// Purpose: implementation of time/date related classes
|
||||
// Author: Vadim Zeitlin
|
||||
// Modified by:
|
||||
// Created: 11.05.99
|
||||
// RCS-ID: $Id$
|
||||
// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
|
||||
// Licence: wxWindows license
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// ============================================================================
|
||||
// declarations
|
||||
// ============================================================================
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// headers
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#ifdef __GNUG__
|
||||
#pragma implementation "datetime.h"
|
||||
#endif
|
||||
|
||||
// For compilers that support precompilation, includes "wx.h".
|
||||
#include "wx/wxprec.h"
|
||||
|
||||
#ifdef __BORLANDC__
|
||||
#pragma hdrstop
|
||||
#endif
|
||||
|
||||
#ifndef WX_PRECOMP
|
||||
#include "wx/string.h"
|
||||
#include "wx/intl.h"
|
||||
#include "wx/log.h"
|
||||
#endif // WX_PRECOMP
|
||||
|
||||
#include "wx/datetime.h"
|
||||
|
||||
// ============================================================================
|
||||
// implementation of wxDateTime
|
||||
// ============================================================================
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// static data
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
Country wxDateTime::ms_country;
|
||||
wxDateTime wxDateTime::ms_InvDateTime;
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// constructors and assignment operators
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
wxDateTime& wxDateTime::Set(const struct tm& tm)
|
||||
{
|
||||
time_t timet = mktime(tm);
|
||||
if ( timet == (time_t)(-1) )
|
||||
{
|
||||
wxFAIL_MSG(_T("Invalid time"));
|
||||
|
||||
return ms_InvDateTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Set(timet);
|
||||
}
|
||||
}
|
||||
|
||||
wxDateTime& wxDateTime::Set(wxDateTime_t hour,
|
||||
wxDateTime_t minute,
|
||||
wxDateTime_t second,
|
||||
wxDateTime_t millisec)
|
||||
{
|
||||
// we allow seconds to be 61 to account for the leap seconds, even if we
|
||||
// don't use them really
|
||||
wxCHECK_MSG( hour < 24 && second < 62 && minute < 60 && millisec < 1000,
|
||||
ms_InvDateTime,
|
||||
_T("Invalid time in wxDateTime::Set()") );
|
||||
|
||||
// get the current date from system
|
||||
time_t timet = GetTimeNow();
|
||||
struct tm *tm = localtime(&timet);
|
||||
|
||||
// adjust the time
|
||||
tm->tm_hour = hour;
|
||||
tm->tm_min = minute;
|
||||
tm->tm_sec = second;
|
||||
|
||||
(void)Set(tm);
|
||||
|
||||
// and finally adjust milliseconds
|
||||
return SetMillisecond(millisec);
|
||||
}
|
||||
|
||||
wxDateTime& wxDateTime::Set(wxDateTime_t day,
|
||||
Month month,
|
||||
int year,
|
||||
wxDateTime_t hour,
|
||||
wxDateTime_t minute,
|
||||
wxDateTime_t second,
|
||||
wxDateTime_t millisec)
|
||||
{
|
||||
wxCHECK_MSG( hour < 24 && second < 62 && minute < 60 && millisec < 1000,
|
||||
ms_InvDateTime,
|
||||
_T("Invalid time in wxDateTime::Set()") );
|
||||
|
||||
if ( year == Inv_Year )
|
||||
year = GetCurrentYear();
|
||||
if ( month == Inv_Month )
|
||||
month = GetCurrentMonth();
|
||||
|
||||
wxCHECK_MSG( day < GetNumberOfDays(month, year), ms_InvDateTime,
|
||||
_T("Invalid date in wxDateTime::Set()") );
|
||||
|
||||
// the range of time_t type (inclusive)
|
||||
static const int yearMinInRange = 1970;
|
||||
static const int yearMaxInRange = 2037;
|
||||
|
||||
// test only the year instead of testing for the exact end of the Unix
|
||||
// time_t range - it doesn't bring anything to do more precise checks
|
||||
if ( year >= yearMaxInRange && year <= yearMaxInRange )
|
||||
{
|
||||
// use the standard library version if the date is in range - this is
|
||||
// probably much more efficient than our code
|
||||
struct tm tm;
|
||||
tm.tm_year = year;
|
||||
tm.tm_mon = month;
|
||||
tm.tm_mday = day;
|
||||
tm.tm_hour = hour;
|
||||
tm.tm_min = minute;
|
||||
tm.tm_sec = second;
|
||||
|
||||
(void)Set(tm);
|
||||
|
||||
// and finally adjust milliseconds
|
||||
return SetMillisecond(millisec);
|
||||
}
|
||||
else
|
||||
{
|
||||
// do time calculations ourselves: we want to calculate the number of
|
||||
// milliseconds between the given date and the epoch (necessarily
|
||||
// negative)
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
Loading…
Reference in New Issue
Block a user