首页 \ 问答 \ 如何使用C获取当前时间(以毫秒为单位)?(How can I get the current time in milliseconds using C?)

如何使用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
更新时间:2023-03-24 22:03

最满意答案

我认为将事务包装在开始/救援块中时,意图会更清晰一些。

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 returnraise线

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 a create.___ template).

If you don't like the begin/rescue block you can just add an and return to the raise lines

raise ActiveRecord::Rollback and return

相关问答

更多