本文整理汇总了Python中arrow.Arrow.fromdatetime方法的典型用法代码示例。如果您正苦于以下问题:Python Arrow.fromdatetime方法的具体用法?Python Arrow.fromdatetime怎么用?Python Arrow.fromdatetime使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类arrow.Arrow
的用法示例。
在下文中一共展示了Arrow.fromdatetime方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: friendly_time
# 需要导入模块: from arrow import Arrow [as 别名]
# 或者: from arrow.Arrow import fromdatetime [as 别名]
def friendly_time(jinja_ctx, context, **kw):
"""Format timestamp in human readable format.
* Context must be a datetimeobject
* Takes optional keyword argument timezone which is a timezone name as a string. Assume the source datetime is in this timezone.
"""
now = context
if not now:
return ""
tz = kw.get("source_timezone", None)
if tz:
tz = timezone(tz)
else:
tz = datetime.timezone.utc
# Meke relative time between two timestamps
now = now.astimezone(tz)
arrow = Arrow.fromdatetime(now)
other = Arrow.fromdatetime(datetime.datetime.utcnow())
return arrow.humanize(other)
示例2: filter_datetime
# 需要导入模块: from arrow import Arrow [as 别名]
# 或者: from arrow.Arrow import fromdatetime [as 别名]
def filter_datetime(jinja_ctx, context, **kw):
"""Format datetime in a certain timezone."""
now = context
if not now:
return ""
tz = kw.get("timezone", None)
if tz:
tz = timezone(tz)
else:
tz = datetime.timezone.utc
locale = kw.get("locale", "en_US")
arrow = Arrow.fromdatetime(now, tzinfo=tz)
# Convert to target timezone
tz = kw.get("target_timezone")
if tz:
arrow = arrow.to(tz)
else:
tz = arrow.tzinfo
format = kw.get("format", "YYYY-MM-DD HH:mm")
text = arrow.format(format, locale=locale)
if kw.get("show_timezone"):
text = text + " ({})".format(tz)
return text
示例3: arrow
# 需要导入模块: from arrow import Arrow [as 别名]
# 或者: from arrow.Arrow import fromdatetime [as 别名]
def arrow(date=None, tz=None):
if date is None:
return utcnow() if tz is None else now(tz)
else:
if tz is None:
try:
tz = parser.TzinfoParser.parse(date)
return now(tz)
except:
return Arrow.fromdatetime(date)
else:
tz = parser.TzinfoParser.parse(tz)
return Arrow.fromdatetime(date, tz)
示例4: __init__
# 需要导入模块: from arrow import Arrow [as 别名]
# 或者: from arrow.Arrow import fromdatetime [as 别名]
def __init__(self, source, n, str_type, count_type, start_date=Arrow(1970,1,1).datetime, stop_date=utcnow().datetime):
super(Vector, self).__init__(n=n, str_type=str_type)
if source not in c['Comment'].collection_names():
raise ValueError("{} is not a collection in the Comment database".format(source))
if str_type not in StringLike.__subclasses__():
raise ValueError("{} is not a valid string type class".format(str_type))
for date in [start_date, stop_date]:
if not isinstance(date, datetime):
raise TypeError("{} is not a datetime.datetime object".format(date))
self.count_type = count_type
self.start_date = Arrow.fromdatetime(start_date).datetime
self.stop_date = Arrow.fromdatetime(stop_date).datetime
self.body = c['Body'][source]
self.cache = c['BodyCache'][source]
self.comment = c['Comment'][source]
self.__fromdb__()
示例5: get_dates
# 需要导入模块: from arrow import Arrow [as 别名]
# 或者: from arrow.Arrow import fromdatetime [as 别名]
def get_dates(_):
days = parse_days(get_content())
# we used to be able to support previous weeks; each item is a week
return [
(
(
Arrow.fromdatetime(parse(day)),
parse_locations(locations)
)
for day, locations in days
)
]
示例6: arrow
# 需要导入模块: from arrow import Arrow [as 别名]
# 或者: from arrow.Arrow import fromdatetime [as 别名]
def arrow(date=None, tz=None):
if date is None:
return utcnow() if tz is None else now(tz)
else:
if tz is None:
try:
tz = parser.TzinfoParser.parse(date)
return now(tz)
except:
pass
if isinstance(date, (float, int)):
return Arrow.utcfromtimestamp(date)
return Arrow.fromdatetime(date)
else:
tz = parser.TzinfoParser.parse(tz)
return Arrow.fromdatetime(date, tz)
示例7: civil_twilight
# 需要导入模块: from arrow import Arrow [as 别名]
# 或者: from arrow.Arrow import fromdatetime [as 别名]
def civil_twilight(date, lon, lat):
"""
Returns the evening civil twilight time as a `datetime.datetime` in UTC.
Takes the date to calculate for (as a `datetime.date`), and the longitude
and lattitude of the location.
Evening civil twilight is defined as ending when the geometric centre of
the sun is 6° below the horizon.
"""
location = ephem.Observer()
location.date = date.strftime("%Y/%m/%d")
location.lon = force_str(lon)
location.lat = force_str(lat)
location.horizon = force_str("-6")
twilight = location.next_setting(ephem.Sun(), use_center=True)
return Arrow.fromdatetime(twilight.datetime()).datetime
示例8: test_json
# 需要导入模块: from arrow import Arrow [as 别名]
# 或者: from arrow.Arrow import fromdatetime [as 别名]
def test_json(self, mocked_now):
# patch now inside the Stopwatch object
tz = gettz('America/Sao_Paulo')
now = datetime(2016, 4, 29, hour=15, minute=38, second=8, tzinfo=tz)
mocked_now.return_value = Arrow.fromdatetime(now)
# make a new request
resp = self.app.get('/api/stopwatch/')
json_resp = loads(resp.data.decode('utf-8'))
# assertions
keys = ('days', 'hours', 'minutes', 'seconds')
values = (11, 16, 1, 8)
with self.subTest():
for key, value in zip(keys, values):
self.assertIn(key, json_resp)
self.assertEqual(json_resp[key], value)
示例9: get
# 需要导入模块: from arrow import Arrow [as 别名]
# 或者: from arrow.Arrow import fromdatetime [as 别名]
def get(*args, **kwargs):
'''Returns an :class:`Arrow <arrow.Arrow>` object based on flexible inputs.
Usage::
>>> import arrow
**No inputs** to get current UTC time::
>>> arrow.get()
<Arrow [2013-05-08T05:51:43.316458+00:00]>
**One str**, **float**, or **int**, convertible to a floating-point timestamp, to get that timestamp in UTC::
>>> arrow.get(1367992474.293378)
<Arrow [2013-05-08T05:54:34.293378+00:00]>
>>> arrow.get(1367992474)
<Arrow [2013-05-08T05:54:34+00:00]>
>>> arrow.get('1367992474.293378')
<Arrow [2013-05-08T05:54:34.293378+00:00]>
>>> arrow.get('1367992474')
<Arrow [2013-05-08T05:54:34+00:00]>
**One str**, convertible to a timezone, or **tzinfo**, to get the current time in that timezone::
>>> arrow.get('local')
<Arrow [2013-05-07T22:57:11.793643-07:00]>
>>> arrow.get('US/Pacific')
<Arrow [2013-05-07T22:57:15.609802-07:00]>
>>> arrow.get('-07:00')
<Arrow [2013-05-07T22:57:22.777398-07:00]>
>>> arrow.get(tz.tzlocal())
<Arrow [2013-05-07T22:57:28.484717-07:00]>
**One** naive **datetime**, to get that datetime in UTC::
>>> arrow.get(datetime(2013, 5, 5))
<Arrow [2013-05-05T00:00:00+00:00]>
**One** aware **datetime**, to get that datetime::
>>> arrow.get(datetime(2013, 5, 5, tzinfo=tz.tzlocal()))
<Arrow [2013-05-05T00:00:00-07:00]>
**Two** arguments, a naive or aware **datetime**, and a timezone expression (as above)::
>>> arrow.get(datetime(2013, 5, 5), 'US/Pacific')
<Arrow [2013-05-05T00:00:00-07:00]>
**Two** arguments, both **str**, to parse the first according to the format of the second::
>>> arrow.get('2013-05-05 12:30:45', 'YYYY-MM-DD HH:mm:ss')
<Arrow [2013-05-05T12:30:45+00:00]>
**Three or more** arguments, as for the constructor of a **datetime**::
>>> arrow.get(2013, 5, 5, 12, 30, 45)
<Arrow [2013-05-05T12:30:45+00:00]>
'''
arg_count = len(args)
if arg_count == 0:
return Arrow.utcnow()
if arg_count == 1:
arg = args[0]
timestamp = None
try:
timestamp = float(arg)
except:
pass
# (int), (float), (str(int)) or (str(float)) -> from timestamp.
if timestamp is not None:
return Arrow.utcfromtimestamp(timestamp)
# (datetime) -> from datetime.
elif isinstance(arg, datetime):
return Arrow.fromdatetime(arg)
# (tzinfo) -> now, @ tzinfo.
elif isinstance(arg, tzinfo):
return Arrow.now(arg)
# (str) -> now, @ tzinfo.
elif isinstance(arg, str):
_tzinfo = parser.TzinfoParser.parse(arg)
return Arrow.now(_tzinfo)
else:
raise TypeError('Can\'t parse single argument type of \'{0}\''.format(type(arg)))
#.........这里部分代码省略.........