WO2017071357A1 - Time acquisition method and device - Google Patents
Time acquisition method and device Download PDFInfo
- Publication number
- WO2017071357A1 WO2017071357A1 PCT/CN2016/093086 CN2016093086W WO2017071357A1 WO 2017071357 A1 WO2017071357 A1 WO 2017071357A1 CN 2016093086 W CN2016093086 W CN 2016093086W WO 2017071357 A1 WO2017071357 A1 WO 2017071357A1
- Authority
- WO
- WIPO (PCT)
- Prior art keywords
- timestamp
- time
- storage area
- computer program
- unix
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Ceased
Links
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/52—Program synchronisation; Mutual exclusion, e.g. by means of semaphores
Definitions
- the present invention relates to the field of computer technologies, and in particular, to a time acquisition method and apparatus.
- the client (computer) requests synchronization to the time synchronization server through the network;
- the time synchronization server sends the current precise time to the client
- the time for directly acquiring the computer system is the same as the time synchronization server on the network.
- the time to modify the computer system usually requires advanced privileges at the administrator level.
- the embodiment of the invention provides a time acquisition method and device, which can provide accurate and correct time for the computer program, does not affect the operation of other programs, and requires low authority.
- the embodiment of the invention provides a time acquisition method, including:
- the Unix timestamp currently stored in the timestamp storage area is converted into time data, and the time data is used as the current time.
- the timing increases the value of the Unix timestamp stored in the timestamp storage area, specifically:
- the value of the Unix timestamp stored in the timestamp storage area is incremented by one every one second.
- the obtaining a Unix timestamp returned by the time synchronization server, and storing the Unix timestamp in the timestamp storage area is specifically:
- the timing increases the value of the Unix timestamp stored in the timestamp storage area, specifically:
- the value of the Unix timestamp stored in the timestamp storage area is incremented by one every 1 millisecond.
- assigning a time stamp storage area to the computer program in the memory, and sending the time stamp acquisition instruction to the time synchronization server includes:
- the time acquisition permission is a permission that is allocated in advance for a computer program that needs to use time during the running process.
- time acquisition method of the computer program further includes:
- the timestamp storage area is released from the memory when the execution of the computer program is stopped.
- an embodiment of the present invention further provides a time acquiring apparatus, including:
- a timestamp obtaining module configured to acquire a Unix timestamp returned by the time synchronization server, and store the Unix timestamp in the timestamp storage area;
- a timestamp maintenance module configured to periodically increase a value of a Unix timestamp stored in the timestamp storage area
- a time acquisition module configured to convert a Unix timestamp currently stored in the timestamp storage area into time data when the current time needs to be acquired in the process of executing the computer program, and use the time data as the current time .
- the timestamp maintenance module is specifically configured to add 1 to the value of the Unix timestamp stored in the timestamp storage area every 1 second.
- the timestamp obtaining module is specifically configured to acquire a Unix timestamp returned by the time synchronization server, convert the Unix timestamp into a millisecond format, and store the formatted Unix timestamp. In the timestamp storage area;
- the timestamp maintenance module is specifically configured to add 1 to the value of the Unix timestamp stored in the timestamp storage area every 1 millisecond.
- startup execution module includes:
- a permission verification unit configured to determine whether the computer program has time acquisition authority when starting to execute a computer program
- an execution unit configured to allocate a timestamp storage area in the memory for the computer program when the computer program has time acquisition permission, and send a timestamp acquisition instruction to the time synchronization server;
- the time acquisition permission is a permission that is allocated in advance for a computer program that needs to use time during the running process.
- the time acquisition device further includes a stop execution module
- the stop execution module is configured to release the timestamp storage area from the memory when the execution of the computer program is stopped.
- the embodiment of the present invention has the following beneficial effects: the embodiment of the present invention provides a time acquisition method, by storing a timestamp storage area in a memory for a computer program, and storing the Unix timestamp obtained from the time synchronization server in the The timestamp storage area is maintained in time and periodically; when the computer program needs to acquire the current time, the Unix timestamp currently stored in the timestamp storage area is converted into time data, and the time data is used as the current time. That is, the time of the computer program is independent of the time of the computer system, and does not affect each other, and the entire process does not change the time of the computer system.
- the embodiment of the present invention further provides a time acquiring device, which is used to execute the steps of the time acquiring method to achieve the corresponding technical effects.
- FIG. 1 is a schematic flowchart of a time acquisition method according to an embodiment of the present invention.
- FIG. 2 is a structural block diagram of a time acquisition apparatus according to an embodiment of the present invention.
- FIG. 1 is a schematic flowchart diagram of a first embodiment of a time acquisition method provided by the present invention, where the method includes the following steps:
- Timing increases a value of a Unix timestamp stored in the timestamp storage area.
- the Unix timestamp is the number of seconds elapsed since January 1, 1970 (the midnight of UTC/GMT), regardless of leap seconds.
- the 0 of the Unix timestamp is in accordance with the ISO 8601 specification: 1970-01-01T00:00:00Z.
- One hour is expressed as a UNIX timestamp format of: 3600 seconds; one day is represented as a UNIX timestamp of 86400 seconds.
- the timing in step S103 increases the value of the Unix timestamp stored in the timestamp storage area, specifically:
- the value of the Unix timestamp stored in the timestamp storage area is incremented by one every one second.
- the time precision of the Unix timestamp stored in the timestamp storage area is 1 second.
- the obtaining the Unix timestamp returned by the time synchronization server in step S102, and storing the Unix timestamp in the timestamp storage area is specifically:
- step S103 increases the value of the Unix timestamp stored in the timestamp storage area, specifically:
- the value of the Unix timestamp stored in the timestamp storage area is incremented by one every 1 millisecond.
- the time precision of the Unix timestamp stored in the timestamp storage area is 1 millisecond.
- the computer program allocates a timestamp storage area in the memory, and sends the timestamp acquisition instruction to the time synchronization server, including:
- the time acquisition permission is a computer program that is in advance required to use the time during the running process.
- the assigned permissions Only computer programs that use time acquisition permissions assign a timestamp storage area and send timestamp fetch instructions to the time synchronization server.
- time acquisition method of the computer program further includes:
- the timestamp storage area is released from the memory when the execution of the computer program is stopped. That is, the timestamp storage area is a storage area temporarily allocated when the computer program is running, and is released when the computer program is closed, reducing the memory footprint.
- the working principle of the time obtaining method is as follows: by allocating a timestamp storage area in the memory for the computer program, the Unix timestamp obtained from the time synchronization server is stored in the timestamp storage area and timed Maintaining; when the computer program needs to acquire the current time, converting the Unix timestamp currently stored in the timestamp storage area into time data, and using the time data as the current time, thus the computer program
- the time is independent of the time of the computer system and does not affect the time of the computer system during the time synchronization process.
- the alarm clock program After the alarm program is started, the alarm clock program allocates a timestamp storage area in the memory, and sends a timestamp acquisition instruction to the time synchronization server, and stores the Unix timestamp returned by the time synchronization server in the timestamp storage area. And periodically increase the value of the Unix timestamp.
- the alarm clock program acquires the current time every 1 minute. When the alarm clock program needs to obtain the current time, the Unix timestamp currently stored in the timestamp storage area is converted into time data, and the time data is used as the current time.
- the alarm program compares the current time obtained with the set ringing time, and starts ringing if the ringing time is reached.
- FIG. 2 it is a structural block diagram of a time acquisition apparatus according to an embodiment of the present invention, including:
- the execution execution module 101 is configured to allocate a timestamp storage area in the memory for the computer program when starting to execute a computer program, and send a timestamp acquisition instruction to the time synchronization server;
- a timestamp obtaining module 102 configured to acquire a Unix timestamp returned by the time synchronization server, and store the Unix timestamp in the timestamp storage area;
- a timestamp maintenance module 103 configured to periodically increase a value of a Unix timestamp stored in the timestamp storage area
- the time obtaining module 104 is configured to convert the Unix timestamp currently stored in the timestamp storage area into time data when the current time needs to be acquired in the process of executing the computer program, and use the time data as the current time.
- the timestamp maintenance module 103 is specifically configured to add 1 to the value of the Unix timestamp stored in the timestamp storage area every 1 second.
- the timestamp obtaining module 102 is specifically configured to acquire a Unix timestamp returned by the time synchronization server, convert the Unix timestamp into a millisecond format, and convert the formatted Unix timestamp. Stored in the timestamp storage area;
- the timestamp maintenance module 103 is specifically configured to add 1 to the value of the Unix timestamp stored in the timestamp storage area every 1 millisecond.
- startup execution module 101 includes:
- a permission verification unit configured to determine whether the computer program has time acquisition authority when starting to execute a computer program
- an execution unit configured to allocate a timestamp storage area in the memory for the computer program when the computer program has time acquisition permission, and send a timestamp acquisition instruction to the time synchronization server;
- the time acquisition permission is a permission that is allocated in advance for a computer program that needs to use time during the running process.
- the time acquisition device further includes a stop execution module
- the stop execution module is configured to release the timestamp storage area from the memory when the execution of the computer program is stopped.
- the time acquisition device provided in this embodiment is used to perform all the process steps of the time acquisition method described above, and the working principle and technical effects are in one-to-one correspondence, and thus are not described again.
- the embodiment of the present invention provides a time acquisition method, which is a time synchronization service by allocating a time stamp storage area in a memory for a computer program.
- the Unix timestamp obtained by the device is stored in the timestamp storage area and periodically maintained; when the computer program needs to acquire the current time, the Unix timestamp currently stored in the timestamp storage area is converted into time data. And using the time data as the current time. That is, the time of the computer program is independent of the time of the computer system, and does not affect each other, and the entire process does not change the time of the computer system.
- the embodiment of the present invention further provides a time acquiring device, which is used to execute the steps of the time acquiring method to achieve the corresponding technical effects.
- the storage medium may be a magnetic disk, an optical disk, a read-only memory (ROM), or a random access memory (RAM).
Landscapes
- Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Debugging And Monitoring (AREA)
Abstract
Description
本发明涉及计算机技术领域,尤其涉及一种时间获取方法与装置。The present invention relates to the field of computer technologies, and in particular, to a time acquisition method and apparatus.
很多计算机程序在运行时都需要获取当前的时间。为了保证获取的时间精确无误,计算机程序不是直接获取计算机系统上的时间(因为计算机系统上的时间可能不准确),而是通过网络向时间同步服务器获取正确的时间。Many computer programs need to get the current time at runtime. In order to ensure that the acquisition time is accurate, the computer program does not directly acquire the time on the computer system (because the time on the computer system may be inaccurate), but obtains the correct time through the network to the time synchronization server.
具体如下时间获取步骤如下:The specific steps are as follows:
1、客户端(计算机)通过网络向时间同步服务器请求同步;1. The client (computer) requests synchronization to the time synchronization server through the network;
2、时间同步服务器将当前的精确时间发给客户端;2. The time synchronization server sends the current precise time to the client;
3、客户端收到时间数据后更改计算机系统的时间;3. The time when the client changes the computer system after receiving the time data;
4、之后如果客户端需要获取系统时间的时候直接获取计算机系统的时间就是和网络上的时间同步服务器一致的时间。4. After that, if the client needs to obtain the system time, the time for directly acquiring the computer system is the same as the time synchronization server on the network.
由于计算机程序每次请求同步都需要更改计算机系统的时间,再读取计算机系统的时间作为当前的时间,则会存在以下的缺点:Since the computer program needs to change the time of the computer system every time it requests synchronization, and then reads the time of the computer system as the current time, there are the following disadvantages:
1、同步时间之后因为直接修改了计算机系统时间会影响到其他直接使用计算机时间的程序,可能导致其他程序出错;1. After the synchronization time, because the computer system time is directly modified, it will affect other programs that use the computer time directly, which may cause other programs to go wrong.
2、修改计算机系统的时间通常需要管理员级别的高级权限。2. The time to modify the computer system usually requires advanced privileges at the administrator level.
发明内容Summary of the invention
本发明实施例提出一种时间获取方法与装置,能够为计算机程序提供精确无误的时间,不会影响到其他程序的运行,并且需要的权限低。The embodiment of the invention provides a time acquisition method and device, which can provide accurate and correct time for the computer program, does not affect the operation of other programs, and requires low authority.
本发明实施例提供一种时间获取方法,包括:The embodiment of the invention provides a time acquisition method, including:
当开始执行一个计算机程序时,为所述计算机程序在内存中分配一个时间戳存储区,并向时间同步服务器发送时间戳获取指令; When starting to execute a computer program, assigning a time stamp storage area to the computer program in the memory, and sending a time stamp acquisition instruction to the time synchronization server;
获取所述时间同步服务器返回的Unix时间戳,并将所述Unix时间戳存储在所述时间戳存储区中;Obtaining a Unix timestamp returned by the time synchronization server, and storing the Unix timestamp in the timestamp storage area;
定时增加存储在所述时间戳存储区中的Unix时间戳的数值;Timingly increasing the value of the Unix timestamp stored in the timestamp storage area;
当执行所述计算机程序的过程中需要获取当前时间时,将所述时间戳存储区当前存储的Unix时间戳转换为时间数据,并将所述时间数据作为所述当前时间。When the current time needs to be acquired in the process of executing the computer program, the Unix timestamp currently stored in the timestamp storage area is converted into time data, and the time data is used as the current time.
在一个实施方式中,所述定时增加存储在所述时间戳存储区中的Unix时间戳的数值,具体是:In one embodiment, the timing increases the value of the Unix timestamp stored in the timestamp storage area, specifically:
每隔1秒的时间对存储在所述时间戳存储区中的Unix时间戳的数值加1。The value of the Unix timestamp stored in the timestamp storage area is incremented by one every one second.
在另一个实施方式中,所述获取所述时间同步服务器返回的Unix时间戳,并将所述Unix时间戳存储在所述时间戳存储区中具体是:In another embodiment, the obtaining a Unix timestamp returned by the time synchronization server, and storing the Unix timestamp in the timestamp storage area is specifically:
获取所述时间同步服务器返回的Unix时间戳,将所述Unix时间戳转换为毫秒的格式,并将转换格式后的Unix时间戳存储在所述时间戳存储区中;Obtaining a Unix timestamp returned by the time synchronization server, converting the Unix timestamp into a millisecond format, and storing the converted Unix timestamp in the timestamp storage area;
则所述定时增加存储在所述时间戳存储区中的Unix时间戳的数值,具体是:The timing increases the value of the Unix timestamp stored in the timestamp storage area, specifically:
每隔1毫秒的时间对存储在所述时间戳存储区中的Unix时间戳的数值加1。The value of the Unix timestamp stored in the timestamp storage area is incremented by one every 1 millisecond.
进一步地,所述当开始执行一个计算机程序时,为所述计算机程序在内存中分配一个时间戳存储区,并向时间同步服务器发送时间戳获取指令包括:Further, when the computer program starts to be executed, assigning a time stamp storage area to the computer program in the memory, and sending the time stamp acquisition instruction to the time synchronization server includes:
当开始执行一个计算机程序时,判断所述计算机程序是否具有时间获取权限;When starting to execute a computer program, determining whether the computer program has time acquisition authority;
若所述计算机程序具有时间获取权限,则为所述计算机程序在内存中分配一个时间戳存储区,并向时间同步服务器发送时间戳获取指令;If the computer program has time acquisition permission, assigning a time stamp storage area to the computer program in the memory, and sending a timestamp acquisition instruction to the time synchronization server;
其中,所述时间获取权限是预先为在运行过程中需要使用时间的计算机程序分配的权限。The time acquisition permission is a permission that is allocated in advance for a computer program that needs to use time during the running process.
进一步地,所述计算机程序的时间获取方法还包括:Further, the time acquisition method of the computer program further includes:
当停止执行所述计算机程序时,从所述内存中释放所述时间戳存储区。The timestamp storage area is released from the memory when the execution of the computer program is stopped.
相应地,本发明实施例还提供一种时间获取装置,包括:Correspondingly, an embodiment of the present invention further provides a time acquiring apparatus, including:
启动执行模块,用于当开始执行一个计算机程序时,为所述计算机程序在 内存中分配一个时间戳存储区,并向时间同步服务器发送时间戳获取指令;Launching an execution module for when the computer program is started to be executed Allocating a timestamp storage area in the memory and sending a timestamp acquisition instruction to the time synchronization server;
时间戳获取模块,用于获取所述时间同步服务器返回的Unix时间戳,并将所述Unix时间戳存储在所述时间戳存储区中;a timestamp obtaining module, configured to acquire a Unix timestamp returned by the time synchronization server, and store the Unix timestamp in the timestamp storage area;
时间戳维护模块,用于定时增加存储在所述时间戳存储区中的Unix时间戳的数值;a timestamp maintenance module, configured to periodically increase a value of a Unix timestamp stored in the timestamp storage area;
时间获取模块,用于当执行所述计算机程序的过程中需要获取当前时间时,将所述时间戳存储区当前存储的Unix时间戳转换为时间数据,并将所述时间数据作为所述当前时间。a time acquisition module, configured to convert a Unix timestamp currently stored in the timestamp storage area into time data when the current time needs to be acquired in the process of executing the computer program, and use the time data as the current time .
在一个实施方式中,所述时间戳维护模块具体用于每隔1秒的时间对存储在所述时间戳存储区中的Unix时间戳的数值加1。In an embodiment, the timestamp maintenance module is specifically configured to add 1 to the value of the Unix timestamp stored in the timestamp storage area every 1 second.
在另一个实施方式中,所述时间戳获取模块具体用于获取所述时间同步服务器返回的Unix时间戳,将所述Unix时间戳转换为毫秒的格式,并将转换格式后的Unix时间戳存储在所述时间戳存储区中;In another embodiment, the timestamp obtaining module is specifically configured to acquire a Unix timestamp returned by the time synchronization server, convert the Unix timestamp into a millisecond format, and store the formatted Unix timestamp. In the timestamp storage area;
所述时间戳维护模块具体用于每隔1毫秒的时间对存储在所述时间戳存储区中的Unix时间戳的数值加1。The timestamp maintenance module is specifically configured to add 1 to the value of the Unix timestamp stored in the timestamp storage area every 1 millisecond.
进一步地,所述启动执行模块包括:Further, the startup execution module includes:
权限验证单元,用于当开始执行一个计算机程序时,判断所述计算机程序是否具有时间获取权限;a permission verification unit, configured to determine whether the computer program has time acquisition authority when starting to execute a computer program;
启动执行单元,用于当所述计算机程序具有时间获取权限时,为所述计算机程序在内存中分配一个时间戳存储区,并向时间同步服务器发送时间戳获取指令;Activating an execution unit, configured to allocate a timestamp storage area in the memory for the computer program when the computer program has time acquisition permission, and send a timestamp acquisition instruction to the time synchronization server;
其中,所述时间获取权限是预先为在运行过程中需要使用时间的计算机程序分配的权限。The time acquisition permission is a permission that is allocated in advance for a computer program that needs to use time during the running process.
进一步地,所述时间获取装置还包括停止执行模块;Further, the time acquisition device further includes a stop execution module;
所述停止执行模块用于当停止执行所述计算机程序时,从所述内存中释放所述时间戳存储区。 The stop execution module is configured to release the timestamp storage area from the memory when the execution of the computer program is stopped.
实施本发明实施例,具有如下有益效果:本发明实施例提出一种时间获取方法,通过为计算机程序在内存中分配一个时间戳存储区,将从时间同步服务器得到的Unix时间戳存储在所述时间戳存储区中并定时对其维护;在所述计算机程序需要获取当前时间时,则将所述时间戳存储区当前存储的Unix时间戳转换为时间数据,并将所述时间数据作为所述当前时间。即计算机程序的时间与计算机系统的时间是独立的,互不影响,整个过程不会更改计算机系统的时间。因此本方案不会受到其他程序或者人为更改计算机系统的时间的影响,也不会影响到其他需要使用计算机系统的时间的程序,并且无需高级别的权限。同时本发明实施例还提供了一种时间获取装置,用于执行上述时间获取方法的步骤,达到相应的技术效果。The embodiment of the present invention has the following beneficial effects: the embodiment of the present invention provides a time acquisition method, by storing a timestamp storage area in a memory for a computer program, and storing the Unix timestamp obtained from the time synchronization server in the The timestamp storage area is maintained in time and periodically; when the computer program needs to acquire the current time, the Unix timestamp currently stored in the timestamp storage area is converted into time data, and the time data is used as the current time. That is, the time of the computer program is independent of the time of the computer system, and does not affect each other, and the entire process does not change the time of the computer system. Therefore, this solution is not affected by other programs or the time required to change the computer system, nor does it affect other programs that require the use of the computer system, and does not require high-level permissions. At the same time, the embodiment of the present invention further provides a time acquiring device, which is used to execute the steps of the time acquiring method to achieve the corresponding technical effects.
图1是本发明实施例提供的时间获取方法的流程示意图;1 is a schematic flowchart of a time acquisition method according to an embodiment of the present invention;
图2是本发明实施例提供的时间获取装置的结构框图。FIG. 2 is a structural block diagram of a time acquisition apparatus according to an embodiment of the present invention.
下面将结合本发明实施例中的附图,对本发明实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例仅仅是本发明一部分实施例,而不是全部的实施例。基于本发明中的实施例,本领域普通技术人员在没有作出创造性劳动前提下所获得的所有其他实施例,都属于本发明保护的范围。The technical solutions in the embodiments of the present invention are clearly and completely described in the following with reference to the accompanying drawings in the embodiments of the present invention. It is obvious that the described embodiments are only a part of the embodiments of the present invention, but not all embodiments. All other embodiments obtained by those skilled in the art based on the embodiments of the present invention without creative efforts are within the scope of the present invention.
参见图1,是本发明提供的时间获取方法的第一实施例的流程示意图,该方法包括以下步骤:FIG. 1 is a schematic flowchart diagram of a first embodiment of a time acquisition method provided by the present invention, where the method includes the following steps:
S101,当开始启动一个计算机程序时,为所述计算机程序在内存中分配一个时间戳存储区,并向时间同步服务器发送时间戳获取指令;S101. When starting to start a computer program, allocate a timestamp storage area in the memory for the computer program, and send a timestamp acquisition instruction to the time synchronization server;
S102,获取所述时间同步服务器返回的Unix时间戳,并将所述Unix时间戳存储在所述时间戳存储区中;S102. Acquire a Unix timestamp returned by the time synchronization server, and store the Unix timestamp in the timestamp storage area.
S103,定时增加存储在所述时间戳存储区中的Unix时间戳的数值; S103. Timing increases a value of a Unix timestamp stored in the timestamp storage area.
S104,当执行所述计算机程序的过程中需要获取当前时间时,将所述时间戳存储区当前存储的Unix时间戳转换为时间数据,并将所述时间数据作为所述当前时间。S104. When the current time needs to be acquired in the process of executing the computer program, convert the Unix timestamp currently stored in the timestamp storage area into time data, and use the time data as the current time.
Unix时间戳是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒。Unix时间戳的0按照ISO 8601规范为:1970-01-01T00:00:00Z。一个小时表示为UNIX时间戳格式为:3600秒;一天表示为UNIX时间戳为86400秒。The Unix timestamp is the number of seconds elapsed since January 1, 1970 (the midnight of UTC/GMT), regardless of leap seconds. The 0 of the Unix timestamp is in accordance with the ISO 8601 specification: 1970-01-01T00:00:00Z. One hour is expressed as a UNIX timestamp format of: 3600 seconds; one day is represented as a UNIX timestamp of 86400 seconds.
在一个实施方式中,步骤S103中所述定时增加存储在所述时间戳存储区中的Unix时间戳的数值,具体是:In an embodiment, the timing in step S103 increases the value of the Unix timestamp stored in the timestamp storage area, specifically:
每隔1秒的时间对存储在所述时间戳存储区中的Unix时间戳的数值加1。The value of the Unix timestamp stored in the timestamp storage area is incremented by one every one second.
因此,存储在所述时间戳存储区中的Unix时间戳对应的时间精度为1秒。Therefore, the time precision of the Unix timestamp stored in the timestamp storage area is 1 second.
在另一个实施例中,步骤S102中所述获取所述时间同步服务器返回的Unix时间戳,并将所述Unix时间戳存储在所述时间戳存储区中具体是:In another embodiment, the obtaining the Unix timestamp returned by the time synchronization server in step S102, and storing the Unix timestamp in the timestamp storage area is specifically:
获取所述时间同步服务器返回的Unix时间戳,将所述Unix时间戳转换为毫秒的格式,并将转换格式后的Unix时间戳存储在所述时间戳存储区中;Obtaining a Unix timestamp returned by the time synchronization server, converting the Unix timestamp into a millisecond format, and storing the converted Unix timestamp in the timestamp storage area;
则步骤S103中所述定时增加存储在所述时间戳存储区中的Unix时间戳的数值,具体是:Then, the timing in step S103 increases the value of the Unix timestamp stored in the timestamp storage area, specifically:
每隔1毫秒的时间对存储在所述时间戳存储区中的Unix时间戳的数值加1。The value of the Unix timestamp stored in the timestamp storage area is incremented by one every 1 millisecond.
因此,存储在所述时间戳存储区中的Unix时间戳对应的时间精度为1毫秒。Therefore, the time precision of the Unix timestamp stored in the timestamp storage area is 1 millisecond.
进一步地,步骤S101中所述当开始执行一个计算机程序时,为所述计算机程序在内存中分配一个时间戳存储区,并向时间同步服务器发送时间戳获取指令包括:Further, when the computer program starts to be executed in step S101, the computer program allocates a timestamp storage area in the memory, and sends the timestamp acquisition instruction to the time synchronization server, including:
当开始执行一个计算机程序时,判断所述计算机程序是否具有时间获取权限;When starting to execute a computer program, determining whether the computer program has time acquisition authority;
若所述计算机程序具有时间获取权限,则为所述计算机程序在内存中分配一个时间戳存储区,并向时间同步服务器发送时间戳获取指令;If the computer program has time acquisition permission, assigning a time stamp storage area to the computer program in the memory, and sending a timestamp acquisition instruction to the time synchronization server;
其中,所述时间获取权限是预先为在运行过程中需要使用时间的计算机程 序分配的权限。只有用于时间获取权限的计算机程序才会分配一个时间戳存储区以及向时间同步服务器发送时间戳获取指令。The time acquisition permission is a computer program that is in advance required to use the time during the running process. The assigned permissions. Only computer programs that use time acquisition permissions assign a timestamp storage area and send timestamp fetch instructions to the time synchronization server.
进一步地,所述计算机程序的时间获取方法还包括:Further, the time acquisition method of the computer program further includes:
当停止执行所述计算机程序时,从所述内存中释放所述时间戳存储区。即所述时间戳存储区是当所述计算机程序正在运行时临时分配的存储区,当所述计算机程序关闭后则会释放,减少内存占用空间。The timestamp storage area is released from the memory when the execution of the computer program is stopped. That is, the timestamp storage area is a storage area temporarily allocated when the computer program is running, and is released when the computer program is closed, reducing the memory footprint.
本发明实施例提供的时间获取方法的工作原理如下:通过为计算机程序在内存中分配一个时间戳存储区,将从时间同步服务器得到的Unix时间戳存储在所述时间戳存储区中并定时对其维护;在所述计算机程序需要获取当前时间时,则将所述时间戳存储区当前存储的Unix时间戳转换为时间数据,并将所述时间数据作为所述当前时间,因此将计算机程序的时间和计算机系统的时间独立开来,在时间同步过程中不会影响到计算机系统的时间。The working principle of the time obtaining method provided by the embodiment of the present invention is as follows: by allocating a timestamp storage area in the memory for the computer program, the Unix timestamp obtained from the time synchronization server is stored in the timestamp storage area and timed Maintaining; when the computer program needs to acquire the current time, converting the Unix timestamp currently stored in the timestamp storage area into time data, and using the time data as the current time, thus the computer program The time is independent of the time of the computer system and does not affect the time of the computer system during the time synchronization process.
以下以闹钟程序为例说明本实施例的工作过程:The following describes the working process of this embodiment by taking an alarm clock program as an example:
闹钟程序在启动后,为所述闹钟程序在内存中分配一个时间戳存储区,并向时间同步服务器发送时间戳获取指令,将时间同步服务器返回的Unix时间戳存储在所述时间戳存储区中并定时增加Unix时间戳的数值。闹钟程序每隔1分钟获取当前的时间,在所述闹钟程序需要获取当前时间时,则将所述时间戳存储区当前存储的Unix时间戳转换为时间数据,并将所述时间数据作为所述当前时间。闹钟程序将获取到的当前时间与设定的打铃时间比较,如果到达打铃时间就开始打铃。After the alarm program is started, the alarm clock program allocates a timestamp storage area in the memory, and sends a timestamp acquisition instruction to the time synchronization server, and stores the Unix timestamp returned by the time synchronization server in the timestamp storage area. And periodically increase the value of the Unix timestamp. The alarm clock program acquires the current time every 1 minute. When the alarm clock program needs to obtain the current time, the Unix timestamp currently stored in the timestamp storage area is converted into time data, and the time data is used as the current time. The alarm program compares the current time obtained with the set ringing time, and starts ringing if the ringing time is reached.
相应地,本发明实施例还提供一种时间获取装置,用于执行上述的时间获取方法。如图2所示,其是本发明实施例提供的一种时间获取装置的结构框图,包括:Correspondingly, the embodiment of the invention further provides a time acquiring device for performing the time acquisition method described above. As shown in FIG. 2, it is a structural block diagram of a time acquisition apparatus according to an embodiment of the present invention, including:
启动执行模块101,用于当开始执行一个计算机程序时,为所述计算机程序在内存中分配一个时间戳存储区,并向时间同步服务器发送时间戳获取指令;The
时间戳获取模块102,用于获取所述时间同步服务器返回的Unix时间戳,并将所述Unix时间戳存储在所述时间戳存储区中;
a
时间戳维护模块103,用于定时增加存储在所述时间戳存储区中的Unix时间戳的数值;a
时间获取模块104,用于当执行所述计算机程序的过程中需要获取当前时间时,将所述时间戳存储区当前存储的Unix时间戳转换为时间数据,并将所述时间数据作为所述当前时间。The
在一个实施方式中,所述时间戳维护模块103具体用于每隔1秒的时间对存储在所述时间戳存储区中的Unix时间戳的数值加1。In an embodiment, the
在另一个实施方式中,所述时间戳获取模块102具体用于获取所述时间同步服务器返回的Unix时间戳,将所述Unix时间戳转换为毫秒的格式,并将转换格式后的Unix时间戳存储在所述时间戳存储区中;In another embodiment, the
所述时间戳维护模块103具体用于每隔1毫秒的时间对存储在所述时间戳存储区中的Unix时间戳的数值加1。The
进一步地,所述启动执行模块101包括:Further, the
权限验证单元,用于当开始执行一个计算机程序时,判断所述计算机程序是否具有时间获取权限;a permission verification unit, configured to determine whether the computer program has time acquisition authority when starting to execute a computer program;
启动执行单元,用于当所述计算机程序具有时间获取权限时,为所述计算机程序在内存中分配一个时间戳存储区,并向时间同步服务器发送时间戳获取指令;Activating an execution unit, configured to allocate a timestamp storage area in the memory for the computer program when the computer program has time acquisition permission, and send a timestamp acquisition instruction to the time synchronization server;
其中,所述时间获取权限是预先为在运行过程中需要使用时间的计算机程序分配的权限。The time acquisition permission is a permission that is allocated in advance for a computer program that needs to use time during the running process.
进一步地,所述时间获取装置还包括停止执行模块;Further, the time acquisition device further includes a stop execution module;
所述停止执行模块用于当停止执行所述计算机程序时,从所述内存中释放所述时间戳存储区。The stop execution module is configured to release the timestamp storage area from the memory when the execution of the computer program is stopped.
本实施例提供的时间获取装置,用于执行上述的时间获取方法的所有流程步骤,其工作原理和技术效果一一对应,因而不再赘述。The time acquisition device provided in this embodiment is used to perform all the process steps of the time acquisition method described above, and the working principle and technical effects are in one-to-one correspondence, and thus are not described again.
实施本发明实施例,具有如下有益效果:本发明实施例提出一种时间获取方法,通过为计算机程序在内存中分配一个时间戳存储区,将从时间同步服务 器得到的Unix时间戳存储在所述时间戳存储区中并定时对其维护;在所述计算机程序需要获取当前时间时,则将所述时间戳存储区当前存储的Unix时间戳转换为时间数据,并将所述时间数据作为所述当前时间。即计算机程序的时间与计算机系统的时间是独立的,互不影响,整个过程不会更改计算机系统的时间。因此本方案不会受到其他程序或者人为更改计算机系统的时间的影响,也不会影响到其他需要使用计算机系统的时间的程序,并且无需高级别的权限。同时本发明实施例还提供了一种时间获取装置,用于执行上述时间获取方法的步骤,达到相应的技术效果。The embodiments of the present invention have the following beneficial effects: the embodiment of the present invention provides a time acquisition method, which is a time synchronization service by allocating a time stamp storage area in a memory for a computer program. The Unix timestamp obtained by the device is stored in the timestamp storage area and periodically maintained; when the computer program needs to acquire the current time, the Unix timestamp currently stored in the timestamp storage area is converted into time data. And using the time data as the current time. That is, the time of the computer program is independent of the time of the computer system, and does not affect each other, and the entire process does not change the time of the computer system. Therefore, this solution is not affected by other programs or the time required to change the computer system, nor does it affect other programs that require the use of the computer system, and does not require high-level permissions. At the same time, the embodiment of the present invention further provides a time acquiring device, which is used to execute the steps of the time acquiring method to achieve the corresponding technical effects.
本领域普通技术人员可以理解实现上述实施例方法中的全部或部分流程,是可以通过计算机程序来指令相关的硬件来完成,所述的程序可存储于一计算机可读取存储介质中,该程序在执行时,可包括如上述各方法的实施例的流程。其中,所述的存储介质可为磁碟、光盘、只读存储记忆体(Read-Only Memory,ROM)或随机存储记忆体(Random Access Memory,RAM)等。One of ordinary skill in the art can understand that all or part of the process of implementing the foregoing embodiments can be completed by a computer program to instruct related hardware, and the program can be stored in a computer readable storage medium. When executed, the flow of an embodiment of the methods as described above may be included. The storage medium may be a magnetic disk, an optical disk, a read-only memory (ROM), or a random access memory (RAM).
以上所述是本发明的优选实施方式,应当指出,对于本技术领域的普通技术人员来说,在不脱离本发明原理的前提下,还可以做出若干改进和润饰,这些改进和润饰也视为本发明的保护范围。 The above is a preferred embodiment of the present invention, and it should be noted that those skilled in the art can also make several improvements and retouchings without departing from the principles of the present invention. It is the scope of protection of the present invention.
Claims (10)
Applications Claiming Priority (2)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN201510711748.7 | 2015-10-27 | ||
| CN201510711748.7A CN105242974A (en) | 2015-10-27 | 2015-10-27 | Time acquisition method and device |
Publications (1)
| Publication Number | Publication Date |
|---|---|
| WO2017071357A1 true WO2017071357A1 (en) | 2017-05-04 |
Family
ID=55040628
Family Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| PCT/CN2016/093086 Ceased WO2017071357A1 (en) | 2015-10-27 | 2016-08-03 | Time acquisition method and device |
Country Status (2)
| Country | Link |
|---|---|
| CN (1) | CN105242974A (en) |
| WO (1) | WO2017071357A1 (en) |
Cited By (2)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN110519002A (en) * | 2019-09-27 | 2019-11-29 | 苏州浪潮智能科技有限公司 | A kind of method for synchronizing time and relevant apparatus |
| CN113489657A (en) * | 2021-06-29 | 2021-10-08 | 中国银联股份有限公司 | Distributed flow velocity control system and operation method thereof |
Families Citing this family (5)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN105242974A (en) * | 2015-10-27 | 2016-01-13 | 广州视睿电子科技有限公司 | Time acquisition method and device |
| CN107122001B (en) * | 2017-03-31 | 2019-06-04 | 武汉斗鱼网络科技有限公司 | A kind of method and device that application program is synchronous with server time |
| CN114520704A (en) * | 2020-11-19 | 2022-05-20 | 比亚迪股份有限公司 | Local area network time synchronization method, device, computer equipment and storage medium |
| CN114489870A (en) * | 2022-02-11 | 2022-05-13 | 上海驻云信息科技有限公司 | Method and system for automatically converting UNIX timestamp units |
| CN117896035B (en) * | 2024-03-14 | 2024-06-04 | 杭州义益钛迪信息技术有限公司 | Data acquisition method and equipment of edge controller |
Citations (6)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US6311283B1 (en) * | 1998-09-17 | 2001-10-30 | Apple Computer, Inc. | Need based synchronization of computer system time clock to reduce loading on network server |
| US20080183623A1 (en) * | 2007-01-29 | 2008-07-31 | Zhangwei Xu | Secure Provisioning with Time Synchronization |
| CN101866399A (en) * | 2009-04-16 | 2010-10-20 | 成都市华为赛门铁克科技有限公司 | Method and terminal for offline file control |
| CN102497665A (en) * | 2011-12-28 | 2012-06-13 | 深圳市五巨科技有限公司 | Mobile terminal time correction method and apparatus thereof |
| CN104935465A (en) * | 2015-06-17 | 2015-09-23 | 北京京东尚科信息技术有限公司 | Log generating method and log generating device of client application program, and server |
| CN105242974A (en) * | 2015-10-27 | 2016-01-13 | 广州视睿电子科技有限公司 | Time acquisition method and device |
Family Cites Families (1)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US7920086B2 (en) * | 2006-07-07 | 2011-04-05 | Honeywell International Inc. | Display for displaying compressed video |
-
2015
- 2015-10-27 CN CN201510711748.7A patent/CN105242974A/en active Pending
-
2016
- 2016-08-03 WO PCT/CN2016/093086 patent/WO2017071357A1/en not_active Ceased
Patent Citations (6)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US6311283B1 (en) * | 1998-09-17 | 2001-10-30 | Apple Computer, Inc. | Need based synchronization of computer system time clock to reduce loading on network server |
| US20080183623A1 (en) * | 2007-01-29 | 2008-07-31 | Zhangwei Xu | Secure Provisioning with Time Synchronization |
| CN101866399A (en) * | 2009-04-16 | 2010-10-20 | 成都市华为赛门铁克科技有限公司 | Method and terminal for offline file control |
| CN102497665A (en) * | 2011-12-28 | 2012-06-13 | 深圳市五巨科技有限公司 | Mobile terminal time correction method and apparatus thereof |
| CN104935465A (en) * | 2015-06-17 | 2015-09-23 | 北京京东尚科信息技术有限公司 | Log generating method and log generating device of client application program, and server |
| CN105242974A (en) * | 2015-10-27 | 2016-01-13 | 广州视睿电子科技有限公司 | Time acquisition method and device |
Cited By (2)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN110519002A (en) * | 2019-09-27 | 2019-11-29 | 苏州浪潮智能科技有限公司 | A kind of method for synchronizing time and relevant apparatus |
| CN113489657A (en) * | 2021-06-29 | 2021-10-08 | 中国银联股份有限公司 | Distributed flow velocity control system and operation method thereof |
Also Published As
| Publication number | Publication date |
|---|---|
| CN105242974A (en) | 2016-01-13 |
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| WO2017071357A1 (en) | Time acquisition method and device | |
| US7453910B1 (en) | Synchronization of independent clocks | |
| US11609598B2 (en) | Network card, time synchronization methods and devices, and computer storage media | |
| US11520753B1 (en) | Time reservations for ensuring consistent reads in a distributed database without logging | |
| JP6079879B2 (en) | Distribution apparatus, distribution system, and distribution method | |
| CN106897206B (en) | Service testing method and device | |
| CN109558065B (en) | Data deleting method and distributed storage system | |
| CN107040327B (en) | Slave TDMA slot synchronous calibration method and device | |
| US12022015B2 (en) | Method for distributed and secure timekeeping | |
| EP2902940B1 (en) | Storage system and method for processing data operation request | |
| CN106899654B (en) | Sequence value generation method, device and system | |
| CN115987477B (en) | Multi-reference source time synchronization method, system, module and medium | |
| US20190377597A1 (en) | Managing execution of data processing jobs in a virtual computing environment | |
| CN100371851C (en) | Time synchronization method and device | |
| US20120266010A1 (en) | Conversion of timestamps between multiple entities within a computing system | |
| CN108377173A (en) | A kind of clock synchronizing method and device and vehicle | |
| CN113126694A (en) | Time synchronization method, system, equipment and storage medium of computer management subsystem | |
| JP2013206472A (en) | Method of forming timestamp in real-time system, data processing device, computer program product, and digital storage medium | |
| JP4744199B2 (en) | Time correction method and time correction device | |
| WO2025123727A1 (en) | Time synchronization method and system, electronic device, storage medium and vehicle | |
| CN106533598A (en) | Synchronizing Device | |
| CN107241365A (en) | A kind of cloud platform method for synchronizing time | |
| US20140173264A1 (en) | Priority activation of metrology driver in boot sequence | |
| Kobori et al. | Performance evaluation of portable time synchronization method using eBPF | |
| CN114189305A (en) | Time synchronization method, device, equipment and readable storage medium |
Legal Events
| Date | Code | Title | Description |
|---|---|---|---|
| 121 | Ep: the epo has been informed by wipo that ep was designated in this application |
Ref document number: 16858796 Country of ref document: EP Kind code of ref document: A1 |
|
| NENP | Non-entry into the national phase |
Ref country code: DE |
|
| 122 | Ep: pct application non-entry in european phase |
Ref document number: 16858796 Country of ref document: EP Kind code of ref document: A1 |