-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertX.cpp
More file actions
821 lines (702 loc) · 25 KB
/
Copy pathConvertX.cpp
File metadata and controls
821 lines (702 loc) · 25 KB
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
#include "ConvertX.hpp"
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <stdexcept>
namespace
{
// Time unit boundaries for validation
bool IsTimeUnit(ConvertX::UnitTag unit)
{
return unit >= ConvertX::UnitTag::TIMESTART && unit <= ConvertX::UnitTag::TIMEEND;
}
// Text unit boundaries for validation
bool IsTextUnit(ConvertX::UnitTag unit)
{
return unit >= ConvertX::UnitTag::TEXTSTART && unit <= ConvertX::UnitTag::TEXTEND;
}
// Helper functions for text conversions
bool TryGetNextCodePoint(const tstring& text, size_t& index, unsigned int& value); // Attempts to parse the next Unicode code point from the text starting at index
tstring FormatIntegerBase(long long value, unsigned int base); // Formats an integer value in the specified base (2, 8, 10, or 16)
tstring CodePointToString(unsigned int codePoint); // Converts a Unicode code point to a string (UTF-8 or UTF-16 depending on platform)
// Helper functions for parsing numeric values with trailing whitespace validation
bool IsWhitespace(tstring::value_type ch)
{
return ch == static_cast<tstring::value_type>(' ')
|| ch == static_cast<tstring::value_type>('\t')
|| ch == static_cast<tstring::value_type>('\r')
|| ch == static_cast<tstring::value_type>('\n');
}
// Validates that the substring of `text` starting at `startIndex` consists only of whitespace characters.
bool HasOnlyTrailingWhitespace(const tstring& text, size_t startIndex)
{
for (size_t i = startIndex; i < text.length(); ++i)
{
if (!IsWhitespace(text[i]))
{
return false;
}
}
return true;
}
// Attempts to parse a double value from the text, ensuring that any remaining characters after the parsed number are only whitespace.
bool TryParseDouble(const tstring& text, double& value)
{
try
{
size_t processed = 0;
value = std::stod(text, &processed);
return HasOnlyTrailingWhitespace(text, processed);
}
catch (const std::exception&)
{
value = 0.0;
return false;
}
}
// Attempts to parse an unsigned integer value from the text, ensuring that any remaining characters after the parsed number are only whitespace and that the value does not exceed the maximum for a Unicode code point.
bool TryParseCodePoint(const tstring& text, unsigned int& value)
{
try
{
size_t processed = 0;
const unsigned long long parsed = std::stoull(text, &processed, 10);
if (!HasOnlyTrailingWhitespace(text, processed))
{
value = 0;
return false;
}
#if _UNICODE
if (parsed > 0x10FFFFull)
#else
if (parsed > 0xFFull)
#endif
{
value = 0;
return false;
}
value = static_cast<unsigned int>(parsed);
return true;
}
catch (const std::exception&)
{
value = 0;
return false;
}
}
// Attempts to parse an unsigned integer value from the text in the specified base, ensuring that any remaining characters after the parsed number are only whitespace and that the value does not exceed the maximum for a 32-bit unsigned integer.
bool TryParseUnsignedInteger(const tstring& text, unsigned int& value, int base = 10)
{
try
{
size_t processed = 0;
const unsigned long long parsed = std::stoull(text, &processed, base);
if (!HasOnlyTrailingWhitespace(text, processed) || parsed > 0xFFFFFFFFull)
{
value = 0;
return false;
}
value = static_cast<unsigned int>(parsed);
return true;
}
catch (const std::exception&)
{
value = 0;
return false;
}
}
// Converts a plain string to an encoded representation of its Unicode code points in the specified base (2, 8, or 16). Each code point is separated by a space in the output.
tstring ConvertStringToEncodedCodePoints(const tstring& value, unsigned int base)
{
if (value.empty())
{
return tstring();
}
tstring result;
size_t index = 0;
bool first = true;
while (index < value.length())
{
unsigned int codePoint = 0;
if (!TryGetNextCodePoint(value, index, codePoint))
{
return tstring();
}
if (!first)
{
result += static_cast<tstring::value_type>(' ');
}
const tstring encoded = FormatIntegerBase(static_cast<long long>(codePoint), base);
result.append(encoded);
first = false;
}
return result;
}
// Converts an encoded string of Unicode code points in the specified base (2, 8, or 16) back to a plain string. The input should consist of code point values separated by spaces. Returns an empty string if any token fails to parse or if any code point is invalid.
tstring ConvertEncodedCodePointsToString(const tstring& value, int base)
{
if (value.empty())
{
return tstring();
}
std::basic_istringstream<tstring::value_type> stream(value);
tstring token;
tstring result;
bool foundToken = false;
while (stream >> token)
{
unsigned int codePoint = 0;
if (!TryParseUnsignedInteger(token, codePoint, base))
{
return tstring();
}
const tstring decoded = CodePointToString(codePoint);
if (decoded.empty())
{
return tstring();
}
result.append(decoded);
foundToken = true;
}
return foundToken ? result : tstring();
}
// Formats a signed integer value as a string in base 10.
tstring FormatSignedInteger(long long value)
{
std::basic_ostringstream<tstring::value_type> stream;
stream << value;
return stream.str();
}
// Formats a signed integer value as a string in the specified base (2, 8, or 16). Returns an empty string if the base is invalid.
tstring FormatIntegerBase(long long value, unsigned int base)
{
if (base < 2 || base > 16)
{
return tstring();
}
#if _UNICODE
const wchar_t* digits = L"0123456789ABCDEF";
#else
const char* digits = "0123456789ABCDEF";
#endif
const bool negative = value < 0;
unsigned long long magnitude = negative
? static_cast<unsigned long long>(-(value + 1)) + 1ull
: static_cast<unsigned long long>(value);
tstring result;
do
{
result.push_back(static_cast<tstring::value_type>(digits[magnitude % base]));
magnitude /= base;
} while (magnitude > 0);
if (negative)
{
result.push_back(static_cast<tstring::value_type>('-'));
}
std::reverse(result.begin(), result.end());
return result;
}
// Attempts to parse a single Unicode code point from the input text.
// The text should represent exactly one code point, which can be either a single character (for code points in the BMP) or a surrogate pair (for code points above U+FFFF).
// Returns `true` if parsing is successful and sets `value` to the parsed code point; otherwise, returns `false` and sets `value` to 0.
bool TryGetSingleCodePoint(const tstring& text, unsigned int& value)
{
value = 0;
#if _UNICODE
if (text.empty())
{
return false;
}
if (text.length() == 1)
{
const wchar_t ch = text[0];
if (ch >= 0xD800 && ch <= 0xDFFF)
{
return false;
}
value = static_cast<unsigned int>(ch);
return true;
}
if (text.length() == 2)
{
const wchar_t high = text[0];
const wchar_t low = text[1];
if (high >= 0xD800 && high <= 0xDBFF
&& low >= 0xDC00 && low <= 0xDFFF)
{
value = 0x10000u
+ ((static_cast<unsigned int>(high) - 0xD800u) << 10)
+ (static_cast<unsigned int>(low) - 0xDC00u);
return true;
}
}
return false;
#else
if (text.length() != 1)
{
return false;
}
value = static_cast<unsigned char>(text[0]);
return true;
#endif
}
// Attempts to parse a signed integer value from the input text in the specified base, ensuring that any remaining characters after the parsed number are only whitespace.
// Returns `true` if parsing is successful; otherwise, returns `false` and sets `value` to 0.
bool TryParseSignedInteger(const tstring& text, long long& value, int base = 10)
{
try
{
size_t processed = 0;
value = std::stoll(text, &processed, base);
return HasOnlyTrailingWhitespace(text, processed);
}
catch (const std::exception&)
{
value = 0;
return false;
}
}
// Formats a double value as a string with up to 15 significant digits, ensuring that the output is precise enough to round-trip back to the same value when parsed.
tstring FormatDouble(double value)
{
std::basic_ostringstream<tstring::value_type> stream;
stream << std::setprecision(15) << value;
return stream.str();
}
// Formats an unsigned integer value as a string in base 10.
tstring FormatUnsignedInt(unsigned int value)
{
std::basic_ostringstream<tstring::value_type> stream;
stream << value;
return stream.str();
}
// Converts a Unicode code point to a string. For code points in the BMP (U+0000 to U+FFFF), returns a single-character string.
// For code points above U+FFFF, returns a surrogate pair string in UTF-16.
// Returns an empty string if the code point is invalid.
tstring CodePointToString(unsigned int codePoint)
{
#if _UNICODE
if (codePoint > 0x10FFFFu || (codePoint >= 0xD800u && codePoint <= 0xDFFFu))
{
return tstring();
}
if (codePoint <= 0xFFFFu)
{
return tstring(1, static_cast<wchar_t>(codePoint));
}
codePoint -= 0x10000u;
const wchar_t highSurrogate = static_cast<wchar_t>(0xD800u + (codePoint >> 10));
const wchar_t lowSurrogate = static_cast<wchar_t>(0xDC00u + (codePoint & 0x3FFu));
tstring result;
result.push_back(highSurrogate);
result.push_back(lowSurrogate);
return result;
#else
if (codePoint > 0xFFu)
{
return tstring();
}
return tstring(1, static_cast<char>(codePoint));
#endif
}
// Attempts to parse the next Unicode code point from the input text starting at the specified index. Updates the index to point to the position after the parsed code point.
bool TryGetNextCodePoint(const tstring& text, size_t& index, unsigned int& value)
{
value = 0;
if (index >= text.length())
{
return false;
}
#if _UNICODE
const wchar_t ch = text[index];
if (ch >= 0xD800 && ch <= 0xDBFF)
{
if ((index + 1) >= text.length())
{
return false;
}
const wchar_t low = text[index + 1];
if (low < 0xDC00 || low > 0xDFFF)
{
return false;
}
value = 0x10000u
+ ((static_cast<unsigned int>(ch) - 0xD800u) << 10)
+ (static_cast<unsigned int>(low) - 0xDC00u);
index += 2;
return true;
}
if (ch >= 0xDC00 && ch <= 0xDFFF)
{
return false;
}
value = static_cast<unsigned int>(ch);
++index;
return true;
#else
value = static_cast<unsigned char>(text[index]);
++index;
return true;
#endif
}
}
// Constructor for ConvertX class. Currently does not perform any specific initialization, but is defined to allow for proper resource management if needed in the future.
ConvertX::ConvertX()
{
}
// Destructor for ConvertX class. Currently does not perform any specific cleanup, but is defined to allow for proper resource management if needed in the future.
ConvertX::~ConvertX()
{
}
// Main conversion function that takes an input value as a string, the source unit type, and the target unit type. It determines the appropriate conversion method based on the unit types and attempts to perform the conversion. If the conversion is successful, it returns the converted value as a string; otherwise, it returns an empty string to indicate failure.
tstring ConvertX::SmartConvert(const tstring& value, UnitTag fromType, UnitTag toType)
{
if (!IsValidUnits(fromType, toType))
{
return tstring();
}
switch (DetectConversionType(value, fromType, toType))
{
case ConversionType::Time:
{
double numericValue = 0.0;
if (!TryParseDouble(value, numericValue))
{
return tstring();
}
Time timeConverter;
return FormatDouble(timeConverter.Convert(numericValue, fromType, toType));
}
case ConversionType::Text:
{
Text textConverter;
return textConverter.Convert(value, fromType, toType);
}
default:
return tstring();
}
}
// Determines the conversion type (Time or Text) based on the unit types and the content of the input value. If both unit types are time units, it returns Time. If both unit types are text units, it returns Text. If the value can be parsed as a double, it assumes it's a time conversion; otherwise, it defaults to text conversion.
ConvertX::ConversionType ConvertX::DetectConversionType(const tstring& value, UnitTag fromType, UnitTag toType)
{
if (IsTimeUnit(fromType) && IsTimeUnit(toType))
{
return ConversionType::Time;
}
if (IsTextUnit(fromType) && IsTextUnit(toType))
{
return ConversionType::Text;
}
double numericValue = 0.0;
if (TryParseDouble(value, numericValue))
{
return ConversionType::Time;
}
return ConversionType::Text;
}
// Validates that the provided unit types are compatible for conversion. It returns true if both unit types are time units or if both unit types are text units; otherwise, it returns false.
bool ConvertX::IsValidUnits(UnitTag fromType, UnitTag toType)
{
return (IsTimeUnit(fromType) && IsTimeUnit(toType))
|| (IsTextUnit(fromType) && IsTextUnit(toType));
}
// Main conversion function for text conversions. It checks the source and target unit types and delegates to the appropriate conversion methods.
// If the source and target types are the same, it returns the input value unchanged. If the source type is a string, it converts from string to the target type. If the target type is a string, it converts from the source type to string. For other combinations, it first converts from the source type to string and then from string to the target type.
tstring ConvertX::Text::Convert(const tstring& value, UnitTag fromType, UnitTag toType)
{
if (fromType == toType)
{
return value;
}
if (fromType == UnitTag::String)
{
return FromString::Convert(value, toType);
}
if (toType == UnitTag::String)
{
return ToString::Convert(value, fromType);
}
const tstring intermediate = ToString::Convert(value, fromType);
if (intermediate.empty() && !value.empty())
{
return tstring();
}
return FromString::Convert(intermediate, toType);
}
// Converts a plain string to the specified text unit type. If the target type is String, it returns the input value unchanged.
// For other target types, it converts the string to the corresponding encoded representation of its Unicode code points.
tstring ConvertX::Text::FromString::Convert(const tstring& value, UnitTag toType)
{
switch (toType)
{
case UnitTag::String:
return value;
case UnitTag::CodePoints:
return ToCodePoints(value);
case UnitTag::BinaryString:
return ToBinaryString(value);
case UnitTag::OctalString:
return ToOctalString(value);
case UnitTag::HexadecimalString:
return ToHexadecimalString(value);
default:
return tstring();
}
}
// Converts a plain string to a space-separated string of Unicode code point values in decimal.
// Each code point in the input string is parsed and converted to its corresponding unsigned integer value, which is then formatted as a decimal string and concatenated to the result with spaces in between.
// If any code point cannot be parsed, the function returns an empty string to indicate failure.
tstring ConvertX::Text::FromString::ToCodePoints(const tstring& value)
{
if (value.empty())
{
return tstring();
}
tstring result;
size_t index = 0;
bool first = true;
while (index < value.length())
{
unsigned int codePoint = 0;
if (!TryGetNextCodePoint(value, index, codePoint))
{
return tstring();
}
if (!first)
{
result += static_cast<tstring::value_type>(' ');
}
result += FormatUnsignedInt(codePoint);
first = false;
}
return result;
}
// Converts a plain string to a space-separated string of Unicode code point values in binary (base 2).
// Each code point in the input string is parsed and converted to its corresponding unsigned integer value, which is then formatted as a binary string and concatenated to the result with spaces in between.
// If any code point cannot be parsed, the function returns an empty string to indicate failure.
tstring ConvertX::Text::FromString::ToBinaryString(const tstring& value)
{
return ConvertStringToEncodedCodePoints(value, 2);
}
// Converts a plain string to a space-separated string of Unicode code point values in octal (base 8).
tstring ConvertX::Text::FromString::ToOctalString(const tstring& value)
{
return ConvertStringToEncodedCodePoints(value, 8);
}
// Converts a plain string to a space-separated string of Unicode code point values in hexadecimal (base 16).
tstring ConvertX::Text::FromString::ToHexadecimalString(const tstring& value)
{
return ConvertStringToEncodedCodePoints(value, 16);
}
// Converts a string representation of a text unit type (such as CodePoints, BinaryString, OctalString, or HexadecimalString) back to a plain string.
// The input value should be a space-separated string of code point values in the corresponding format.
tstring ConvertX::Text::ToString::Convert(const tstring& value, UnitTag fromType)
{
switch (fromType)
{
case UnitTag::String:
return value;
case UnitTag::CodePoints:
return FromCodePoints(value);
case UnitTag::BinaryString:
return FromBinaryString(value);
case UnitTag::OctalString:
return FromOctalString(value);
case UnitTag::HexadecimalString:
return FromHexadecimalString(value);
default:
return tstring();
}
}
// Converts a space-separated string of Unicode code point values in decimal back to a plain string.
// Each token in the input string is parsed as an unsigned integer code point value, which is then converted to its corresponding character(s) and concatenated to the result.
tstring ConvertX::Text::ToString::FromCodePoints(const tstring& value)
{
if (value.empty())
{
return tstring();
}
std::basic_istringstream<tstring::value_type> stream(value);
tstring token;
tstring result;
bool foundToken = false;
while (stream >> token)
{
unsigned int codePoint = 0;
if (!TryParseCodePoint(token, codePoint))
{
return tstring();
}
const tstring decoded = CodePointToString(codePoint);
if (decoded.empty())
{
return tstring();
}
result += decoded;
foundToken = true;
}
return foundToken ? result : tstring();
}
// Converts a space-separated string of Unicode code point values in binary (base 2) back to a plain string.
// Each token in the input string is parsed as an unsigned integer code point value in binary, which is then converted to its corresponding character(s) and concatenated to the result.
tstring ConvertX::Text::ToString::FromBinaryString(const tstring& value)
{
return ConvertEncodedCodePointsToString(value, 2);
}
// Converts a space-separated string of Unicode code point values in octal (base 8) back to a plain string.
// Each token in the input string is parsed as an unsigned integer code point value in octal, which is then converted to its corresponding character(s) and concatenated to the result.
tstring ConvertX::Text::ToString::FromOctalString(const tstring& value)
{
return ConvertEncodedCodePointsToString(value, 8);
}
// Converts a space-separated string of Unicode code point values in hexadecimal (base 16) back to a plain string.
// Each token in the input string is parsed as an unsigned integer code point value in hexadecimal, which is then converted to its corresponding character(s) and concatenated to the result.
tstring ConvertX::Text::ToString::FromHexadecimalString(const tstring& value)
{
return ConvertEncodedCodePointsToString(value, 16);
}
// Main conversion function for time conversions. It first converts the input value from the source unit type to seconds, and then converts the resulting seconds value to the target unit type.
// This approach centralizes the conversion logic around seconds as a common intermediate unit, simplifying the implementation of individual unit conversions.
double ConvertX::Time::Convert(double value, UnitTag fromType, UnitTag toType)
{
return FromSeconds::Convert(ToSeconds::Convert(value, fromType), toType);
}
// Converts a time value in seconds to the specified target time unit type.
// It uses a switch statement to determine the appropriate conversion method based on the target unit type and returns the converted value. If the target unit type is not recognized, it returns 0.0 to indicate failure.
double ConvertX::Time::FromSeconds::Convert(double seconds, UnitTag toType)
{
switch (toType)
{
case UnitTag::Seconds:
return seconds;
case UnitTag::Minutes:
return ToMinutes(seconds);
case UnitTag::Hours:
return ToHours(seconds);
case UnitTag::Days:
return ToDays(seconds);
case UnitTag::Weeks:
return ToWeeks(seconds);
case UnitTag::Months:
return ToMonths(seconds);
case UnitTag::Years:
return ToYears(seconds);
case UnitTag::Milliseconds:
return ToMilliseconds(seconds);
case UnitTag::Microseconds:
return ToMicroseconds(seconds);
default:
return 0.0;
}
}
// Converts a time value in seconds to years by dividing the input seconds by the number of seconds in a year.
// It ensures that the input seconds value is non-negative by using std::max to return 0.0 if the input is negative.
double ConvertX::Time::FromSeconds::ToYears(double seconds)
{
return std::max(seconds, 0.0) / SecondsInYear;
}
// Converts a time value in seconds to months by dividing the input seconds by the number of seconds in a month.
// It ensures that the input seconds value is non-negative by using std::max to return 0.0 if the input is negative.
double ConvertX::Time::FromSeconds::ToMonths(double seconds)
{
return std::max(seconds, 0.0) / SecondsInMonth;
}
// Converts a time value in seconds to weeks by dividing the input seconds by the number of seconds in a week.
double ConvertX::Time::FromSeconds::ToWeeks(double seconds)
{
return std::max(seconds, 0.0) / SecondsInWeek;
}
// Converts a time value in seconds to days by dividing the input seconds by the number of seconds in a day.
double ConvertX::Time::FromSeconds::ToDays(double seconds)
{
return std::max(seconds, 0.0) / SecondsInDay;
}
// Converts a time value in seconds to hours by dividing the input seconds by the number of seconds in an hour.
double ConvertX::Time::FromSeconds::ToHours(double seconds)
{
return std::max(seconds, 0.0) / SecondsInHour;
}
// Converts a time value in seconds to minutes by dividing the input seconds by the number of seconds in a minute.
double ConvertX::Time::FromSeconds::ToMinutes(double seconds)
{
return std::max(seconds, 0.0) / SecondsInMinute;
}
// Converts a time value in seconds to milliseconds by dividing the input seconds by the number of seconds in a millisecond.
double ConvertX::Time::FromSeconds::ToMilliseconds(double seconds)
{
return std::max(seconds, 0.0) / SecondsInMillisecond;
}
// Converts a time value in seconds to microseconds by dividing the input seconds by the number of seconds in a microsecond.
double ConvertX::Time::FromSeconds::ToMicroseconds(double seconds)
{
return std::max(seconds, 0.0) / SecondsInMicrosecond;
}
// Converts a time value from the specified source unit type to seconds.
// It uses a switch statement to determine the appropriate conversion method based on the source unit type and returns the converted value in seconds.
// If the source unit type is not recognized, it returns 0.0 to indicate failure.
double ConvertX::Time::ToSeconds::Convert(double value, UnitTag fromType)
{
switch (fromType)
{
case UnitTag::Seconds:
return value;
case UnitTag::Minutes:
return FromMinutes(value);
case UnitTag::Hours:
return FromHours(value);
case UnitTag::Days:
return FromDays(value);
case UnitTag::Weeks:
return FromWeeks(value);
case UnitTag::Months:
return FromMonths(value);
case UnitTag::Years:
return FromYears(value);
case UnitTag::Milliseconds:
return FromMilliseconds(value);
case UnitTag::Microseconds:
return FromMicroseconds(value);
default:
return 0.0;
}
}
// Converts a time value in years to seconds by multiplying the input years by the number of seconds in a year.
double ConvertX::Time::ToSeconds::FromYears(double years)
{
return std::max(years, 0.0) * SecondsInYear;
}
// Converts a time value in months to seconds by multiplying the input months by the number of seconds in a month.
double ConvertX::Time::ToSeconds::FromMonths(double months)
{
return std::max(months, 0.0) * SecondsInMonth;
}
// Converts a time value in weeks to seconds by multiplying the input weeks by the number of seconds in a week.
double ConvertX::Time::ToSeconds::FromWeeks(double weeks)
{
return std::max(weeks, 0.0) * SecondsInWeek;
}
// Converts a time value in days to seconds by multiplying the input days by the number of seconds in a day.
double ConvertX::Time::ToSeconds::FromDays(double days)
{
return std::max(days, 0.0) * SecondsInDay;
}
// Converts a time value in hours to seconds by multiplying the input hours by the number of seconds in an hour.
double ConvertX::Time::ToSeconds::FromHours(double hours)
{
return std::max(hours, 0.0) * SecondsInHour;
}
// Converts a time value in minutes to seconds by multiplying the input minutes by the number of seconds in a minute.
double ConvertX::Time::ToSeconds::FromMinutes(double minutes)
{
return std::max(minutes, 0.0) * SecondsInMinute;
}
// Converts a time value in milliseconds to seconds by multiplying the input milliseconds by the number of seconds in a millisecond.
double ConvertX::Time::ToSeconds::FromMilliseconds(double milliseconds)
{
return std::max(milliseconds, 0.0) * SecondsInMillisecond;
}
// Converts a time value in microseconds to seconds by multiplying the input microseconds by the number of seconds in a microsecond.
double ConvertX::Time::ToSeconds::FromMicroseconds(double microseconds)
{
return std::max(microseconds, 0.0) * SecondsInMicrosecond;
}