Changeset 70895 in vbox for trunk/src/VBox/Runtime/common/time
- Timestamp:
- Feb 7, 2018 11:14:02 PM (7 years ago)
- svn:sync-xref-src-repo-rev:
- 120721
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/time/time.cpp
r69111 r70895 56 56 /** The min nano second into the min day. (1677-09-21T00-12-43.145224192) */ 57 57 #define RTTIME_MIN_DAY_NANO ( INT64_C(1000000000) * (00*3600 + 12*60 + 43) + 145224192 ) 58 59 /** 60 * Asserts that a_pTime is normalized. 61 */ 62 #define RTTIME_ASSERT_NORMALIZED(a_pTime) \ 63 do \ 64 { \ 65 Assert(RT_ABS((a_pTime)->offUTC) <= 840); \ 66 Assert((a_pTime)->u32Nanosecond < 1000000000); \ 67 Assert((a_pTime)->u8Second < 60); \ 68 Assert((a_pTime)->u8Minute < 60); \ 69 Assert((a_pTime)->u8Hour < 24); \ 70 Assert((a_pTime)->u8Month >= 1 && (a_pTime)->u8Month <= 12); \ 71 Assert((a_pTime)->u8WeekDay < 7); \ 72 Assert((a_pTime)->u16YearDay >= 1); \ 73 Assert((a_pTime)->u16YearDay <= (rtTimeIsLeapYear((a_pTime)->i32Year) ? 366 : 365)); \ 74 Assert((a_pTime)->u8MonthDay >= 1 && (a_pTime)->u8MonthDay <= 31); \ 75 } while (0) 58 76 59 77 … … 906 924 RT_EXPORT_SYMBOL(RTTimeSpecFromString); 907 925 926 927 /** 928 * Adds one day to @a pTime. 929 * 930 * ASSUMES it is zulu time so DST can be ignored. 931 */ 932 static PRTTIME rtTimeAdd1Day(PRTTIME pTime) 933 { 934 Assert(!pTime->offUTC); 935 rtTimeNormalizeInternal(pTime); 936 pTime->u8MonthDay += 1; 937 pTime->u16YearDay = 0; 938 return rtTimeNormalizeInternal(pTime); 939 } 940 941 942 /** 943 * Subtracts one day from @a pTime. 944 * 945 * ASSUMES it is zulu time so DST can be ignored. 946 */ 947 static PRTTIME rtTimeSub1Day(PRTTIME pTime) 948 { 949 Assert(!pTime->offUTC); 950 rtTimeNormalizeInternal(pTime); 951 if (pTime->u16YearDay > 1) 952 { 953 pTime->u16YearDay -= 0; 954 pTime->u8Month = 0; 955 pTime->u8MonthDay = 0; 956 } 957 else 958 { 959 pTime->i32Year -= 1; 960 pTime->u16YearDay = rtTimeIsLeapYear(pTime->i32Year) ? 366 : 365; 961 pTime->u8MonthDay = 31; 962 pTime->u8Month = 12; 963 } 964 return rtTimeNormalizeInternal(pTime); 965 } 966 967 968 /** 969 * Adds a signed number of minutes to @a pTime. 970 * 971 * ASSUMES it is zulu time so DST can be ignored. 972 * 973 * @param pTime The time structure to work on. 974 * @param cAddend Number of minutes to add. 975 * ASSUMES the value isn't all that high! 976 */ 977 static PRTTIME rtTimeAddMinutes(PRTTIME pTime, int32_t cAddend) 978 { 979 Assert(RT_ABS(cAddend) < 31 * 24 * 60); 980 981 /* 982 * Work on minutes of the day. 983 */ 984 unsigned const cMinutesInDay = 24 * 60; 985 int32_t iDayMinute = (unsigned)pTime->u8Hour * 60 + pTime->u8Minute; 986 iDayMinute += cAddend; 987 988 while (iDayMinute >= cMinutesInDay) 989 { 990 rtTimeAdd1Day(pTime); 991 iDayMinute -= cMinutesInDay; 992 } 993 994 while (iDayMinute < 0) 995 { 996 rtTimeSub1Day(pTime); 997 iDayMinute += cMinutesInDay; 998 } 999 1000 pTime->u8Hour = iDayMinute / 60; 1001 pTime->u8Minute = iDayMinute % 60; 1002 1003 return pTime; 1004 } 1005 1006 1007 /** 1008 * Converts @a pTime to zulu time (UTC) if needed. 1009 * 1010 * @returns pTime. 1011 * @param pTime What to convers (in/out). 1012 */ 1013 static PRTTIME rtTimeConvertToZulu(PRTTIME pTime) 1014 { 1015 RTTIME_ASSERT_NORMALIZED(pTime); 1016 if ((pTime->fFlags & RTTIME_FLAGS_TYPE_MASK) != RTTIME_FLAGS_TYPE_UTC) 1017 { 1018 int32_t offUTC = pTime->offUTC; 1019 pTime->offUTC = 0; 1020 pTime->fFlags &= ~RTTIME_FLAGS_TYPE_MASK; 1021 pTime->fFlags |= RTTIME_FLAGS_TYPE_UTC; 1022 if (offUTC != 0) 1023 rtTimeAddMinutes(pTime, offUTC); 1024 } 1025 return pTime; 1026 } 1027 1028 1029 /** 1030 * Compares two normalized time structures. 1031 * 1032 * @retval 0 if equal. 1033 * @retval -1 if @a pLeft is earlier than @a pRight. 1034 * @retval 1 if @a pRight is earlier than @a pLeft. 1035 * 1036 * @param pLeft The left side time. NULL is accepted. 1037 * @param pRight The right side time. NULL is accepted. 1038 * 1039 * @note A NULL time is considered smaller than anything else. If both are 1040 * NULL, they are considered equal. 1041 */ 1042 RTDECL(int) RTTimeCompare(PCRTTIME pLeft, PCRTTIME pRight) 1043 { 1044 #ifdef RT_STRICT 1045 if (pLeft) 1046 RTTIME_ASSERT_NORMALIZED(pLeft); 1047 if (pRight) 1048 RTTIME_ASSERT_NORMALIZED(pRight); 1049 #endif 1050 1051 int iRet; 1052 if (pLeft) 1053 { 1054 if (pRight) 1055 { 1056 /* 1057 * Only work with normalized zulu time. 1058 */ 1059 RTTIME TmpLeft; 1060 if ( pLeft->offUTC != 0 1061 || pLeft->u16YearDay == 0 1062 || pLeft->u16YearDay > 366 1063 || pLeft->u8Hour >= 60 1064 || pLeft->u8Minute >= 60 1065 || pLeft->u8Second >= 60) 1066 { 1067 TmpLeft = *pLeft; 1068 pLeft = rtTimeConvertToZulu(rtTimeNormalizeInternal(&TmpLeft)); 1069 } 1070 1071 RTTIME TmpRight; 1072 if ( pRight->offUTC != 0 1073 || pRight->u16YearDay == 0 1074 || pRight->u16YearDay > 366 1075 || pRight->u8Hour >= 60 1076 || pRight->u8Minute >= 60 1077 || pRight->u8Second >= 60) 1078 { 1079 TmpRight = *pRight; 1080 pRight = rtTimeConvertToZulu(rtTimeNormalizeInternal(&TmpRight)); 1081 } 1082 1083 /* 1084 * Do the comparison. 1085 */ 1086 if ( pLeft->i32Year != pRight->i32Year) 1087 iRet = pLeft->i32Year < pRight->i32Year ? -1 : 1; 1088 else if ( pLeft->u16YearDay != pRight->u16YearDay) 1089 iRet = pLeft->u16YearDay < pRight->u16YearDay ? -1 : 1; 1090 else if ( pLeft->u8Hour != pRight->u8Hour) 1091 iRet = pLeft->u8Hour < pRight->u8Hour ? -1 : 1; 1092 else if ( pLeft->u8Minute != pRight->u8Minute) 1093 iRet = pLeft->u8Minute < pRight->u8Minute ? -1 : 1; 1094 else if ( pLeft->u8Second != pRight->u8Second) 1095 iRet = pLeft->u8Second < pRight->u8Second ? -1 : 1; 1096 else if ( pLeft->u32Nanosecond != pRight->u32Nanosecond) 1097 iRet = pLeft->u32Nanosecond < pRight->u32Nanosecond ? -1 : 1; 1098 else 1099 iRet = 0; 1100 } 1101 else 1102 iRet = 1; 1103 } 1104 else 1105 iRet = pRight ? -1 : 0; 1106 return iRet; 1107 } 1108 RT_EXPORT_SYMBOL(RTTimeCompare); 1109
Note:
See TracChangeset
for help on using the changeset viewer.