如何使用C获取当前时间(以毫秒为单位)?(How can I get the current time in milliseconds using C?)
如何在C中获得当前时间(以毫秒为单位)? 我正在做以下事情以便在几秒钟内获得时间:
struct tm ptm; now = time(NULL); localtime_r(&now,ptm); myTime= (ptm->tm_hour * 3600) + (ptm->tm_min * 60) + (ptm->tm_sec);
查看time.h,
struct tm
中没有毫秒成员。How might I get the current time in milliseconds in C? I am doing following to get the time in seconds:
struct tm ptm; now = time(NULL); localtime_r(&now,ptm); myTime= (ptm->tm_hour * 3600) + (ptm->tm_min * 60) + (ptm->tm_sec);
Looking at time.h,
struct tm
does not have the millisecond member in it.
原文:https://stackoverflow.com/questions/1370163
最满意答案
我认为将事务包装在开始/救援块中时,意图会更清晰一些。
def create begin ActiveRecord::Base.transaction do @user = User.new params[:user] unless @user.save raise ActiveRecord::Rollback end //More stuff ... @order = Order.new params[:order] ... unless @order.save raise ActiveRecord::Rollback end end rescue ActiveRecord::Rollback render action: "new" and return end end
您需要在
create
方法中返回 ,否则它的执行将继续到方法的结尾,并且将发生Rails默认渲染(在这种情况下,它意味着尝试查找create.___
模板) 。如果你不喜欢开始/救援区,你可以添加一个
and return
到raise
线raise ActiveRecord::Rollback and return
I think the intention is a bit clearer when wrapping the transaction in a begin/rescue block.
def create begin ActiveRecord::Base.transaction do @user = User.new params[:user] unless @user.save raise ActiveRecord::Rollback end //More stuff ... @order = Order.new params[:order] ... unless @order.save raise ActiveRecord::Rollback end end rescue ActiveRecord::Rollback render action: "new" and return end end
You need to return in the
create
method, otherwise it's execution will continue to the end of the method and Rails default render will occur (in this case it means attempting to find acreate.___
template).If you don't like the begin/rescue block you can just add an
and return
to theraise
linesraise ActiveRecord::Rollback and return
相关问答
更多-
最后,我想通了! # rescue NoMethodError def method_missing(method, *args, &block) if method == 'class_breadcrumb' flash.now["alert-danger"] = "You didn't provide a breadcrumb for #{request.fullpath}! Please send us a feedback including this message!" ren ...
-
这可以在https://github.com/ruby-grape/grape/pull/939中找到 ,来自https://github.com/ruby-grape/grape/issues/1300 。 This is answered in https://github.com/ruby-grape/grape/pull/939, pointed from https://github.com/ruby-grape/grape/issues/1300.
-
从范围内的ActiveRecord :: RecordNotFound救援(rescue from ActiveRecord::RecordNotFound in scope)[2022-03-15]
您的范围永远不会引发异常。 你find (可以引发异常) def get_active_question @question = Question.active.first if @question # you have a question render 'one_view' else # no active questions render 'another_view' end end Your scope will never raise an ... -
你可以这样做: def create r = Record.create(record_params) if r.save render json: r.to_json else render json: errors: r.errors, status: 422 end end 如果返回错误,它将返回: { errors: { attr_1:["can't be blank"], attr_2:["can't be blank"] ...
-
我认为将事务包装在开始/救援块中时,意图会更清晰一些。 def create begin ActiveRecord::Base.transaction do @user = User.new params[:user] unless @user.save raise ActiveRecord::Rollback end //More stuff ... @order = Order.new params[: ...
-
我只是在尝试找出解决同一问题的方法时阅读了您的帖子。 最后我做了以下事情: class ExampleController < ApplicationController rescue_from Exception, :with => :render_404 ... private def render_404(exception = nil) logger.info "Exception, redirecting: #{exception.message}" if exceptio ...
-
如何在“render:template => ...”之后访问“assign”?(How do I access “assigns” after “render :template => …”?)[2023-05-15]
这很奇怪,我不知道为什么。 作为替代方案,您是否尝试将异常作为显式本地传递? def not_found(exception) render :template => '/errors/not_found', :status => 404, :locals => {:exception => exception} end 和观点:Error 404: Not Found
<%= debug exception %>