Provide common non-primitive operations on bdlt::Datetime
.
More...
Namespaces |
namespace | bdlt |
Detailed Description
- Outline
-
-
- Purpose:
- Provide common non-primitive operations on
bdlt::Datetime
.
-
- Classes:
-
- See also:
- Component bdlt_datetime, Component bdlt_datetimeinterval, Component 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:
-
- 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): Next, we define a utility to allow us to use bdlt::Datetime
with our legacy access tracker: class MyAccessTrackerUtil {
public:
static int getLastAccess(bdlt::Datetime *result,
const MyAccessTracker& tracker,
const bsl::string& username);
static void updateLastAccess(MyAccessTracker *tracker,
const bsl::string& username,
const bdlt::Datetime& accessTime);
};
Then, we implement getLastAccess
: Next, we implement updateLastAccess
: Finally, we create an access tracker then interact with it using bdlt::Datetime
times. void exerciseTracker()
{
MyAccessTracker accessTracker;
bsl::string richtofenName = "Baron von Richtofen";
bdlt::Datetime richtofenDate(1918, 4, 21, 11, 0, 0);
MyAccessTrackerUtil::updateLastAccess(&accessTracker,
richtofenName,
richtofenDate);
bdlt::Datetime lastAccessTime;
int rc = MyAccessTrackerUtil::getLastAccess(&lastAccessTime,
accessTracker,
richtofenName);
assert(0 == rc);
assert(lastAccessTime == richtofenDate);
}