001:
002:
003:
004:
005:
006:
007:
008:
009:
010:
011:
012:
013:
014:
015:
016:
017:
018:
019:
020:
021:
022:
023:
024:
025:
026:
027:
028:
029:
030:
031:
032:
033:
034:
035:
036:
037:
038:
039:
040:
041:
042:
043:
044:
045:
046:
047:
048:
049:
050:
051:
052:
053:
054:
055:
056:
057:
058:
059:
060:
061:
062:
063:
064:
065:
066:
067:
068:
069:
070:
071:
072:
073:
074:
075:
076:
077:
078:
079:
080:
081:
082:
083:
084:
085:
086:
087:
088:
089:
090:
091:
092:
093:
094:
095:
096:
097:
098:
099:
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:


/*
* Copyright (c), Zeriph Enterprises
* All rights reserved.
*
* Contributor(s):
* Zechariah Perez, omni (at) zeriph (dot) com
*
* THIS SOFTWARE IS PROVIDED BY ZERIPH AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ZERIPH AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if !defined(OMNI_SPAN_HPP)
#define OMNI_SPAN_HPP 1
#include <omni/defs/class_macros.hpp>
#include <omni/types/tick_t.hpp>
#include <omni/strings.hpp>
#include <omni/ostream.hpp>
#include <omni/math.hpp>
#if defined(OMNI_SAFE_SPAN)
#include <omni/sync/basic_lock.hpp>
#define OMNI_SAFE_SPANMTX_FW ,m_mtx()
#define OMNI_SPAN_ALOCK_FW omni::sync::scoped_lock<omni::sync::basic_lock> uuid12345(&this->m_mtx);
#define OMNI_SPAN_OLOCK_FW(o) omni::sync::scoped_lock<omni::sync::basic_lock> uuid54321(&o.m_mtx);
#define OMNI_SPAN_MLOCK_FW this->m_mtx.lock();
#define OMNI_SPAN_ULOCK_FW this->m_mtx.unlock();
#else
#define OMNI_SAFE_SPANMTX_FW
#define OMNI_SPAN_ALOCK_FW
#define OMNI_SPAN_OLOCK_FW(o)
#define OMNI_SPAN_MLOCK_FW
#define OMNI_SPAN_ULOCK_FW
#endif
namespace omni {
namespace chrono {
template < typename TickType >
class span
{
public:
typedef TickType span_t;
span() :
OMNI_CTOR_FW(omni::chrono::span)
m_ticks(0)
OMNI_SAFE_SPANMTX_FW
{
OMNI_D5_FW("created");
}
span(const omni::chrono::span<TickType>& cp) :
OMNI_CPCTOR_FW(cp)
m_ticks(cp.m_ticks)
OMNI_SAFE_SPANMTX_FW
{
OMNI_D5_FW("copied");
}
//implicit
span(span_t ticks) :
OMNI_CTOR_FW(omni::chrono::span)
m_ticks(ticks)
OMNI_SAFE_SPANMTX_FW
{
OMNI_D5_FW("created");
}
span(span_t hours, span_t minutes, span_t seconds) :
OMNI_CTOR_FW(omni::chrono::span)
m_ticks(0)
OMNI_SAFE_SPANMTX_FW
{
this->m_ticks = _time_to_ticks(hours, minutes, seconds);
OMNI_D5_FW("created");
}
span(span_t days, span_t hours, span_t minutes, span_t seconds) :
OMNI_CTOR_FW(omni::chrono::span)
m_ticks(0)
OMNI_SAFE_SPANMTX_FW
{
this->m_ticks = _time_to_ticks(days, hours, minutes, seconds, 0);
OMNI_D5_FW("created");
}
span(span_t days, span_t hours, span_t minutes, span_t seconds, span_t milliseconds) :
OMNI_CTOR_FW(omni::chrono::span)
m_ticks(0)
OMNI_SAFE_SPANMTX_FW
{
this->m_ticks = _time_to_ticks(days, hours, minutes, seconds, milliseconds);
OMNI_D5_FW("created");
}
~span()
{
OMNI_TRY_FW
OMNI_DTOR_FW
OMNI_CATCH_FW
OMNI_D5_FW("destroyed");
}
omni::chrono::span<TickType>& add(const omni::chrono::span<TickType>& other)
{
OMNI_SPAN_ALOCK_FW
if (this == &other) {
// t+t = 2t = t<<1
OMNI_BITS_WILL_ADD_OVER_FW(this->m_ticks, this->m_ticks)
this->m_ticks <<= 1;
} else {
OMNI_BITS_WILL_ADD_OVER_FW(this->m_ticks, other.ticks())
this->m_ticks += other.ticks();
}
return *this;
}
omni::chrono::span<TickType>& add(TickType ticks)
{
OMNI_SPAN_ALOCK_FW
OMNI_BITS_WILL_ADD_OVER_FW(this->m_ticks, ticks)
this->m_ticks += ticks;
return *this;
}
bool can_negate() const
{
return std::numeric_limits<span_t>::min() < 0;
}
// days portion
span_t days() const
{
return static_cast<span_t>(std::abs(static_cast<double>(this->ticks())) / OMNI_TICKS_PER_DAY);
}
omni::chrono::span<TickType> duration() const
{
OMNI_SPAN_ALOCK_FW
return span<TickType>(this->m_ticks >= 0 ? this->m_ticks : -this->m_ticks);
}
// hours component
span_t hours() const
{
return static_cast<span_t>(std::abs(static_cast<double>(this->ticks())) / OMNI_TICKS_PER_HOUR) % 24;
}
uint64_t hash_code() const
{
OMNI_SPAN_ALOCK_FW
return this->m_ticks ^ (this->m_ticks >> 32);
}
// milliseconds component
span_t milliseconds() const
{
return static_cast<span_t>(std::abs(static_cast<double>(this->ticks())) / OMNI_TICKS_PER_MILLISECOND) % 1000;
}
// minutes component
span_t minutes() const
{
return static_cast<span_t>(std::abs(static_cast<double>(this->ticks())) / OMNI_TICKS_PER_MINUTE) % 60;
}
omni::chrono::span<TickType> negate() const
{
if (this->can_negate()) {
// has sign, can negate
OMNI_SPAN_ALOCK_FW
return span<TickType>(-this->m_ticks);
}
OMNI_ERR_RETV_FW(OMNI_OVERFLOW_STR, omni::exceptions::overflow_error("cannot negate unsigned span"), span<TickType>::zero())
}
// seconds component
span_t seconds() const
{
return static_cast<span_t>(std::abs(static_cast<double>(this->ticks())) / OMNI_TICKS_PER_SECOND) % 60;
}
omni::chrono::span<TickType>& subtract(const omni::chrono::span<TickType>& other)
{
OMNI_SPAN_ALOCK_FW
if (this == &other) {
this->m_ticks = 0; // x-x is always 0
} else {
OMNI_BITS_WILL_SUB_UNDER_FW(this->m_ticks, other.ticks())
this->m_ticks -= other.ticks();
}
return *this;
}
omni::chrono::span<TickType>& subtract(TickType ticks)
{
OMNI_SPAN_ALOCK_FW
OMNI_BITS_WILL_SUB_UNDER_FW(this->m_ticks, ticks)
this->m_ticks -= ticks;
return *this;
}
void swap(omni::chrono::span<TickType>& o)
{
if (this != &o) {
OMNI_SPAN_ALOCK_FW
OMNI_SPAN_OLOCK_FW(o)
std::swap(this->m_ticks, o.m_ticks);
}
}
// total ticks
span_t ticks() const
{
OMNI_SPAN_ALOCK_FW
return this->m_ticks;
}
// days as whole and fractional
double total_days() const
{
return static_cast<double>(this->ticks()) * OMNI_DAYS_PER_TICK;
}
// hours as whole and fractional
double total_hours() const
{
return static_cast<double>(this->ticks()) * OMNI_HOURS_PER_TICK;
}
double total_milliseconds() const
{
double temp = static_cast<double>(this->ticks()) * OMNI_MILLISECONDS_PER_TICK;
if (static_cast<TickType>(temp) > omni::chrono::span<TickType>::max_milliseconds()) {
return static_cast<double>(omni::chrono::span<TickType>::max_milliseconds());
}
if (static_cast<TickType>(temp) < omni::chrono::span<TickType>::min_milliseconds()) {
return static_cast<double>(omni::chrono::span<TickType>::min_milliseconds());
}
return temp;
}
double total_minutes() const
{
return static_cast<double>(this->ticks()) * OMNI_MINUTES_PER_TICK;
}
double total_seconds() const
{
return static_cast<double>(this->ticks()) * OMNI_SECONDS_PER_TICK;
}
omni::string_t to_string_t() const
{
omni::sstream_t ss;
this->_to_string_base(ss);
return ss.str();
}
std::string to_string() const
{
std::stringstream ss;
this->_to_string_base(ss);
return ss.str();
}
std::wstring to_wstring() const
{
std::wstringstream ss;
this->_to_string_base(ss);
return ss.str();
}
omni::chrono::span<TickType>& operator=(const omni::chrono::span<TickType>& other)
{
if (this != &other) {
OMNI_SPAN_ALOCK_FW
OMNI_ASSIGN_FW(other)
this->m_ticks = other.ticks();
}
return *this;
}
omni::chrono::span<TickType>& operator=(span_t val)
{
OMNI_SPAN_ALOCK_FW
this->m_ticks = val;
return *this;
}
omni::chrono::span<TickType> operator-(const omni::chrono::span<TickType>& other)
{
if (this == &other) {
// no need to lock, since (m_ticks - m_ticks == 0)
return span<TickType>(0);
}
OMNI_SPAN_ALOCK_FW
OMNI_BITS_WILL_SUB_UNDER_FW(this->m_ticks, other.ticks())
return span<TickType>(this->m_ticks - other.ticks());
}
omni::chrono::span<TickType> operator-(span_t val)
{
OMNI_SPAN_ALOCK_FW
return span<TickType>(this->m_ticks - val);
}
omni::chrono::span<TickType> operator+(const omni::chrono::span<TickType>& other)
{
if (this == &other) {
OMNI_SPAN_ALOCK_FW
return span<TickType>(this->m_ticks + this->m_ticks);
}
OMNI_SPAN_ALOCK_FW
OMNI_BITS_WILL_ADD_OVER_FW(this->m_ticks, other.ticks())
return span<TickType>(this->m_ticks + other.ticks());
}
omni::chrono::span<TickType> operator+(span_t val)
{
OMNI_SPAN_ALOCK_FW
OMNI_BITS_WILL_ADD_OVER_FW(this->m_ticks, val)
return span<TickType>(this->m_ticks + val);
}
omni::chrono::span<TickType>& operator-=(const omni::chrono::span<TickType>& other)
{
OMNI_SPAN_ALOCK_FW
if (this == &other) {
this->m_ticks = 0; // x-x is always 0
} else {
OMNI_BITS_WILL_SUB_UNDER_FW(this->m_ticks, other.ticks())
this->m_ticks -= other.ticks();
}
return *this;
}
omni::chrono::span<TickType>& operator-=(span_t val)
{
OMNI_SPAN_ALOCK_FW
OMNI_BITS_WILL_SUB_UNDER_FW(this->m_ticks, val)
this->m_ticks -= val;
return *this;
}
omni::chrono::span<TickType>& operator+=(const omni::chrono::span<TickType>& other)
{
OMNI_SPAN_ALOCK_FW
if (this == &other) {
// t+t = 2t = t<<1
OMNI_BITS_WILL_ADD_OVER_FW(this->m_ticks, this->m_ticks)
this->m_ticks <<= 1;
} else {
OMNI_BITS_WILL_ADD_OVER_FW(this->m_ticks, other.ticks())
this->m_ticks += other.ticks();
}
return *this;
}
omni::chrono::span<TickType>& operator+=(span_t val)
{
OMNI_SPAN_ALOCK_FW
this->m_ticks += val;
return *this;
}
omni::chrono::span<TickType> operator-()
{
return this->negate();
}
omni::chrono::span<TickType>& operator+()
{
// useful as a no-op (might get optimized out)
return *this;
}
bool operator==(const omni::chrono::span<TickType>& other) const
{
if (this == &other) { return true; }
OMNI_SPAN_ALOCK_FW
return this->m_ticks == other.ticks();
}
bool operator==(span_t val) const
{
OMNI_SPAN_ALOCK_FW
return this->m_ticks == val;
}
bool operator!=(const omni::chrono::span<TickType>& other) const
{
if (this == &other) { return false; }
OMNI_SPAN_ALOCK_FW
return this->m_ticks != other.ticks();
}
bool operator!=(span_t val) const
{
OMNI_SPAN_ALOCK_FW
return this->m_ticks != val;
}
bool operator<(const omni::chrono::span<TickType>& other) const
{
if (this == &other) { return false; }
OMNI_SPAN_ALOCK_FW
return this->m_ticks < other.ticks();
}
bool operator<(span_t val) const
{
OMNI_SPAN_ALOCK_FW
return this->m_ticks < val;
}
bool operator>(const omni::chrono::span<TickType>& other) const
{
if (this == &other) { return false; }
OMNI_SPAN_ALOCK_FW
return this->m_ticks > other.ticks();
}
bool operator>(span_t val) const
{
OMNI_SPAN_ALOCK_FW
return this->m_ticks > val;
}
bool operator<=(const omni::chrono::span<TickType>& other) const
{
if (this == &other) { return true; }
OMNI_SPAN_ALOCK_FW
return this->m_ticks <= other.ticks();
}
bool operator<=(span_t val) const
{
OMNI_SPAN_ALOCK_FW
return this->m_ticks <= val;
}
bool operator>=(const omni::chrono::span<TickType>& other) const
{
if (this == &other) { return true; }
OMNI_SPAN_ALOCK_FW
return this->m_ticks >= other.ticks();
}
bool operator>=(span_t val) const
{
OMNI_SPAN_ALOCK_FW
return this->m_ticks >= val;
}
operator span_t() const
{
OMNI_SPAN_ALOCK_FW
return this->m_ticks;
}
operator std::string() const { return this->to_string(); }
operator std::wstring() const { return this->to_wstring(); }
OMNI_MEMBERS_FW(omni::chrono::span<TickType>) // disposing,name,type(),hash()
OMNI_OSTREAM_FW(omni::chrono::span<TickType>)
static omni::chrono::span<TickType> parse(const std::string& s)
{
span<TickType> ret;
if (!omni::chrono::span<TickType>::try_parse(s, ret)) {
OMNI_ERR_RETV_FW("parse error", omni::exceptions::invalid_parse(), ret)
}
return ret;
}
static omni::chrono::span<TickType> parse(const std::wstring& s)
{
span<TickType> ret;
if (!omni::chrono::span<TickType>::try_parse(s, ret)) {
OMNI_ERR_RETV_FW("parse error", omni::exceptions::invalid_parse(), ret)
}
return ret;
}
static bool try_parse(const std::string& s, omni::chrono::span<TickType>& result)
{
return _try_parse<std::string, char>(s, result);
}
static bool try_parse(const std::wstring& s, omni::chrono::span<TickType>& result)
{
return _try_parse<std::wstring, wchar_t>(s, result);
}
static inline omni::chrono::span<TickType> from_time(span_t hours, span_t minutes, span_t seconds)
{
return span<TickType>(hours, minutes, seconds);
}
static inline omni::chrono::span<TickType> from_time(span_t days, span_t hours, span_t minutes, span_t seconds)
{
return span<TickType>(days, hours, minutes, seconds);
}
static inline omni::chrono::span<TickType> from_time(span_t days, span_t hours, span_t minutes, span_t seconds, span_t milliseconds)
{
return span<TickType>(days, hours, minutes, seconds, milliseconds);
}
static inline omni::chrono::span<TickType> from_days(double value)
{
return _interval(value, OMNI_MILLISECONDS_PER_DAY);
}
static inline omni::chrono::span<TickType> from_hours(double value)
{
return _interval(value, OMNI_MILLISECONDS_PER_HOUR);
}
static inline omni::chrono::span<TickType> from_milliseconds(double value)
{
return _interval(value, 1);
}
static inline omni::chrono::span<TickType> from_minutes(double value)
{
return _interval(value, OMNI_MILLISECONDS_PER_MINUTE);
}
static inline omni::chrono::span<TickType> from_microseconds(span_t value)
{
return span<TickType>(value * OMNI_TICKS_PER_MICROSECOND);
}
static inline omni::chrono::span<TickType> from_nanoseconds(span_t value)
{
return span<TickType>(value * OMNI_TICKS_PER_NANOSECOND);
}
static inline omni::chrono::span<TickType> from_seconds(double value)
{
return _interval(value, OMNI_MILLISECONDS_PER_SECOND);
}
static inline omni::chrono::span<TickType> from_ticks(span_t value)
{
return span<TickType>(value);
}
static inline span_t max_seconds()
{
return std::numeric_limits<span_t>::max() / OMNI_TICKS_PER_SECOND;
}
static inline span_t min_seconds()
{
return std::numeric_limits<span_t>::min() / OMNI_TICKS_PER_SECOND;
}
static inline span_t max_milliseconds()
{
return std::numeric_limits<span_t>::max() / OMNI_TICKS_PER_MILLISECOND;
}
static inline span_t min_milliseconds()
{
return std::numeric_limits<span_t>::min() / OMNI_TICKS_PER_MILLISECOND;
}
static inline omni::chrono::span<TickType> max_value()
{
return span<TickType>(std::numeric_limits<span_t>::max());
}
static inline omni::chrono::span<TickType> min_value()
{
return span<TickType>(std::numeric_limits<span_t>::min());
}
static inline omni::chrono::span<TickType> zero()
{
return span<TickType>(0);
}
private:
span_t m_ticks; // 100-nanosecond units
#if defined(OMNI_SAFE_SPAN)
mutable omni::sync::basic_lock m_mtx;
#endif
template < typename StrStr >
void _to_string_base(StrStr& ss) const
{
span_t t = this->ticks();
span_t d = static_cast<span_t>(std::abs(static_cast<double>(t)) / OMNI_TICKS_PER_DAY);
span_t h = static_cast<span_t>(std::abs(static_cast<double>(t)) / OMNI_TICKS_PER_HOUR) % 24;
span_t m = static_cast<span_t>(std::abs(static_cast<double>(t)) / OMNI_TICKS_PER_MINUTE) % 60;
span_t s = static_cast<span_t>(std::abs(static_cast<double>(t)) / OMNI_TICKS_PER_SECOND) % 60;
// d.hh:mm:ss.ms
if (t < 0) { ss << "-"; }
ss << d << ".";
if (h < 10) { ss << "0"; }
ss << h << ":";
if (m < 10) { ss << "0"; }
ss << m << ":";
if (s < 10) { ss << "0"; }
ss << s << ".";
ss << (static_cast<span_t>(std::abs(static_cast<double>(t)) / OMNI_TICKS_PER_MILLISECOND) % 1000);
}
static omni::chrono::span<TickType> _interval(double value, int32_t scale)
{
if (!omni::math::is_nan(value)) { // NaN check
double tmp = value * scale;
double millis = tmp + ((value >= 0) ? 0.5 : -0.5);
if ((static_cast<TickType>(millis) > omni::chrono::span<TickType>::max_milliseconds()) || (static_cast<TickType>(millis) < omni::chrono::span<TickType>::min_milliseconds())) {
OMNI_ERR_RETV_FW(OMNI_OVERFLOW_STR, omni::exceptions::overflow_error("invalid span"), span<TickType>::zero())
}
return span<TickType>(static_cast<span_t>(millis) * OMNI_TICKS_PER_MILLISECOND);
}
OMNI_ERR_RETV_FW(OMNI_NAN_ERR_STR, omni::exceptions::nan_error("span cannot be NaN"), span<TickType>::zero())
}
static span_t _time_to_ticks(span_t days, span_t hours, span_t minutes, span_t seconds, span_t milliseconds)
{
span_t tot = (((days * 86400 /* 3600 * 24*/) + (hours * 3600) + (minutes * 60) + seconds) * 1000) + milliseconds;
if (tot > omni::chrono::span<TickType>::max_milliseconds() || tot < omni::chrono::span<TickType>::min_milliseconds())
OMNI_ERR_RETV_FW(OMNI_OVERFLOW_STR, omni::exceptions::overflow_error("invalid span"), span<TickType>::zero())
return tot * OMNI_TICKS_PER_MILLISECOND;
}
static span_t _time_to_ticks(span_t hour, span_t minute, span_t second)
{
// totalSeconds is bounded by 2^31 * 2^12 + 2^31 * 2^8 + 2^31,
// which is less than 2^44, meaning we will not overflow totalSeconds.
span_t tot = (hour * 3600) + (minute * 60) + second;
if (tot > omni::chrono::span<TickType>::max_seconds() || tot < omni::chrono::span<TickType>::min_seconds()) {
OMNI_ERR_RETV_FW(OMNI_OVERFLOW_STR, omni::exceptions::overflow_error("invalid span"), span<TickType>::zero())
}
return tot * OMNI_TICKS_PER_SECOND;
}
template < typename Str, typename Char >
static bool _try_parse(const Str& s, span<TickType>& result)
{
/*
Elements in square brackets ([ and ]) are optional.
One selection from the list of alternatives enclosed in braces ({ and }) and separated by vertical bars (|) is required.
Element Description
ws Optional white space.
- An optional minus sign, which indicates a negative TimeSpan.
d Days, ranging from 0 to 10675199.
. A culture-sensitive symbol that separates days from hours. The invariant format uses a period (".") character.
hh Hours, ranging from 0 to 23.
: The culture-sensitive time separator symbol. The invariant format uses a colon (":") character.
mm Minutes, ranging from 0 to 59.
ss Optional seconds, ranging from 0 to 59.
. A culture-sensitive symbol that separates seconds from fractions of a second. The invariant format uses a period (".") character.
ff Optional fractional seconds, consisting of one to seven decimal digits.
The components of s must collectively specify a time interval that is greater than or equal to TimeSpan.MinValue and less than or equal to TimeSpan.MaxValue.
FORMAT:
[ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]
If the s argument is not a day value only, it must include an hours and a minutes component; other components are optional.
If they are present, the values of each time component must fall within a specified range.
For example, the value of hh, the hours component, must be between 0 and 23.
Because of this, passing "23:00:00" to the Parse method returns a time interval of 23 hours.
On the other hand, passing "24:00:00" returns a time interval of 24 days.
Because "24" is outside the range of the hours component, it is interpreted as the days component.
// d.hh:mm:ss.ms
// foreach (+InvariantPattern, -InvariantPattern, +LocalizedPattern, -LocalizedPattern) try
// 1 number => d
// 2 numbers => h:m
// 3 numbers => h:m:s | d.h:m | h:m:.f
// 4 numbers => h:m:s.f | d.h:m:s | d.h:m:.f
// 5 numbers => d.h:m:s.f
*/
Str ts = omni::string::util::trim(s);
if (ts.size() == 0) { return false; }
bool can_negate = (std::numeric_limits<TickType>::min() < 0);
bool is_neg = false;
if (ts[0] == omni::char_util::to_char_t<Char, char>('-')) {
if (can_negate) {
is_neg = true;
ts = ts.substr(1);
} else {
return false;
}
} else if (ts[0] == omni::char_util::to_char_t<Char, char>('+')) {
ts = ts.substr(1); // remove the + sign
}
TickType val;
if (omni::string::util::is_numeric(ts)) {
// 1 number: days, ranging from 0 to 10675199 (whole numbers only)
val = omni::string::util::to_type<TickType>(ts);
if (val < 0 || val > 10675199) { return false; }
val = _time_to_ticks(val, 0, 0, 0, 0);
result.m_ticks = (is_neg ? -val : val);
return true;
}
Char period = omni::char_util::to_char_t<Char, char>('.');
Str delim = omni::string::util::to_type<Str>(":");
Str pdel = omni::string::util::to_type<Str>(".");
if (ts.find(omni::char_util::to_char_t<Char, char>(':')) != Str::npos) {
TickType days = 0, hrs = 0, mins = 0, sec = 0, ms = 0;
Str msstr;
omni_sequence_t<Str> vals = omni::string::util::split(ts, delim);
omni_sequence_t<Str> pvals;
switch (vals.size()) {
case 2: {
// d.h:m | h:m
if (!omni::string::util::is_numeric(vals[0], true) ||
!omni::string::util::is_numeric(vals[1]))
{ return false; }
mins = omni::string::util::to_type<TickType>(vals[1]);
if (vals[0].find(period) != Str::npos) {
pvals = omni::string::util::split(vals[0], pdel);
days = omni::string::util::to_type<TickType>(pvals[0]);
hrs = omni::string::util::to_type<TickType>(pvals[1]);
} else {
hrs = omni::string::util::to_type<TickType>(vals[0]);
}
} break;
case 3: {
// h:m:s | h:m:.f | h:m:s.f | d.h:m:s | d.h:m:.f | d.h:m:s.f
if (!omni::string::util::is_numeric(vals[0], true) ||
!omni::string::util::is_numeric(vals[1]) ||
!omni::string::util::is_numeric(vals[2], true))
{ return false; }
mins = omni::string::util::to_type<TickType>(vals[1]);
if (vals[0].find(period) != Str::npos) {
// d.h: m:s | d.h: m:.f | d.h: m:s.f
pvals = omni::string::util::split(vals[0], pdel);
days = omni::string::util::to_type<TickType>(pvals[0]);
hrs = omni::string::util::to_type<TickType>(pvals[1]);
} else {
// h: m:s | h: m:.f | h: m:s.f
hrs = omni::string::util::to_type<TickType>(vals[0]);
}
if (vals[2].find(period) != Str::npos) {
// h:m: .f | d.h:m: .f | h:m: s.f | d.h:m: s.f
pvals = omni::string::util::split(vals[2], pdel);
if (pvals.size() > 1) {
if (!pvals[0].empty()) { sec = omni::string::util::to_type<TickType>(pvals[0]); }
msstr = pvals[1];
} else {
msstr = pvals[0];
}
} else {
// h:m: s | d.h:m: s
sec = omni::string::util::to_type<TickType>(vals[2]);
}
} break;
}
// hh Hours, ranging from 0 to 23.
// mm Minutes, ranging from 0 to 59.
// ss Optional seconds, ranging from 0 to 59.
// ff Optional fractional seconds, consisting of one to seven decimal digits.
if ((hrs < 0 || hrs > 23) || (mins < 0 || mins > 59) || (sec < 0 || sec > 59))
{ return false; }
if (!msstr.empty()) {
ms = omni::string::util::to_type<double>(omni::string::util::to_type<Str>("0.") + msstr) * 1000;
}
val = _time_to_ticks(days, hrs, mins, sec, ms);
result.m_ticks = (is_neg ? -val : val);
return true;
}
return false;
}
};
typedef omni::chrono::span<int64_t> span_t;
typedef omni::chrono::span<uint64_t> unsigned_span_t;
typedef unsigned_span_t uspan_t;
} // namespace chrono
} // namespace omni
namespace std {
template < typename TickType >
inline void swap(omni::chrono::span<TickType>& o1, omni::chrono::span<TickType>& o2)
{
o1.swap(o2);
}
}
#endif // OMNI_SPAN_HPP