Get n days before now

When I tried to google  and get a way to get a Date() n days before now.
Lots of the answer are trying to do with Calendar.set() or GregorianCalendar.roll().
When n > 30, enven > 366, the solution becomes complex.
The easier way for me is calculating with milliseconds since January 1, 1970, 00:00:00 GMT.

A groovy example:
What you have to care for this way is the value of milliseconds is large.
For example, the value of milliseconds of 50days is more than 32-bit unsiged.
But the good news is that the value of milliseconds of 100years is less than 43-bit unsigned.

import java.util.Date

def now = new Date()
def nowMsFrom1970 = now.getTime()
def days = 32
def MsInOneDay = 24*3600*1000

def days_before = now.clone()
days_before.setTime(nowMsFrom1970 - (long)days * MsInOneDay)

log.info(now.format("YYYY-MM-dd HH:mm:ss"))
log.info("Day before " + days.toString() + " days:")
log.info(days_before.format("YYYY-MM-dd HH:mm:ss"))

result:
Thu Aug 23 22:38:18 CST 2012:INFO:2012-08-23 22:38:18
Thu Aug 23 22:38:18 CST 2012:INFO:Day before 32 days:
Thu Aug 23 22:38:18 CST 2012:INFO:2012-07-22 22:38:18
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
请帮我写一段单元测试,来测试以下代码:from flask import render_template, Blueprint, request, abort from flask_login import login_required, current_user from sqlalchemy import select, between, or_, desc from flbs.sign.sign_model import OperationLog, User from flbs.extensions import db from datetime import datetime dashboard_bp = Blueprint('dashboard', __name__) @dashboard_bp.before_request @login_required def add_operation_log(): # OperationLog.add_operation_log(current_user.userid, current_user.username) pass @dashboard_bp.route('/dashboard') def dashboard(): if 'd1' not in current_user.permissions: return abort(403) page = request.args.get('page', default=1) try: page = int(page) except ValueError: page = 1 daterange = request.args.get('daterange', default=datetime.now().strftime('%Y-%m-%d') + ' - ' + datetime.now().strftime( '%Y-%m-%d')) date_1 = daterange[:10] date_2 = daterange[-10:] + ' 23:59:59.999999' user = request.args.get('user', '').strip() module = request.args.get('module', '').strip() sql_query = select(OperationLog).where(between(OperationLog.c_date, datetime.strptime(date_1, "%Y-%m-%d"), datetime.strptime(date_2, "%Y-%m-%d %H:%M:%S.%f"))) if user: sql_query = sql_query.where(or_(OperationLog.userid == user, OperationLog.username == user)) if module: sql_query = sql_query.where( or_(OperationLog.endpoint.like("%" + module + "%"), OperationLog.full_path.like("%" + module + "%"))) sql_query = sql_query.order_by(desc(OperationLog.id)) # print(sql_query) paginated = db.paginate(select=sql_query, page=page, per_page=10) # 分页 pagination_query = {'daterange': daterange, 'user': user, 'module': module} return render_template('dashboard/dashboard.html', header_title='flask-sqlalchemy', tips='test pagination', user=user, daterange=daterange, module=module, paginated=paginated, endpoint=request.endpoint, total=paginated.total, full_path=request.full_path, pagination_query=pagination_query)
最新发布
05-25
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值