Changeset 498
- Timestamp:
- 01/11/11 04:27:36 (14 years ago)
- Files:
-
- trunk/src/core/time.d (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/core/time.d
r494 r498 880 880 881 881 /++ 882 882 Returns the fractional seconds passed the second. 883 883 884 884 Examples: 885 885 -------------------- 886 886 assert(dur!"msecs"(1000).fracSec == FracSec.from!"msecs"(0)); 887 887 assert(dur!"msecs"(1217).fracSec == FracSec.from!"msecs"(217)); 888 888 assert(dur!"usecs"(43).fracSec == FracSec.from!"usecs"(43)); 889 889 assert(dur!"hnsecs"(50_007).fracSec == FracSec.from!"hnsecs"(50_007)); 890 assert(dur!"nsecs"(62_127).fracSec == FracSec.from!"nsecs"(62_100)); 890 891 -------------------- 891 892 +/ 892 893 @property FracSec fracSec() const pure nothrow 893 894 { 894 895 try 895 896 { 896 897 long hnsecs = _hnsecs; 897 898 auto days = splitUnitsFromHNSecs!"days"(hnsecs) + 1; 898 899 899 900 if(hnsecs < 0) … … 912 913 assert(0, "FracSec.from!\"hnsecs\"() threw."); 913 914 } 914 915 915 916 unittest 916 917 { 917 918 //Verify Examples. 918 919 assert(dur!"msecs"(1000).fracSec == FracSec.from!"msecs"(0)); 919 920 assert(dur!"msecs"(1217).fracSec == FracSec.from!"msecs"(217)); 920 921 assert(dur!"usecs"(43).fracSec == FracSec.from!"usecs"(43)); 921 922 assert(dur!"hnsecs"(50_007).fracSec == FracSec.from!"hnsecs"(50_007)); 923 assert(dur!"nsecs"(62_127).fracSec == FracSec.from!"nsecs"(62_100)); 922 924 923 925 const dur = Duration(12); 924 926 const cdur = Duration(12); 925 927 immutable idur = Duration(12); 926 928 static assert(__traits(compiles, dur.fracSec)); 927 929 static assert(__traits(compiles, cdur.fracSec)); 928 930 static assert(__traits(compiles, idur.fracSec)); 929 931 } 930 932 931 933 … … 1195 1197 long _hnsecs; 1196 1198 } 1197 1199 1198 1200 1199 1201 /++ 1200 1202 This allows you to construct a Duration from the given time units 1201 1203 with the given length. 1202 1204 1203 1205 The possible values for units are "weeks", "days", "hours", "minutes", 1204 1206 "seconds", "msecs" (milliseconds), "usecs", (microseconds), 1205 and "hnsecs" (hecto-nanoseconds, i.e. 100 ns).1207 "hnsecs" (hecto-nanoseconds, i.e. 100 ns), and "nsecs". 1206 1208 1207 1209 Params: 1208 1210 units = The time units of the duration (e.g. "days"). 1209 1211 length = The number of units in the duration. 1210 1212 +/ 1211 1213 @safe 1212 1214 Duration dur(string units)(long length) pure nothrow 1213 1215 if(units == "weeks" || 1214 1216 units == "days" || 1215 1217 units == "hours" || … … 1493 1495 assert(t.usecs == 1_999_999); 1494 1496 } 1495 1497 1496 1498 if(ticksPerSec >= 1_000_000) 1497 1499 assert(TickDuration.from!"usecs"(7).usecs == 7); 1498 1500 } 1499 1501 1500 1502 //test from!"hnsecs"(). 1501 1503 unittest 1502 1504 { 1503 auto t = TickDuration.from!"hnsecs"(10_000_000); 1504 assert(t.hnsecs == 10_000_000); 1505 t = TickDuration.from!"hnsecs"(20_000_000); 1506 assert(t.hnsecs == 20_000_000); 1507 1508 if(ticksPerSec == 1_000_000) 1505 //Skipping tests on Windows until properly robust tests 1506 //can be devised and tested on a Windows box. 1507 //The differences in ticksPerSec on Windows makes testing 1508 //exact values a bit precarious. 1509 version(Posix) 1509 1510 { 1510 t.length -= 1; 1511 assert(t.hnsecs == 19999990); 1512 assert(TickDuration.from!"hnsecs"(70).hnsecs == 70); 1513 assert(TickDuration.from!"hnsecs"(7).hnsecs == 0); 1511 auto t = TickDuration.from!"hnsecs"(10_000_000); 1512 assert(t.hnsecs == 10_000_000); 1513 t = TickDuration.from!"hnsecs"(20_000_000); 1514 assert(t.hnsecs == 20_000_000); 1515 1516 if(ticksPerSec == 1_000_000) 1517 { 1518 t.length -= 1; 1519 assert(t.hnsecs == 19999990); 1520 assert(TickDuration.from!"hnsecs"(70).hnsecs == 70); 1521 assert(TickDuration.from!"hnsecs"(7).hnsecs == 0); 1522 } 1523 1524 if(ticksPerSec >= 10_000_000) 1525 { 1526 t.length -= 1; 1527 assert(t.hnsecs == 19999999); 1528 assert(TickDuration.from!"hnsecs"(70).hnsecs == 70); 1529 assert(TickDuration.from!"hnsecs"(7).hnsecs == 7); 1530 } 1514 1531 } 1515 1516 if(ticksPerSec >= 10_000_000) 1532 } 1533 1534 //test from!"nsecs"(). 1535 unittest 1536 { 1537 //Skipping tests on Windows until properly robust tests 1538 //can be devised and tested on a Windows box. 1539 //The differences in ticksPerSec on Windows makes testing 1540 //exact values a bit precarious. 1541 version(Posix) 1517 1542 { 1518 t.length -= 1; 1519 assert(t.hnsecs == 19999999); 1520 assert(TickDuration.from!"hnsecs"(70).hnsecs == 70); 1521 assert(TickDuration.from!"hnsecs"(7).hnsecs == 7); 1522 } 1523 } 1524 1525 //test from!"nsecs"(). 1526 unittest 1527 { 1528 auto t = TickDuration.from!"nsecs"(1_000_000_000); 1529 assert(t.nsecs == 1_000_000_000); 1530 t = TickDuration.from!"nsecs"(2_000_000_000); 1531 assert(t.nsecs == 2_000_000_000); 1532 1533 if(ticksPerSec == 1_000_000) 1534 { 1535 t.length -= 1; 1536 assert(t.nsecs == 1999999000); 1537 } 1538 1539 if(ticksPerSec >= 1_000_000_000) 1540 { 1541 t.length -= 1; 1542 assert(t.nsecs == 1999999999); 1543 auto t = TickDuration.from!"nsecs"(1_000_000_000); 1544 assert(t.nsecs == 1_000_000_000); 1545 t = TickDuration.from!"nsecs"(2_000_000_000); 1546 assert(t.nsecs == 2_000_000_000); 1547 1548 if(ticksPerSec == 1_000_000) 1549 { 1550 t.length -= 1; 1551 assert(t.nsecs == 1999999000); 1552 } 1553 1554 if(ticksPerSec >= 1_000_000_000) 1555 { 1556 t.length -= 1; 1557 assert(t.nsecs == 1999999999); 1558 } 1543 1559 } 1544 1560 } 1545 1561 1546 1562 1547 1563 /++ 1548 1564 Returns a Duration with the same number of hnsecs as this TickDuration. 1549 1565 +/ 1550 1566 Duration opCast(T)() const pure nothrow 1551 1567 if(is(T == Duration)) 1552 1568 { … … 2456 2472 2457 2473 Note that this does not give you any greater precision 2458 2474 than setting the value of this FracSec as hnsecs. 2459 2475 2460 2476 Params: 2461 2477 nsecs = The number of nsecs passed the second. 2462 2478 2463 2479 Throws: 2464 2480 TimeException if the given value is not less than one second. 2465 2481 +/ 2466 @property void nsecs( intnsecs) pure2482 @property void nsecs(long nsecs) pure 2467 2483 { 2468 2484 //So that -99 through -1 throw instead of result in FracSec(0). 2469 2485 if(nsecs < 0) 2470 2486 _enforceValid(-1); 2471 2487 2472 2488 immutable hnsecs = cast(int)convert!("nsecs", "hnsecs")(nsecs); 2473 2489 2474 2490 _enforceValid(hnsecs); 2475 2491 _hnsecs = hnsecs; 2476 2492 } 2477 2493 2478 2494 unittest 2479 2495 { 2480 static void testFS( intnsecs, in FracSec expected = FracSec.init, size_t line = __LINE__)2496 static void testFS(long nsecs, in FracSec expected = FracSec.init, size_t line = __LINE__) 2481 2497 { 2482 2498 FracSec fs; 2483 2499 2484 2500 fs.nsecs = nsecs; 2485 2501 2486 2502 if(fs != expected) 2487 2503 throw new AssertError("", __FILE__, line); 2488 2504 } 2489 2505 2490 2506 tAssertExThrown!TimeException(testFS(-1));
