| 3539 | |
| 3540 | |
| 3541 | class UTCTimeField(Field[float, int]): |
| 3542 | __slots__ = ["epoch", "delta", "strf", |
| 3543 | "use_msec", "use_micro", "use_nano", "custom_scaling"] |
| 3544 | |
| 3545 | def __init__(self, |
| 3546 | name, # type: str |
| 3547 | default, # type: int |
| 3548 | use_msec=False, # type: bool |
| 3549 | use_micro=False, # type: bool |
| 3550 | use_nano=False, # type: bool |
| 3551 | epoch=None, # type: Optional[Tuple[int, int, int, int, int, int, int, int, int]] # noqa: E501 |
| 3552 | strf="%a, %d %b %Y %H:%M:%S %z", # type: str |
| 3553 | custom_scaling=None, # type: Optional[int] |
| 3554 | fmt="I" # type: str |
| 3555 | ): |
| 3556 | # type: (...) -> None |
| 3557 | Field.__init__(self, name, default, fmt=fmt) |
| 3558 | mk_epoch = EPOCH if epoch is None else calendar.timegm(epoch) |
| 3559 | self.epoch = mk_epoch |
| 3560 | self.delta = mk_epoch - EPOCH |
| 3561 | self.strf = strf |
| 3562 | self.use_msec = use_msec |
| 3563 | self.use_micro = use_micro |
| 3564 | self.use_nano = use_nano |
| 3565 | self.custom_scaling = custom_scaling |
| 3566 | |
| 3567 | def i2repr(self, pkt, x): |
| 3568 | # type: (Optional[Packet], float) -> str |
| 3569 | if x is None: |
| 3570 | x = time.time() - self.delta |
| 3571 | elif self.use_msec: |
| 3572 | x = x / 1e3 |
| 3573 | elif self.use_micro: |
| 3574 | x = x / 1e6 |
| 3575 | elif self.use_nano: |
| 3576 | x = x / 1e9 |
| 3577 | elif self.custom_scaling: |
| 3578 | x = x / self.custom_scaling |
| 3579 | x += self.delta |
| 3580 | # To make negative timestamps work on all plateforms (e.g. Windows), |
| 3581 | # we need a trick. |
| 3582 | t = ( |
| 3583 | datetime.datetime(1970, 1, 1) + |
| 3584 | datetime.timedelta(seconds=x) |
| 3585 | ).strftime(self.strf) |
| 3586 | return "%s (%d)" % (t, int(x)) |
| 3587 | |
| 3588 | def i2m(self, pkt, x): |
| 3589 | # type: (Optional[Packet], Optional[float]) -> int |
| 3590 | if x is None: |
| 3591 | x = time.time() - self.delta |
| 3592 | if self.use_msec: |
| 3593 | x = x * 1e3 |
| 3594 | elif self.use_micro: |
| 3595 | x = x * 1e6 |
| 3596 | elif self.use_nano: |
| 3597 | x = x * 1e9 |
| 3598 | elif self.custom_scaling: |
no outgoing calls
no test coverage detected