-
Notifications
You must be signed in to change notification settings - Fork 319
/
bdlt_datetime.h
2474 lines (2120 loc) · 96.3 KB
/
bdlt_datetime.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// bdlt_datetime.h -*-C++-*-
#ifndef INCLUDED_BDLT_DATETIME
#define INCLUDED_BDLT_DATETIME
#include <bsls_ident.h>
BSLS_IDENT("$Id: $")
//@PURPOSE: Provide a value-semantic type representing both date and time.
//
//@CLASSES:
// bdlt::Datetime: date and time value (at least microsecond resolution)
//
//@SEE_ALSO: bdlt_date, bdlt_time, bdlt_datetimetz
//
//@DESCRIPTION: This component implements a value-semantic type,
// 'bdlt::Datetime', that represents the composition of a date and a time
// value. The combined "date+time" value of a 'bdlt::Datetime' object is
// expressed textually as "yyyy/mm/dd_hh:mm:ss.ssssss", where "yyyy/mm/dd"
// represents the "date" part of the value and "hh:mm:ss.ssssss" represents the
// "time" part.
//
// In addition to the usual value-semantic complement of methods for getting
// and setting value, the 'bdlt::Datetime' class provides methods and operators
// for making relative adjustments to value ('addDays', 'addTime', 'addHours',
// etc.). In particular, note that adding units of time to a 'bdlt::Datetime'
// object can affect the values of both the time and date parts of the object.
// For example, invoking 'addHours(2)' on a 'bdlt::Datetime' object whose value
// is "1987/10/03_22:30:00.000000" updates the value to
// "1987/10/04_00:30:00.000000".
//
///Valid 'bdlt::Datetime' Values and Their Representations
///-------------------------------------------------------
// The "date" part of a 'bdlt::Datetime' value has a range of validity
// identical to a 'bdlt::Date' object -- i.e., valid dates (according to the
// Unix [POSIX] calendar) having years in the range '[1 .. 9999]'. The valid
// time values are '[00:00:00.000000 .. 23:59:59.999999]'. Furthermore, the
// unset time value (i.e., 24:00:00.000000, corresponding to the default
// constructed value for 'bdlt::Time') is available for every valid date. Note
// that the supported range of time does *not* allow for the injection of leap
// seconds. The value "0001/01/01_24:00:00.000000" is the default constructed
// value of 'bdlt::Datetime'.
//
// Furthermore, consistent with the 'bdlt::Time' type, a 'bdlt::Datetime'
// object whose "time" part has the default constructed value, behaves the
// same, with respect to manipulators and (most) free operators, as if the
// "time" part had the value 00:00:00.000000. As for 'bdlt::Time', the
// behavior of all 'bdlt::Datetime' relational comparison operators is
// undefined if the "time" part of either operand is 24:00:00.000000.
// Consequently, 'bdlt::Datetime' objects whose "time" part has the default
// constructed value must *not* be used as keys for the standard associative
// containers, since 'operator<' is not defined for such objects.
//
///Attributes
///----------
// Conceptually, the two primary attributes of 'bdlt::Datetime' are the
// constituent date and time values. These attributes are given the special
// designation "part" in this component (i.e., the "time" part and the "date"
// part, respectively) to distinguish them from the many other attributes (see
// below) that derive from these two main parts.
//..
// Name Related Type Default Range
// ---- ------------ --------------- ------------------------------------
// date bdlt::Date 0001/01/01 [0001/01/01 .. 9999/12/31]
// time bdlt::Time 24:00:00.000000 [00:00:00.000000 .. 23:59:59.999999]
//..
// A 'bdlt::Datetime' object can be used in terms of its "date" and "time"
// parts or, if appropriate to an application, the object can be viewed as a
// single, integrated type having the combined individual attributes of date
// and time. Accessors and manipulators are provided for each of these eight
// (derived) attributes:
//..
// Name Type Default Range Constraint
// ----------- ---- ------- ----------- -----------------------------
// year int 1 [1 .. 9999] none
// month int 1 [1 .. 12] none
// day int 1 [1 .. 31] must exist for year and month
// hour int 24 [0 .. 24] none
// minute int 0 [0 .. 59] must be 0 if '24 == hour'
// second int 0 [0 .. 59] must be 0 if '24 == hour'
// millisecond int 0 [0 .. 999] must be 0 if '24 == hour'
// microsecond int 0 [0 .. 999] must be 0 if '24 == hour'
//..
// There are two additional "date" part attributes to 'bdlt::Datetime':
//..
// Name Type Default Range Constraint
// --------- --------------------- ------- ------------ ----------------------
// dayOfYear int 1 [ 1 .. 366] 366 only on leap years
// dayOfWeek bdlt::DayOfWeek::Enum SAT [SUN .. SAT] tied to calendar day
//..
// where 'dayOfYear' tracks the value of 'year/month/day' (and *vice* *versa*),
// and 'dayOfWeek' can be accessed but not explicitly set.
//
///ISO Standard Text Representation
///--------------------------------
// A common standard text representation of a date and time value is described
// by ISO 8601. BDE provides the 'bdlt_iso8601util' component for conversion
// to and from the standard ISO8601 format.
//
///Usage
///-----
// This section illustrates intended use of this component.
//
///Example 1: Basic Syntax
///- - - - - - - - - - - -
// Values represented by objects of type 'bdlt::Datetime' are used widely in
// practice. The values of the individual attributes resulting from a
// default-constructed 'bdlt::Datetime' object, 'dt', are
// "0001/01/01_24:00:00.000000":
//..
// bdlt::Datetime dt; assert( 1 == dt.date().year());
// assert( 1 == dt.date().month());
// assert( 1 == dt.date().day());
// assert(24 == dt.hour());
// assert( 0 == dt.minute());
// assert( 0 == dt.second());
// assert( 0 == dt.millisecond());
// assert( 0 == dt.microsecond());
//..
// We can then set 'dt' to have a specific value, say, 8:43pm on January 6,
// 2013:
//..
// dt.setDatetime(2013, 1, 6, 20, 43);
// assert(2013 == dt.date().year());
// assert( 1 == dt.date().month());
// assert( 6 == dt.date().day());
// assert( 20 == dt.hour());
// assert( 43 == dt.minute());
// assert( 0 == dt.second());
// assert( 0 == dt.millisecond());
// assert( 0 == dt.microsecond());
//..
// Now suppose we add 6 hours and 9 seconds to this value. There is more than
// one way to do it:
//..
// bdlt::Datetime dt2(dt);
// dt2.addHours(6);
// dt2.addSeconds(9);
// assert(2013 == dt2.date().year());
// assert( 1 == dt2.date().month());
// assert( 7 == dt2.date().day());
// assert( 2 == dt2.hour());
// assert( 43 == dt2.minute());
// assert( 9 == dt2.second());
// assert( 0 == dt2.millisecond());
// assert( 0 == dt2.microsecond());
//
// bdlt::Datetime dt3(dt);
// dt3.addTime(6, 0, 9);
// assert(dt2 == dt3);
//..
// Notice that (in both cases) the date changed as a result of adding time;
// however, changing just the date never affects the time:
//..
// dt3.addDays(10);
// assert(2013 == dt3.date().year());
// assert( 1 == dt3.date().month());
// assert( 17 == dt3.date().day());
// assert( 2 == dt3.hour());
// assert( 43 == dt3.minute());
// assert( 9 == dt3.second());
// assert( 0 == dt3.millisecond());
// assert( 0 == dt3.microsecond());
//..
// We can also add more than a day's worth of time:
//..
// dt2.addHours(240);
// assert(dt3 == dt2);
//..
// The individual arguments can also be negative:
//..
// dt2.addTime(-246, 0, -10, 1000); // -246 h, -10 s, +1000 ms
// assert(dt == dt2);
//..
// Finally, we stream the value of 'dt2' to 'stdout':
//..
// bsl::cout << dt2 << bsl::endl;
//..
// The streaming operator produces the following output on 'stdout':
//..
// 06JAN2013_20:43:00.000000
//..
//
///Example 2: Creating a Schedule of Equal Time Intervals
/// - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Calculations involving date and time values are difficult to get correct
// manually; consequently, people tend to schedule events on natural time
// boundaries (e.g., on the hour) even if that is sub-optimal. Having a class
// such as 'bdlt::Datetime' makes doing date and time calculations trivial.
//
// Suppose one wants to divide into an arbitrary interval such as the time
// between sunset and sunrise into an arbitrary number (say 7) of equal
// intervals (perhaps to use as a duty roster for teams making astronomical
// observations).
//
// First, we create objects containing values for the start and end of the time
// interval:
//..
// bdlt::Datetime sunset(2014, 6, 26, 20, 31, 23); // New York City
// bdlt::Datetime sunrise(2014, 6, 27, 5, 26, 51); // New York City
//..
// Then, we calculate the length of each shift in milliseconds (for good
// precision -- we may be synchronizing astronomical instruments). Note that
// the difference of 'sunrise' and 'sunset' creates a temporary
// 'bdlt::DatetimeInterval' object:
//..
// const int numShifts = 7;
// const bsls::Types::Int64 shiftLengthInMsec
// = (sunrise - sunset).totalMilliseconds()
// / numShifts;
//..
// Now, we calculate (and print to 'stdout') the beginning and end times for
// each shift:
//..
// for (int i = 0; i <= numShifts; ++i) {
// bdlt::Datetime startOfShift(sunset);
// startOfShift.addMilliseconds(shiftLengthInMsec * i);
// bsl::cout << startOfShift << bsl::endl;
// }
//..
// Finally, we observe:
//..
// 26JUN2014_20:31:23.000000
// 26JUN2014_21:47:52.714000
// 26JUN2014_23:04:22.428000
// 27JUN2014_00:20:52.142000
// 27JUN2014_01:37:21.856000
// 27JUN2014_02:53:51.570000
// 27JUN2014_04:10:21.284000
// 27JUN2014_05:26:50.998000
//..
// Notice how our objects (since they manage both "date" and "time of day"
// parts of each point in time) seamlessly handle the transition between the
// two days.
#include <bdlscm_version.h>
#include <bdlt_date.h>
#include <bdlt_datetimeimputil.h>
#include <bdlt_datetimeinterval.h>
#include <bdlt_dayofweek.h>
#include <bdlt_time.h>
#include <bdlt_timeunitratio.h>
#include <bdlb_bitutil.h>
#include <bslh_hash.h>
#include <bslmf_integralconstant.h>
#include <bslmf_istriviallycopyable.h>
#include <bsls_assert.h>
#include <bsls_atomic.h>
#include <bsls_log.h>
#include <bsls_performancehint.h>
#include <bsls_platform.h>
#include <bsls_review.h>
#include <bsls_stackaddressutil.h>
#include <bsls_timeinterval.h>
#include <bsls_types.h>
#include <bsl_iosfwd.h>
#include <bsl_cstring.h> // memset
#include <bsl_sstream.h>
namespace BloombergLP {
namespace bdlt {
// ==============
// class Datetime
// ==============
class Datetime {
// This class implements a simply-constrained value-semantic type
// representing the composition of date and time values. Valid date values
// for the "date" part of a 'Datetime' object are the same as those defined
// for 'Date' objects; similarly, valid time values for the "time" part of
// a 'Datetime' object are similar to those defined for 'Time' objects (but
// with additional precision). Relational operators are disallowed on
// 'Datetime' objects whose "time" part has the same value as that of a
// default constructed 'Time' object.
// PRIVATE TYPES
enum {
k_NUM_TIME_BITS = 37,
k_DEFAULT_FRACTIONAL_SECOND_PRECISION = 6
};
// CLASS DATA
static const bsls::Types::Uint64 k_MAX_US_FROM_EPOCH;
static const bsls::Types::Uint64 k_REP_MASK = 0x8000000000000000ULL;
static const bsls::Types::Uint64 k_DATE_MASK = 0xffffffe000000000ULL;
static const bsls::Types::Uint64 k_TIME_MASK = 0x0000001fffffffffULL;
static bsls::AtomicInt64 s_invalidRepresentationCount;
// DATA
bsls::Types::Uint64 d_value; // encoded offset from the epoch
// FRIENDS
friend DatetimeInterval operator-(const Datetime&, const Datetime&);
friend bool operator==(const Datetime&, const Datetime&);
friend bool operator!=(const Datetime&, const Datetime&);
friend bool operator< (const Datetime&, const Datetime&);
friend bool operator<=(const Datetime&, const Datetime&);
friend bool operator> (const Datetime&, const Datetime&);
friend bool operator>=(const Datetime&, const Datetime&);
template <class HASHALG>
friend void hashAppend(HASHALG& hashAlg, const Datetime&);
// PRIVATE MANIPULATOR
void setMicrosecondsFromEpoch(bsls::Types::Uint64 totalMicroseconds);
// Assign to 'd_value' the representation of a datetime such that the
// difference between this datetime and the epoch is the specified
// 'totalMicroseconds'.
// PRIVATE ACCESSORS
bsls::Types::Uint64 microsecondsFromEpoch() const;
// Return the difference, measured in microseconds, between this
// datetime value, with 24:00:00.000000 converted to 0:00:00.000000,
// and the epoch.
bsls::Types::Uint64 updatedRepresentation() const;
// If 'd_value' is a valid representation, return 'd_value'.
// Otherwise, return the representation of the datetime corresponding
// to the datetime implied by assuming the value in 'd_value' is the
// concatenation of a 'Date' and a 'Time', and log or assert the
// detection of an invalid date.
bool validateAndTraceLogRepresentation() const;
// Return 'true' if the representation is valid. Invoke a review
// failure notifying of an invalid use of a 'bdlt::Datetime' instance
// and return 'false' if the representation is invalid and
// 'BSLS_ASSERT_SAFE' is inactive. The behavior is undefined if the
// representation is invalid and 'BSLS_ASSERT_SAFE' is active.
public:
// CLASS METHODS
static bool isValid(int year,
int month,
int day,
int hour = 0,
int minute = 0,
int second = 0,
int millisecond = 0,
int microsecond = 0);
// Return 'true' if the specified 'year', 'month', and 'day' attribute
// values, and the optionally specified 'hour', 'minute', 'second',
// 'millisecond', and 'microsecond' attribute values, represent a valid
// 'Datetime' value, and 'false' otherwise. Unspecified trailing
// optional parameters default to 0. 'year', 'month', 'day', 'hour',
// 'minute', 'second', 'millisecond', and 'microsecond' attribute
// values represent a valid 'Datetime' value if
// 'true == Date::isValidYearMonthDay(year, month, day)',
// '0 <= hour < 24', '0 <= minute < 60', '0 <= second < 60',
// '0 <= millisecond < 1000', and '0 <= microsecond < 1000'.
// Additionally, a valid 'year', 'month', 'day' with the time portion
// equal to 24:00:00.000000 also represents a valid 'Datetime' value.
// Aspects
static int maxSupportedBdexVersion(int versionSelector);
// Return the maximum valid BDEX format version, as indicated by the
// specified 'versionSelector', to be passed to the 'bdexStreamOut'
// method. Note that it is highly recommended that 'versionSelector'
// be formatted as "YYYYMMDD", a date representation. Also note that
// 'versionSelector' should be a *compile*-time-chosen value that
// selects a format version supported by both externalizer and
// unexternalizer. See the 'bslx' package-level documentation for more
// information on BDEX streaming of value-semantic types and
// containers.
// CREATORS
Datetime();
// Create a 'Datetime' object whose "date" and "time" parts have their
// respective default-constructed values, "0001/01/01" and
// "24:00:00.000000".
Datetime(const Date& date); // IMPLICIT
// Create a 'Datetime' object whose "date" part has the value of the
// specified 'date' and whose "time" part has the value
// "00:00:00.000000".
Datetime(const Date& date, const Time& time);
// Create a 'Datetime' object whose "date" and "time" parts have the
// values of the specified 'date' and 'time', respectively.
Datetime(int year,
int month,
int day,
int hour = 0,
int minute = 0,
int second = 0,
int millisecond = 0,
int microsecond = 0);
// Create a 'Datetime' object whose "date" part has the value
// represented by the specified 'year', 'month', and 'day' attributes,
// and whose "time" part has the value represented by the optionally
// specified 'hour', 'minute', 'second', 'millisecond', and
// 'microsecond' attributes. Unspecified trailing optional parameters
// default to 0. The behavior is undefined unless the eight attributes
// (collectively) represent a valid 'Datetime' value (see 'isValid').
Datetime(const Datetime& original);
// Create a 'Datetime' object having the value of the specified
// 'original' object.
//! ~Datetime() = default;
// Destroy this 'Datetime' object.
// MANIPULATORS
Datetime& operator=(const Datetime& rhs);
// Assign to this object the value of the specified 'rhs' object, and
// return a reference providing modifiable access to this object.
Datetime& operator+=(const bsls::TimeInterval& rhs);
// Add to this object the value of the specified 'rhs' object, and
// return a reference providing modifiable access to this object. If
// '24 == hour()' on entry, set the 'hour' attribute of this object to
// 0 before performing the addition. The behavior is undefined unless
// the resulting value is valid for 'Datetime' (see 'isValid').
Datetime& operator-=(const bsls::TimeInterval& rhs);
// Subtract from this object the value of the specified 'rhs' object,
// and return a reference providing modifiable access to this object.
// If '24 == hour()' on entry, set the 'hour' attribute of this object
// to 0 before performing the subtraction. The behavior is undefined
// unless the resulting value is valid for 'Datetime' (see 'isValid').
Datetime& operator+=(const DatetimeInterval& rhs);
// Add to this object the value of the specified 'rhs' object, and
// return a reference providing modifiable access to this object. If
// '24 == hour()' on entry, set the 'hour' attribute of this object to
// 0 before performing the addition. The behavior is undefined unless
// the resulting value is valid for 'Datetime' (see 'isValid').
Datetime& operator-=(const DatetimeInterval& rhs);
// Subtract from this object the value of the specified 'rhs' object,
// and return a reference providing modifiable access to this object.
// If '24 == hour()' on entry, set the 'hour' attribute of this object
// to 0 before performing the subtraction. The behavior is undefined
// unless the resulting value is valid for 'Datetime' (see 'isValid').
void setDatetime(const Date& date,
int hour = 0,
int minute = 0,
int second = 0,
int millisecond = 0,
int microsecond = 0);
// Set the value of this object to a 'Datetime' whose "date" part has
// the value represented by the specified 'date', and whose "time" part
// has the value represented by the optionally specified 'hour',
// 'minute', 'second', 'millisecond', and 'microsecond' attributes.
// Unspecified trailing optional parameters default to 0. The behavior
// is undefined unless the attributes (collectively) represent a valid
// 'Datetime' value (see 'isValid').
void setDatetime(const Date& date, const Time& time);
// Set the value of this object to a 'Datetime' whose "date" part has
// the value represented by the specified 'date', and whose "time" part
// has the value represented by the specified 'time'.
void setDatetime(int year,
int month,
int day,
int hour = 0,
int minute = 0,
int second = 0,
int millisecond = 0,
int microsecond = 0);
// Set the value of this object to a 'Datetime' whose "date" part has
// the value represented by the specified 'year', 'month', and 'day'
// attributes, and whose "time" part has the value represented by the
// optionally specified 'hour', 'minute', 'second', 'millisecond', and
// 'microsecond' attributes. Unspecified trailing optional parameters
// default to 0. The behavior is undefined unless the eight attributes
// (collectively) represent a valid 'Datetime' value (see 'isValid').
int setDatetimeIfValid(int year,
int month,
int day,
int hour = 0,
int minute = 0,
int second = 0,
int millisecond = 0,
int microsecond = 0);
// Set the "date" part of this object to have the value represented by
// the specified 'year', 'month', and 'day' attributes, and set the
// "time" part to have the value represented by the optionally
// specified 'hour', 'minute', 'second', 'millisecond', and
// 'microsecond' attributes, if the eight attribute values
// (collectively) represent a valid 'Datetime' value (see 'isValid').
// Unspecified trailing optional parameters default to 0. Return 0 on
// success, and a non-zero value (with no effect) otherwise.
int setDatetimeIfValid(const Date& date,
int hour = 0,
int minute = 0,
int second = 0,
int millisecond = 0,
int microsecond = 0);
// Set the value of this object to a 'Datetime' whose "date" part has
// the value represented by the specified 'date', and whose "time" part
// has the value represented by the optionally specified 'hour',
// 'minute', 'second', 'millisecond', and 'microsecond' attributes, if
// the attribute values (collectively) represent a valid 'Datetime'
// value (see 'isValid'). Unspecified trailing optional parameters
// default to 0. Return 0 on success, and a non-zero value (with no
// effect) otherwise.
void setDate(const Date& date);
// Set the "date" part of this object to have the value of the
// specified 'date'. Note that this method has no effect on the "time"
// part of this object.
void setYearDay(int year, int dayOfYear);
// Set the "date" part of this object to have the value represented by
// the specified 'year' and 'dayOfYear' attribute values. The behavior
// is undefined unless 'year' and 'dayOfYear' represent a valid 'Date'
// value (i.e., 'true == Date::isValidYearDay(year, dayOfYear)'). Note
// that this method has no effect on the "time" part of this object.
int setYearDayIfValid(int year, int dayOfYear);
// Set this object to have the value represented by the specified
// 'year' and 'dayOfYear' if they comprise a valid 'Date' value (see
// 'Date::isValidYearDay'). Return 0 on success, and a non-zero value
// (with no effect) otherwise.
void setYearMonthDay(int year, int month, int day);
// Set the "date" part of this object to have the value represented by
// the specified 'year', 'month', and 'day' attribute values. The
// behavior is undefined unless 'year', 'month', and 'day' represent a
// valid 'Date' value (i.e.,
// 'true == Date::isValidYearMonthDay(year, month, day)'). Note that
// this method has no effect on the "time" part of this object.
int setYearMonthDayIfValid(int year, int month, int day);
// Set this object to have the value represented by the specified
// 'year', 'month', and 'day' if they comprise a valid 'Date' value
// (see 'Date::isValidYearMonthDay'). Return 0 on success, and a
// non-zero value (with no effect) otherwise.
void setTime(const Time& time);
// Set the "time" part of this object to have the value of the
// specified 'time'. Note that this method has no effect on the "date"
// part of this object.
void setTime(int hour,
int minute = 0,
int second = 0,
int millisecond = 0,
int microsecond = 0);
// Set the "time" part of this object to have the value represented by
// the specified 'hour' attribute value and the optionally specified
// 'minute', 'second', 'millisecond', and 'microsecond' attribute
// values. Unspecified trailing optional parameters default to 0. The
// behavior is undefined unless 'hour', 'minute', 'second',
// 'millisecond', and 'microsecond' represent a valid "time" portion of
// a 'Datetime' value. Note that this method has no effect on the
// "date" part of this object.
int setTimeIfValid(int hour,
int minute = 0,
int second = 0,
int millisecond = 0,
int microsecond = 0);
// Set the "time" part of this object to have the value represented by
// the specified 'hour' attribute value and the optionally specified
// 'minute', 'second', 'millisecond', and 'microsecond' attribute
// values if they comprise a valid "time" portion of a 'DateTime'
// value. Unspecified trailing optional parameters default to 0.
// Return 0 on success, and a non-zero value (with no effect)
// otherwise. Note that this method has no effect on the "date" part
// of this object.
void setHour(int hour);
// Set the "hour" attribute of this object to the specified 'hour'
// value. If '24 == hour', set the 'minute', 'second', 'millisecond',
// and 'microsecond' attributes to 0. The behavior is undefined
// unless '0 <= hour <= 24'. Note that this method has no effect on
// the "date" part of this object.
int setHourIfValid(int hour);
// Set the "hour" attribute of this object to the specified 'hour'
// value if '0 <= hour <= 24'. If '24 == hour', set the 'minute',
// 'second', 'millisecond', and 'microsecond' attributes to 0. Return
// 0 on success, and a non-zero value (with no effect) otherwise. Note
// that this method has no effect on the "date" part of this object.
void setMinute(int minute);
// Set the "minute" attribute of this object to the specified 'minute'
// value. If '24 == hour()', set the 'hour' attribute to 0. The
// behavior is undefined unless '0 <= minute <= 59'. Note that this
// method has no effect on the "date" part of this object.
int setMinuteIfValid(int minute);
// Set the "minute" attribute of this object to the specified 'minute'
// value if '0 <= minute <= 59'. If '24 == hour()', set the 'hour'
// attribute to 0. Return 0 on success, and a non-zero value (with no
// effect) otherwise. Note that this method has no effect on the
// "date" part of this object.
void setSecond(int second);
// Set the "second" attribute of this object to the specified 'second'
// value. If '24 == hour()', set the 'hour' attribute to 0. The
// behavior is undefined unless '0 <= second <= 59'. Note that this
// method has no effect on the "date" part of this object.
int setSecondIfValid(int second);
// Set the "second" attribute of this object to the specified 'second'
// value if '0 <= second <= 59'. If '24 == hour()', set the 'hour'
// attribute to 0. Return 0 on success, and a non-zero value (with no
// effect) otherwise. Note that this method has no effect on the
// "date" part of this object.
void setMillisecond(int millisecond);
// Set the "millisecond" attribute of this object to the specified
// 'millisecond' value. If '24 == hour()', set the 'hour' attribute to
// 0. The behavior is undefined unless '0 <= millisecond <= 999'.
// Note that this method has no effect on the "date" part of this
// object.
int setMillisecondIfValid(int millisecond);
// Set the "millisecond" attribute of this object to the specified
// 'millisecond' value if '0 <= millisecond <= 999'. If
// '24 == hour()', set the 'hour' attribute to 0. Return 0 on success,
// and a non-zero value (with no effect) otherwise. Note that this
// method has no effect on the "date" part of this object.
void setMicrosecond(int microsecond);
// Set the "microsecond" attribute of this object to the specified
// 'microsecond' value. If '24 == hour()', set the 'hour' attribute to
// 0. The behavior is undefined unless '0 <= microsecond <= 999'.
// Note that this method has no effect on the "date" part of this
// object.
int setMicrosecondIfValid(int microsecond);
// Set the "microsecond" attribute of this object to the specified
// 'microsecond' value if '0 <= microsecond <= 999'. If
// '24 == hour()', set the 'hour' attribute to 0. Return 0 on success,
// and a non-zero value (with no effect) otherwise. Note that this
// method has no effect on the "date" part of this object.
Datetime& addDays(int days);
// Add the specified number of 'days' to the value of this object.
// Return a reference providing modifiable access to this object. The
// behavior is undefined unless the resulting value is in the valid
// range for a 'Datetime' object. Note that this method has no effect
// on the "time" part of this object. Also note that 'days' may be
// positive, 0, or negative.
int addDaysIfValid(int days);
// Add the specified number of 'days' to the value of this object, if
// the resulting value is in the valid range for a 'Datetime' object.
// Return 0 on success, and a non-zero value (with no effect)
// otherwise. Note that this method has no effect on the "time" part
// of this object. Also note that 'days' may be positive, 0, or
// negative.
Datetime& addTime(bsls::Types::Int64 hours,
bsls::Types::Int64 minutes = 0,
bsls::Types::Int64 seconds = 0,
bsls::Types::Int64 milliseconds = 0,
bsls::Types::Int64 microseconds = 0);
// Add the specified number of 'hours', and the optionally specified
// number of 'minutes', 'seconds', 'milliseconds', and 'microseconds'
// to the value of this object, adjusting the "date" part of this
// object accordingly. Unspecified trailing optional parameters
// default to 0. Return a reference providing modifiable access to
// this object. If '24 == hour()' on entry, set the 'hour' attribute
// to 0 before performing the addition. The behavior is undefined
// unless the resulting value is in the valid range for a 'Datetime'
// object. Note that each argument independently may be positive,
// negative, or 0.
int addTimeIfValid(bsls::Types::Int64 hours,
bsls::Types::Int64 minutes = 0,
bsls::Types::Int64 seconds = 0,
bsls::Types::Int64 milliseconds = 0,
bsls::Types::Int64 microseconds = 0);
// Add the specified number of 'hours', and the optionally specified
// number of 'minutes', 'seconds', 'milliseconds', and 'microseconds'
// to the value of this object, adjusting the "date" part of this
// object accordingly, if the resulting value is in the valid range for
// a 'Datetime' object. Unspecified trailing optional parameters
// default to 0. If '24 == hour()' on entry, set the 'hour' attribute
// to 0 before performing the addition. Return 0 on success, and a
// non-zero value (with no effect) otherwise. Note that each argument
// independently may be positive, negative, or 0.
Datetime& addHours(bsls::Types::Int64 hours);
// Add the specified number of 'hours' to the value of this object,
// adjusting the "date" part of the object accordingly. Return a
// reference providing modifiable access to this object. If
// '24 == hour()' on entry, set the 'hour' attribute to 0 before
// performing the addition. The behavior is undefined unless the
// resulting value is in the valid range for a 'Datetime' object. Note
// that 'hours' may be positive, negative, or 0.
int addHoursIfValid(bsls::Types::Int64 hours);
// Add the specified number of 'hours' to the value of this object,
// adjusting the "date" part of the object accordingly, if the
// resulting value is in the valid range for a 'Datetime' object. If
// '24 == hour()' on entry, set the 'hour' attribute to 0 before
// performing the addition. Return 0 on success, and a non-zero value
// (with no effect) otherwise. Note that 'hours' may be positive,
// negative, or 0.
Datetime& addMinutes(bsls::Types::Int64 minutes);
// Add the specified number of 'minutes' to the value of this object,
// adjusting the "date" part of the object accordingly. Return a
// reference providing modifiable access to this object. If
// '24 == hour()' on entry, set the 'hour' attribute to 0 before
// performing the addition. The behavior is undefined unless the
// resulting value is in the valid range for a 'Datetime' object. Note
// that 'minutes' may be positive, negative, or 0.
int addMinutesIfValid(bsls::Types::Int64 minutes);
// Add the specified number of 'minutes' to the value of this object,
// adjusting the "date" part of the object accordingly, if the
// resulting value is in the valid range for a 'Datetime' object. If
// '24 == hour()' on entry, set the 'hour' attribute to 0 before
// performing the addition. Return 0 on success, and a non-zero value
// (with no effect) otherwise. Note that 'minutes' may be positive,
// negative, or 0.
Datetime& addSeconds(bsls::Types::Int64 seconds);
// Add the specified number of 'seconds' to the value of this object,
// adjusting the "date" part of the object accordingly. Return a
// reference providing modifiable access to this object. If
// '24 == hour()' on entry, set the 'hour' attribute to 0 before
// performing the addition. The behavior is undefined unless the
// resulting value is in the valid range for a 'Datetime' object. Note
// that 'seconds' may be positive, negative, or 0.
int addSecondsIfValid(bsls::Types::Int64 seconds);
// Add the specified number of 'seconds' to the value of this object,
// adjusting the "date" part of the object accordingly, if the
// resulting value is in the valid range for a 'Datetime' object. If
// '24 == hour()' on entry, set the 'hour' attribute to 0 before
// performing the addition. Return 0 on success, and a non-zero value
// (with no effect) otherwise. Note that 'seconds' may be positive,
// negative, or 0.
Datetime& addMilliseconds(bsls::Types::Int64 milliseconds);
// Add the specified number of 'milliseconds' to the value of this
// object, adjusting the "date" part of the object accordingly. Return
// a reference providing modifiable access to this object. If
// '24 == hour()' on entry, set the 'hour' attribute to 0 before
// performing the addition. The behavior is undefined unless the
// resulting value is in the valid range for a 'Datetime' object. Note
// that 'milliseconds' may be positive, negative, or 0.
int addMillisecondsIfValid(bsls::Types::Int64 milliseconds);
// Add the specified number of 'milliseconds' to the value of this
// object, adjusting the "date" part of the object accordingly, if the
// resulting value is in the valid range for a 'Datetime' object. If
// '24 == hour()' on entry, set the 'hour' attribute to 0 before
// performing the addition. Return 0 on success, and a non-zero value
// (with no effect) otherwise. Note that 'milliseconds' may be
// positive, negative, or 0.
Datetime& addMicroseconds(bsls::Types::Int64 microseconds);
// Add the specified number of 'microseconds' to the value of this
// object, adjusting the "date" part of the object accordingly. Return
// a reference providing modifiable access to this object. If
// '24 == hour()' on entry, set the 'hour' attribute to 0 before
// performing the addition. The behavior is undefined unless the
// resulting value is in the valid range for a 'Datetime' object. Note
// that 'microseconds' may be positive, negative, or 0.
int addMicrosecondsIfValid(bsls::Types::Int64 microseconds);
// Add the specified number of 'microseconds' to the value of this
// object, adjusting the "date" part of the object accordingly, if the
// resulting value is in the valid range for a 'Datetime' object. If
// '24 == hour()' on entry, set the 'hour' attribute to 0 before
// performing the addition. Return 0 on success, and a non-zero value
// (with no effect) otherwise. Note that 'microseconds' may be
// positive, negative, or 0.
// Aspects
template <class STREAM>
STREAM& bdexStreamIn(STREAM& stream, int version);
// Assign to this object the value read from the specified input
// 'stream' using the specified 'version' format, and return a
// reference to 'stream'. If 'stream' is initially invalid, this
// operation has no effect. If 'version' is not supported, this object
// is unaltered and 'stream' is invalidated, but otherwise unmodified.
// If 'version' is supported but 'stream' becomes invalid during this
// operation, this object has an undefined, but valid, state. Note
// that no version is read from 'stream'. See the 'bslx' package-level
// documentation for more information on BDEX streaming of
// value-semantic types and containers.
// ACCESSORS
Date date() const;
// Return the value of the "date" part of this object.
int day() const;
// Return the value of the 'day' (of the month) attribute of this
// object.
DayOfWeek::Enum dayOfWeek() const;
// Return the value of the 'dayOfWeek' attribute associated with the
// 'day' (of the month) attribute of this object.
int dayOfYear() const;
// Return the value of the 'dayOfYear' attribute of this object.
void getTime(int *hour,
int *minute = 0,
int *second = 0,
int *millisecond = 0,
int *microsecond = 0) const;
// Load, into the specified 'hour', and the optionally specified
// 'minute', 'second', 'millisecond', and 'microsecond' the respective
// 'hour', 'minute', 'second', 'millisecond', and 'microsecond'
// attribute values from this time object. Unspecified arguments
// default to 0. Supplying 0 for an address argument suppresses the
// loading of the value for the corresponding attribute, but has no
// effect on the loading of other attribute values.
int hour() const;
// Return the value of the 'hour' attribute of this object.
int microsecond() const;
// Return the value of the 'microsecond' attribute of this object.
int millisecond() const;
// Return the value of the 'millisecond' attribute of this object.
int minute() const;
// Return the value of the 'minute' attribute of this object.
int month() const;
// Return the value of the 'month' attribute of this object.
int second() const;
// Return the value of the 'second' attribute of this object.
Time time() const;
// Return the value of the "time" part of this object.
int year() const;
// Return the value of the 'year' attribute of this object.
int printToBuffer(char *result,
int numBytes,
int fractionalSecondPrecision = 6) const;
// Efficiently write to the specified 'result' buffer no more than the
// specified 'numBytes' of a representation of the value of this
// object. Optionally specify 'fractionalSecondPrecision' digits to
// indicate how many fractional second digits to output. If
// 'fractionalSecondPrecision' is not specified then 6 fractional
// second digits will be output (3 digits for milliseconds and 3 digits
// for microseconds). Return the number of characters (not including
// the null character) that would have been written if the limit due to
// 'numBytes' were not imposed. 'result' is null-terminated unless
// 'numBytes' is 0. The behavior is undefined unless '0 <= numBytes',
// '0 <= fractionalSecondPrecision <= 6', and 'result' refers to at
// least 'numBytes' contiguous bytes. Note that the return value is
// greater than or equal to 'numBytes' if the output representation was
// truncated to avoid 'result' overrun.
// Aspects
template <class STREAM>
STREAM& bdexStreamOut(STREAM& stream, int version) const;
// Write the value of this object, using the specified 'version'
// format, to the specified output 'stream', and return a reference to
// 'stream'. If 'stream' is initially invalid, this operation has no
// effect. If 'version' is not supported, 'stream' is invalidated, but
// otherwise unmodified. Note that 'version' is not written to
// 'stream'. See the 'bslx' package-level documentation for more
// information on BDEX streaming of value-semantic types and
// containers.
bsl::ostream& print(bsl::ostream& stream,
int level = 0,
int spacesPerLevel = 4) const;
// Write the value of this object to the specified output 'stream' in a
// human-readable format, and return a reference to 'stream'.
// Optionally specify an initial indentation 'level', whose absolute
// value is incremented recursively for nested objects. If 'level' is
// specified, optionally specify 'spacesPerLevel', whose absolute value
// indicates the number of spaces per indentation level for this and
// all of its nested objects. If 'level' is negative, suppress
// indentation of the first line. If 'spacesPerLevel' is negative,
// format the entire output on one line, suppressing all but the
// initial indentation (as governed by 'level'). If 'stream' is not
// valid on entry, this operation has no effect. Note that this
// human-readable format is not fully specified, and can change without
// notice.
#ifndef BDE_OPENSOURCE_PUBLICATION // pending deprecation
static int maxSupportedBdexVersion();
// !DEPRECATED!: Use 'maxSupportedBdexVersion(int)' instead.
//
// Return the most current BDEX streaming version number supported by
// this class.
#endif // BDE_OPENSOURCE_PUBLICATION -- pending deprecation
#ifndef BDE_OMIT_INTERNAL_DEPRECATED // BDE2.22
static int maxSupportedVersion();
// !DEPRECATED!: Use 'maxSupportedBdexVersion(int)' instead.
//
// Return the most current BDEX streaming version number supported by
// this class.
bsl::ostream& streamOut(bsl::ostream& stream) const;
// !DEPRECATED!: Use 'print' instead.
//
// Format this datetime to the specified output 'stream' and return a
// reference to the modifiable 'stream'.
int validateAndSetDatetime(int year,
int month,
int day,
int hour = 0,
int minute = 0,
int second = 0,
int millisecond = 0);
// !DEPRECATED!: Use 'setDatetimeIfValid' instead.
//
// Set the "date" part of this object's value to the specified 'year',
// 'month', and 'day', and the "time" part to the optionally specified
// 'hour', 'minute', 'second', and 'millisecond', if they represent a
// valid 'Datetime' value, with trailing fields that are not specified
// set to 0. Return 0 on success, and a non-zero value (with no
// effect) otherwise.
#endif // BDE_OMIT_INTERNAL_DEPRECATED -- BDE2.22
};
// FREE OPERATORS
Datetime operator+(const Datetime& lhs, const bsls::TimeInterval& rhs);
// Return a 'Datetime' object having a value that is the sum of the
// specified 'lhs' ('Datetime') and the specified 'rhs'
// ('bsls::TimeInterval'). If '24 == lhs.hour()', the result is the same
// as if the 'hour' attribute of 'lhs' is 0. The behavior is undefined
// unless the resulting value is in the valid range for a 'Datetime'
// object.
Datetime operator+(const bsls::TimeInterval& lhs, const Datetime& rhs);
// Return a 'Datetime' object having a value that is the sum of the
// specified 'lhs' ('bsls::TimeInterval') and the specified 'rhs'
// ('Datetime'). If '24 == rhs.hour()', the result is the same as if the
// 'hour' attribute of 'rhs' is 0. The behavior is undefined unless the
// resulting value is in the valid range for a 'Datetime' object.
Datetime operator+(const Datetime& lhs, const DatetimeInterval& rhs);
// Return a 'Datetime' object having a value that is the sum of the
// specified 'lhs' ('Datetime') and the specified 'rhs'
// ('DatetimeInterval'). If '24 == lhs.hour()', the result is the same as
// if the 'hour' attribute of 'lhs' is 0. The behavior is undefined unless
// the resulting value is in the valid range for a 'Datetime' object.
Datetime operator+(const DatetimeInterval& lhs, const Datetime& rhs);
// Return a 'Datetime' object having a value that is the sum of the
// specified 'lhs' ('DatetimeInterval') and the specified 'rhs'
// ('Datetime'). If '24 == rhs.hour()', the result is the same as if the
// 'hour' attribute of 'rhs' is 0. The behavior is undefined unless the
// resulting value is in the valid range for a 'Datetime' object.
Datetime operator-(const Datetime& lhs, const bsls::TimeInterval& rhs);
// Return a 'Datetime' object having a value that is the difference between
// the specified 'lhs' ('Datetime') and the specified 'rhs'
// ('bsls::TimeInterval'). If '24 == lhs.hour()', the result is the same
// as if the 'hour' attribute of 'lhs' is 0. The behavior is undefined
// unless the resulting value is in the valid range for a 'Datetime'
// object.
Datetime operator-(const Datetime& lhs, const DatetimeInterval& rhs);
// Return a 'Datetime' object having a value that is the difference between
// the specified 'lhs' ('Datetime') and the specified 'rhs'
// ('DatetimeInterval'). If '24 == lhs.hour()', the result is the same as
// if the 'hour' attribute of 'lhs' is 0. The behavior is undefined unless
// the resulting value is in the valid range for a 'Datetime' object.
DatetimeInterval operator-(const Datetime& lhs, const Datetime& rhs);
// Return a 'DatetimeInterval' object having a value that is the difference
// between the specified 'lhs' ('Datetime') and the specified 'rhs'
// ('Datetime'). If the 'hour' attribute of either operand is 24, the
// result is the same as if that 'hour' attribute is 0. The behavior is
// undefined unless the resulting value is in the valid range for a
// 'DatetimeInterval' object.
bool operator==(const Datetime& lhs, const Datetime& rhs);
// Return 'true' if the specified 'lhs' and 'rhs' objects have the same
// value, and 'false' otherwise. Two 'Datetime' objects have the same
// value if they have the same values for their "date" and "time" parts,
// respectively.
bool operator!=(const Datetime& lhs, const Datetime& rhs);
// Return 'true' if the specified 'lhs' and 'rhs' 'Datetime' objects do not
// have the same value, and 'false' otherwise. Two 'Datetime' objects do
// not have the same value if they do not have the same values for either