本文整理汇总了Python中datetime.datetime.fromordinal方法的典型用法代码示例。如果您正苦于以下问题:Python datetime.fromordinal方法的具体用法?Python datetime.fromordinal怎么用?Python datetime.fromordinal使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类datetime.datetime
的用法示例。
在下文中一共展示了datetime.fromordinal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_CCDCesque_changedates
# 需要导入模块: from datetime import datetime [as 别名]
# 或者: from datetime.datetime import fromordinal [as 别名]
def test_CCDCesque_changedates(record):
# Test start, end, and break dates of first two segments
starts = [dt.strptime('1984-06-04', '%Y-%m-%d'),
dt.strptime('1999-07-16', '%Y-%m-%d')]
ends = [dt.strptime('1999-06-30', '%Y-%m-%d'),
dt.strptime('2003-07-11', '%Y-%m-%d')]
breaks = [dt.strptime('1999-07-16', '%Y-%m-%d'),
dt.strptime('2003-07-27', '%Y-%m-%d')]
for i in range(2):
assert dt.fromordinal(record[i]['start']) == starts[i]
assert dt.fromordinal(record[i]['end']) == ends[i]
assert dt.fromordinal(record[i]['break']) == breaks[i]
# And now for the more hazardous tests on coefficents and RMSE
示例2: ordinal2yeardoy
# 需要导入模块: from datetime import datetime [as 别名]
# 或者: from datetime.datetime import fromordinal [as 别名]
def ordinal2yeardoy(ordinal):
""" Convert ordinal dates to two arrays of year and doy
Args:
ordinal (np.ndarray): ordinal dates
Returns:
np.ndarray: nobs x 2 np.ndarray containing the year and DOY for each
ordinal date
"""
_date = [dt.fromordinal(_d) for _d in ordinal]
yeardoy = np.empty((ordinal.size, 2), dtype=np.uint16)
yeardoy[:, 0] = np.array([int(_d.strftime('%Y')) for _d in _date])
yeardoy[:, 1] = np.array([int(_d.strftime('%j')) for _d in _date])
return yeardoy
示例3: _from_ordinal
# 需要导入模块: from datetime import datetime [as 别名]
# 或者: from datetime.datetime import fromordinal [as 别名]
def _from_ordinal(x, tz=None):
ix = int(x)
dt = datetime.fromordinal(ix)
remainder = float(x) - ix
hour, remainder = divmod(24 * remainder, 1)
minute, remainder = divmod(60 * remainder, 1)
second, remainder = divmod(60 * remainder, 1)
microsecond = int(1e6 * remainder)
if microsecond < 10:
microsecond = 0 # compensate for rounding errors
dt = datetime(dt.year, dt.month, dt.day, int(hour), int(minute),
int(second), microsecond)
if tz is not None:
dt = dt.astimezone(tz)
if microsecond > 999990: # compensate for rounding errors
dt += timedelta(microseconds=1e6 - microsecond)
return dt
# Fixed frequency dynamic tick locators and formatters
# -------------------------------------------------------------------------
# --- Locators ---
# -------------------------------------------------------------------------
示例4: _from_ordinal
# 需要导入模块: from datetime import datetime [as 别名]
# 或者: from datetime.datetime import fromordinal [as 别名]
def _from_ordinal(x, tz=None):
ix = int(x)
dt = datetime.fromordinal(ix)
remainder = float(x) - ix
hour, remainder = divmod(24 * remainder, 1)
minute, remainder = divmod(60 * remainder, 1)
second, remainder = divmod(60 * remainder, 1)
microsecond = int(1e6 * remainder)
if microsecond < 10:
microsecond = 0 # compensate for rounding errors
dt = datetime(dt.year, dt.month, dt.day, int(hour), int(minute),
int(second), microsecond)
if tz is not None:
dt = dt.astimezone(tz)
if microsecond > 999990: # compensate for rounding errors
dt += timedelta(microseconds=1e6 - microsecond)
return dt
### Fixed frequency dynamic tick locators and formatters
##### -------------------------------------------------------------------------
#---- --- Locators ---
##### -------------------------------------------------------------------------
示例5: datenum_to_datetime
# 需要导入模块: from datetime import datetime [as 别名]
# 或者: from datetime.datetime import fromordinal [as 别名]
def datenum_to_datetime(datenum):
"""
Convert Matlab datenum into Python datetime.
Parameters
----------
datenum: float
date in datenum format
Returns
-------
datetime :
Datetime object corresponding to datenum.
"""
days = datenum % 1.
return datetime.fromordinal(int(datenum)) \
+ timedelta(days=days) - timedelta(days=366)
示例6: datenum_to_datetime
# 需要导入模块: from datetime import datetime [as 别名]
# 或者: from datetime.datetime import fromordinal [as 别名]
def datenum_to_datetime(datenum):
"""
Convert Matlab datenum into Python datetime.
"""
days = datenum % 1
hours = days % 1 * 24
minutes = hours % 1 * 60
seconds = minutes % 1 * 60
try:
return (
datetime.fromordinal(int(datenum))
+ timedelta(days=int(days))
+ timedelta(hours=int(hours))
+ timedelta(minutes=int(minutes))
+ timedelta(seconds=round(seconds))
- timedelta(days=366)
)
except:
return datenum_to_datetime(700000)
示例7: test_get_day_of_year_dt
# 需要导入模块: from datetime import datetime [as 别名]
# 或者: from datetime.datetime import fromordinal [as 别名]
def test_get_day_of_year_dt():
dt = datetime.fromordinal(1 + np.random.randint(365 * 4000))
result = ccalendar.get_day_of_year(dt.year, dt.month, dt.day)
expected = (dt - dt.replace(month=1, day=1)).days + 1
assert result == expected
示例8: test_get_day_of_year
# 需要导入模块: from datetime import datetime [as 别名]
# 或者: from datetime.datetime import fromordinal [as 别名]
def test_get_day_of_year():
assert ccalendar.get_day_of_year(2001, 3, 1) == 60
assert ccalendar.get_day_of_year(2004, 3, 1) == 61
assert ccalendar.get_day_of_year(1907, 12, 31) == 365
assert ccalendar.get_day_of_year(2004, 12, 31) == 366
dt = datetime.fromordinal(1 + np.random.randint(365 * 4000))
result = ccalendar.get_day_of_year(dt.year, dt.month, dt.day)
expected = (dt - dt.replace(month=1, day=1)).days + 1
assert result == expected
示例9: test_days_ago
# 需要导入模块: from datetime import datetime [as 别名]
# 或者: from datetime.datetime import fromordinal [as 别名]
def test_days_ago(self):
today = pendulum.today()
today_midnight = pendulum.instance(datetime.fromordinal(today.date().toordinal()))
self.assertTrue(dates.days_ago(0) == today_midnight)
self.assertTrue(dates.days_ago(100) == today_midnight + timedelta(days=-100))
self.assertTrue(dates.days_ago(0, hour=3) == today_midnight + timedelta(hours=3))
self.assertTrue(dates.days_ago(0, minute=3) == today_midnight + timedelta(minutes=3))
self.assertTrue(dates.days_ago(0, second=3) == today_midnight + timedelta(seconds=3))
self.assertTrue(dates.days_ago(0, microsecond=3) == today_midnight + timedelta(microseconds=3))
示例10: _is_valid_episode
# 需要导入模块: from datetime import datetime [as 别名]
# 或者: from datetime.datetime import fromordinal [as 别名]
def _is_valid_episode(episode_data, show_key):
# Don't check old episodes (possible wrong season !)
date_string = episode_data.find("meta", itemprop="dateCreated")["content"]
date = datetime.fromordinal(dateutil.parser.parse(date_string).toordinal())
if date > datetime.utcnow():
return False
date_diff = datetime.utcnow() - date
if date_diff >= timedelta(days=2):
debug(" Episode too old")
return False
return True
示例11: _digest_episode
# 需要导入模块: from datetime import datetime [as 别名]
# 或者: from datetime.datetime import fromordinal [as 别名]
def _digest_episode(feed_episode):
debug("Digesting episode")
name = feed_episode.find("h4", itemprop="name", class_="episode__title").text
link = feed_episode.find("a", itemprop="url", class_="episode__link").href
num = int(feed_episode.find("meta", itemprop="episodeNumber")["content"])
date_string = feed_episode.find("meta", itemprop="dateCreated")["content"]
date = datetime.fromordinal(dateutil.parser.parse(date_string).toordinal())
return Episode(num, name, link, date)
示例12: test_fromordinal
# 需要导入模块: from datetime import datetime [as 别名]
# 或者: from datetime.datetime import fromordinal [as 别名]
def test_fromordinal():
assert datetime.fromordinal(730120) == pendulum.DateTime.fromordinal(730120)
示例13: calc_age
# 需要导入模块: from datetime import datetime [as 别名]
# 或者: from datetime.datetime import fromordinal [as 别名]
def calc_age(taken, dob):
birth = datetime.fromordinal(max(int(dob) - 366, 1))
if birth.month < 7:
return taken - birth.year
else:
return taken - birth.year - 1
示例14: calc_age
# 需要导入模块: from datetime import datetime [as 别名]
# 或者: from datetime.datetime import fromordinal [as 别名]
def calc_age(taken, dob):
birth = datetime.fromordinal(max(int(dob) - 366, 1))
if birth.month < 7:
return taken - birth.year
else:
return taken - birth.year - 1
示例15: calculate_age
# 需要导入模块: from datetime import datetime [as 别名]
# 或者: from datetime.datetime import fromordinal [as 别名]
def calculate_age(taken, dob):
birth = datetime.fromordinal(max(int(dob) - 366, 1))
if birth.month < 7:
return taken - birth.year
else:
return taken - birth.year - 1