好吧,你可能会认为这个问题已被提出,但我认为它没有.我读到的所有解决方案都有这种"拼图"技术(如getUTCMonth() + getUTCMinutes + ...).但由于我只想比较两个UTC(!)日期之间的经过秒数,因此不适用.
众所周知,您可以通过以下方式获取当前(非UTC)日期:
var d = new Date();
var t_millis = d.getTime();
Run Code Online (Sandbox Code Playgroud)
但这不是我想要的.我想以UTC 和毫秒为目前的系统日期,所以根本不要乱用字符串.AFAIK变量t_millis将包含GMT中当前时间戳的毫秒值,而不是UTC.(因为d在GMT中也是如此.除非 getTime()执行一种隐式时区转换,即在给出毫秒之前添加偏移量,但我从未在任何地方读过这些内容)
那么除了将偏移量添加到时间值之外真的没别的办法吗?我拼命地错过了getUTCTimeMillis()其他语言中已知的功能.
我试图找到一种方法来使用JS的Array.prototype.map()功能,该函数有一个额外的参数(如果可能的话,我想避免重写内置的Array.prototype.map()).此文档非常好,但不包括"一个或多个附加参数"的情况:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map
function doOpSingle(elem)
{
// do something with one array element
}
var A = ["one", "two", "three", "four"];
var x = A.map(doOpSingle); // this will execute doOpSingle() on each array element
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.但是,如果有问题的函数有两个参数,例如你可能想要对它进行OR的标记(想想位掩码)怎么办?
function doOpSingle2(arrelem,flag)
{
// do something with one array element
}
var A = ["one", "two", "three", "four"];
var theFlag = util.getMask(); // call external function
var y = A.map(doOpSingle2(theFlag)); // this does not work!
Run Code Online (Sandbox Code Playgroud)
当然,任何解决方案都应该没有 for …
我正在尝试创建一个python应用程序来读取通过DBus传递的消息,这些消息提供了与bash dbus-monitor相同的输出.根据我从搜索中得到的代码,代码应该非常简单明了,例如:
import dbus, gobject
from dbus.mainloop.glib import DBusGMainLoop
def msg_cb(bus, msg):
args = msg.get_args_list()
print "Notification from '%s'" % args[0]
print "Summary: %s" % args[3]
print "Body: %s", args[4]
if __name__ == '__main__':
DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
string = "interface='org.freedesktop.Notifications',member='Notify'"
bus.add_match_string(string)
bus.add_message_filter(msg_cb)
mainloop = gobject.MainLoop ()
mainloop.run ()
Run Code Online (Sandbox Code Playgroud)
但启动它我只得到DBus返回的消息说应用程序是连接的,这与我执行bash命令时得到的不同:
dbus-monitor --session interface='org.freedesktop.Notifications',member='Notify'
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我可以观看与过滤条件匹配的所有消息.有人请帮我理解失败的地方吗?谢谢
javascript ×2
arrays ×1
callback ×1
date ×1
dbus ×1
map ×1
milliseconds ×1
parameters ×1
python ×1
utc ×1