Outline
Purpose
Provide common non-primitive operations on bdlt::Datetime
.
Classes
- See also
- bdlt_datetime, bdlt_datetimeinterval, bdlt_epochutil
Description
This component provides non-primitive operations on bdlt::Datetime
objects. In particular, bdlt::DatetimeUtil
supplies conversions of universal time to and from the C-standard struct
tm
(which we alias as bsl::tm
) representations.
This utility component provides the following (static) methods:
Definition bdlt_datetime.h:331
Usage
This section illustrates intended use of this component.
Example 1: Converting Between bsl::tm and bdlt::Datetime
When interfacing with legacy systems, we may encounter calls that represent date/time information using the standard bsl::tm
. In such cases, we have to be able to convert that information to/from a bdlt::Datetime
object in order to interface with the rest of our systems.
Suppose we have a legacy system that tracks last-access times in terms of bsl::tm
. We can use the convertToTm
and convertFromTm
routines from this component to convert that information.
First, we define a class, MyAccessTracker
, that the legacy system uses to manage last-access times (eliding the implementation for brevity):
class MyAccessTracker {
TStringTmMap m_accesses;
public:
const bsl::tm& accessTime);
int getLastAccess(bsl::tm *result,
const bsl::string& username)
const;
};
#define BSLMF_NESTED_TRAIT_DECLARATION(t_TYPE, t_TRAIT)
Definition bslmf_nestedtraitdeclaration.h:231
Definition bslstl_string.h:1281
Definition bslstl_map.h:619
Definition bslma_allocator.h:457
Definition bslma_usesbslmaallocator.h:343
Next, we define a utility to allow us to use bdlt::Datetime
with our legacy access tracker:
class MyAccessTrackerUtil {
public:
const MyAccessTracker& tracker,
static void updateLastAccess(MyAccessTracker *tracker,
};
Then, we implement getLastAccess
:
const MyAccessTracker& tracker,
{
bsl::tm legacyAccessTime;
int rc = tracker.getLastAccess(&legacyAccessTime, username);
if (rc) {
return rc;
}
}
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
static int convertFromTm(Datetime *result, const bsl::tm &timeStruct)
Definition bdlt_datetimeutil.h:266
Next, we implement updateLastAccess
:
void MyAccessTrackerUtil::updateLastAccess(
MyAccessTracker *tracker,
{
bsl::tm legacyAccessTime;
tracker->updateLastAccess(username, legacyAccessTime);
}
static bsl::tm convertToTm(const Datetime &datetime)
Definition bdlt_datetimeutil.h:298
Finally, we create an access tracker then interact with it using bdlt::Datetime
times.
void exerciseTracker()
{
MyAccessTracker accessTracker;
MyAccessTrackerUtil::updateLastAccess(&accessTracker,
richtofenName,
richtofenDate);
int rc = MyAccessTrackerUtil::getLastAccess(&lastAccessTime,
accessTracker,
richtofenName);
assert(0 == rc);
assert(lastAccessTime == richtofenDate);
}