def test_exact(): now = arrow.now() # Default compare is exact approximate = arrow.ArrowFactory(type=ApproximateArrow).get(now) assert now == approximate assert now.replace(seconds=1) != approximate
def parse_datetime(cls, value, span=None): """Transform a human date or time string to a Python UTC datetime """ factory = arrow.ArrowFactory(ArrowSpan) dta = factory.get(dateparser.parse(value, settings={'TIMEZONE': 'UTC'})) dta.setup(value) return dta
def __init__(self, expire_s=None): """ :param expire_s: expiration date delta en seconds all objects inserted will use this delta to set their own expiration date :return: """ super().__init__() self.expire_s = expire_s self.arrow_factory = arrow.ArrowFactory(ExpireArrow)
def show(self) -> None: """ローカル時刻とカウントダウンを表示します.""" # 各ローカル時刻を表示 utc = arrow.utcnow() for zone in self.time_zones: s = utc.to(zone).format(self.time_format) print(f"{zone.split('/')[1]}\t{s}".expandtabs(16)) # カウントダウンを表示 factory = arrow.ArrowFactory(CustomArrow) custom = factory.utcnow() delta = custom.till_the_day(arrow.get(self.the_day_datetime)) print(f"{self.the_day_title}\t{str(delta).split('.')[0]}")
arw = arrow.utcnow() print arw print arw.replace(hour=4, minute=40) print arw.replace(tzinfo='US/Pacific') # 四舍五入 arrow.utcnow().floor('hour') arrow.utcnow().ceil('hour') # 时间循环 start = datetime(2013, 5, 5, 12, 30) end = datetime(2013, 5, 5, 17, 15) for r in arrow.Arrow.span_range('hour', start, end): print r arrow.ArrowFactory() ''' Formate时候的高频字符 Token Output Year YYYY 2000, 2001, 2002 ... 2012, 2013 YY 00, 01, 02 ... 12, 13 Month MMMM January, February, March ... MMM Jan, Feb, Mar ... MM 01, 02, 03 ... 11, 12 M 1, 2, 3 ... 11, 12 Day of Year DDDD 001, 002, 003 ... 364, 365 DDD 1, 2, 3 ... 4, 5 Day of Month DD 01, 02, 03 ... 30, 31 D 1, 2, 3 ... 30, 31 Do 1st, 2nd, 3rd ... 30th, 31st
# <Arrow [2013-05-05T12:30:00+00:00]> # <Arrow [2013-05-05T13:30:00+00:00]> # <Arrow [2013-05-05T14:30:00+00:00]> # <Arrow [2013-05-05T15:30:00+00:00]> # Factories # Use factories to harness Arrow’s module API for a custom Arrow-derived type. First, derive your type: class CustomArrow(arrow.Arrow): def days_till_xmas(self): xmas = arrow.Arrow(self.year, 12, 25) if self > xmas: xmas = xmas.shift(years=1) return (xmas - self).days # Then get and use a factory for it: factory = arrow.ArrowFactory(CustomArrow) custom = factory.utcnow() # <CustomArrow [2013-05-27T23:35:35.533160+00:00]> custom.days_till_xmas() #211 # Supported Tokens # Use the following tokens in parsing and formatting. Note that they’re not the same as the tokens for strptime(3): # Token Output Year YYYY 2000, 2001, 2002 … 2012, 2013 YY 00, 01, 02 … 12, 13 Month MMMM January, February, March … 1 MMM Jan, Feb, Mar … 1 MM 01, 02, 03 … 11, 12
def test_expiration(self): factory = arrow.ArrowFactory(ExpireArrow) expiration_date = factory.utcnow().expiration(seconds=0.1) assert(arrow.utcnow() < expiration_date) time.sleep(0.2) assert(arrow.utcnow() > expiration_date)
def test_not_yet_expire_with_sleep(self): factory = arrow.ArrowFactory(ExpireArrow) utc = factory.utcnow().expiration(seconds=0.2) time.sleep(0.1) assert(not utc.is_expired())
def test_expire(self): factory = arrow.ArrowFactory(ExpireArrow) # replace seconds=xxx is used to go forward or backward on time past = factory.utcnow().replace(seconds=-1) assert(past.is_expired())
def test_not_yet_expire(self): factory = arrow.ArrowFactory(ExpireArrow) future = factory.utcnow().replace(seconds=1) assert(not future.is_expired())
def configure_timestamp_parser(session): """Change boto timestamp parsing to return Arrow objects""" arrow_factory = arrow.ArrowFactory(AlternativeDateTime) parser_factory = session.get_component('response_parser_factory') parser_factory.set_parser_defaults(timestamp_parser=arrow_factory.get)