CN112152631B - Method and device for encoding and decoding variable-length time string - Google Patents

Method and device for encoding and decoding variable-length time string Download PDF

Info

Publication number
CN112152631B
CN112152631B CN201910572006.9A CN201910572006A CN112152631B CN 112152631 B CN112152631 B CN 112152631B CN 201910572006 A CN201910572006 A CN 201910572006A CN 112152631 B CN112152631 B CN 112152631B
Authority
CN
China
Prior art keywords
time
length
string
value
preset
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.)
Active
Application number
CN201910572006.9A
Other languages
Chinese (zh)
Other versions
CN112152631A (en
Inventor
刘旸
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Hangzhou Hikvision Digital Technology Co Ltd
Original Assignee
Hangzhou Hikvision Digital Technology Co Ltd
Priority date (The priority date 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 date listed.)
Filing date
Publication date
Application filed by Hangzhou Hikvision Digital Technology Co Ltd filed Critical Hangzhou Hikvision Digital Technology Co Ltd
Priority to CN201910572006.9A priority Critical patent/CN112152631B/en
Publication of CN112152631A publication Critical patent/CN112152631A/en
Application granted granted Critical
Publication of CN112152631B publication Critical patent/CN112152631B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • HELECTRICITY
    • H03ELECTRONIC CIRCUITRY
    • H03MCODING; DECODING; CODE CONVERSION IN GENERAL
    • H03M7/00Conversion of a code where information is represented by a given sequence or number of digits to a code where the same, similar or subset of information is represented by a different sequence or number of digits
    • H03M7/02Conversion to or from weighted codes, i.e. the weight given to a digit depending on the position of the digit within the block or code word
    • H03M7/12Conversion to or from weighted codes, i.e. the weight given to a digit depending on the position of the digit within the block or code word having two radices, e.g. binary-coded-decimal code

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Compression, Expansion, Code Conversion, And Decoders (AREA)

Abstract

The embodiment of the invention provides a method and a device for encoding and decoding variable-length time strings, wherein the encoding method comprises the following steps: acquiring a type coding value corresponding to a time string according to the character length of the time string to be coded, wherein the type coding value is used for indicating the character length of the time string; acquiring a corresponding time stamp according to the time string, wherein the time stamp is the time length from the reference time to the time indicated by the time string; and coding according to the preset coding length, the type coding value and the time stamp to obtain a binary coding value of the time string, wherein the length of the binary coding value is the preset coding length, and the time strings with different character lengths correspond to the same preset coding length. The method and the device provided by the embodiment of the invention can accurately acquire the length of the binary code value, thereby accurately decoding the time string.

Description

Method and device for encoding and decoding variable-length time string
Technical Field
The embodiment of the invention relates to the technical field of computers, in particular to a method and a device for encoding and decoding a variable-length time string.
Background
As information technology is popular in life, business, and industry, the amount of data that needs to be stored in a computer increases exponentially. In the present-day data storage and analysis, time data is quite common, because many data are collected in time sequence, such as monitoring image data collected by a camera, order data of an e-commerce platform, and the like, the storage time data is not only beneficial to recording the time of occurrence of an event, but also beneficial to analyzing the change state of things, phenomena, and the like along with time.
One expression of the time data is a time string type, that is, "yyyy-MM-dd HH: MM: ss (year-month-day: minutes: seconds)", and in actual practice, the time string type may be long, that is, the length of the time string may be different. Such as a roll-up operation based on time data: the second-level data is rolled up into minute-level data, and the time string is 'yyyy-MM-dd HH: MM'; the minute-level data is rolled up into hour-level data, and the time string is 'yyyy-MM-dd HH'; the hour-level data is rolled up as day-level data, and the time string is "yyyy-MM-dd" until "yyyy". When a conventional encoding scheme encodes a variable-length time series, the conversion of time series of different lengths into binary format lengths is also different.
When a binary format on a storage device needs to be accessed and decoded into time string information, the time string information cannot be accurately decoded from a plurality of bytes because the decoder does not know what the length of the time string is.
Disclosure of Invention
The embodiment of the invention provides a method and a device for encoding and decoding a variable-length time string, which are used for solving the problem that the conventional encoding mode cannot accurately decode time string information from a plurality of bytes because the length of the time string is not known.
In a first aspect, an embodiment of the present invention provides a method for encoding a variable-length time string, including:
acquiring a type coding value corresponding to a time string according to the character length of the time string to be coded, wherein the type coding value is used for indicating the character length of the time string;
acquiring a corresponding time stamp according to the time string, wherein the time stamp is the time length from the reference time to the time indicated by the time string;
and coding according to the preset coding length, the type coding value and the time stamp to obtain a binary coding value of the time string, wherein the length of the binary coding value is the preset coding length, and the time strings with different character lengths correspond to the same preset coding length.
In one possible design, the encoding according to the preset encoding length, the type encoding value and the timestamp, to obtain the binary encoding value of the time string, includes:
encoding the type code value and the timestamp to obtain a decimal code value;
and converting the decimal coded value into the binary coded value according to the preset coding length.
In one possible design, the duration is a second-order duration, and the encoding the type encoded value and the timestamp to obtain a decimal encoded value includes:
multiplying the time stamp with a preset numerical value to obtain a time stamp after multiplication, wherein the preset numerical value is greater than 0 and is an integer multiple of 10;
and adding the time stamp after the multiplication processing and the type code value to obtain the decimal code value.
In one possible design, the obtaining the corresponding time stamp according to the time string includes:
if the character length of the time string is smaller than the preset length, expanding the time string to obtain a time string with the character length of the preset length;
and converting the time string with the character length being the preset length according to a preset conversion tool to obtain the time stamp.
In one possible design, the preset code length is 5 bytes.
In a second aspect, an embodiment of the present invention provides a method for decoding a variable-length time string, including:
acquiring a binary code value of a time string to be decoded according to a preset code length; the length of the binary code value is the preset code length, and the time strings of different character lengths correspond to the same preset code length;
obtaining a type code value and a time stamp of the time string according to the binary code value, wherein the time stamp is the time length from the reference time to the time indicated by the time string, and the type code value is used for indicating the character length of the time string;
and decoding according to the type code value and the time stamp to obtain the time string.
In one possible design, the obtaining the type-coded value and the timestamp of the time-series according to the binary-coded value includes:
converting the binary coded value to a decimal coded value;
and obtaining the type code value and the time stamp of the time string according to the decimal code value.
In one possible design, the deriving the type-coded value and the timestamp of the time-series from the decimal-coded value includes:
Performing modulo processing on the decimal coded value according to a preset numerical value to obtain the type coded value, wherein the preset numerical value is greater than 0 and is an integer multiple of 10;
and removing the last bit of the decimal coded value to obtain the timestamp.
In a third aspect, an embodiment of the present invention provides an apparatus for encoding a variable-length time string, including:
the first acquisition module is used for acquiring a type coding value corresponding to a time string according to the character length of the time string to be coded, wherein the type coding value is used for indicating the character length of the time string;
the second acquisition module is used for acquiring a corresponding time stamp according to the time string, wherein the time stamp is the time length from the reference time to the time indicated by the time string;
the encoding module is used for encoding according to a preset encoding length, the type encoding value and the time stamp to obtain a binary encoding value of the time string, wherein the length of the binary encoding value is the preset encoding length, and the time strings with different character lengths correspond to the same preset encoding length.
In one possible design, the coding module is specifically configured to:
encoding the type code value and the timestamp to obtain a decimal code value;
And converting the decimal coded value into the binary coded value according to the preset coding length.
In one possible design, the duration is a second-level duration, and the encoding module is specifically configured to:
multiplying the time stamp with a preset numerical value to obtain a time stamp after multiplication, wherein the preset numerical value is greater than 0 and is an integer multiple of 10;
and adding the time stamp after the multiplication processing and the type code value to obtain the decimal code value.
In one possible design, the second acquisition module is specifically configured to:
if the character length of the time string is smaller than the preset length, expanding the time string to obtain a time string with the character length of the preset length;
and converting the time string with the character length being the preset length according to a preset conversion tool to obtain the time stamp.
In a fourth aspect, an embodiment of the present invention provides a decoding apparatus for a variable-length time string, including:
the acquisition module is used for acquiring binary code values of the time string to be decoded according to the preset code length; the length of the binary code value is the preset code length, and the time strings of different character lengths correspond to the same preset code length;
The processing module is used for obtaining a type code value and a time stamp of the time string according to the binary code value, wherein the time stamp is the time length from the reference time to the time indicated by the time string, and the type code value is used for indicating the character length of the time string;
and the decoding module is used for decoding according to the type coding value and the time stamp to obtain the time string.
In one possible design, the processing module is specifically configured to:
converting the binary coded value to a decimal coded value;
and obtaining the type code value and the time stamp of the time string according to the decimal code value.
In one possible design, the processing module is specifically configured to:
performing modulo processing on the decimal coded value according to a preset numerical value to obtain the type coded value, wherein the preset numerical value is greater than 0 and is an integer multiple of 10;
and removing the last bit of the decimal coded value to obtain the timestamp.
In a fifth aspect, an embodiment of the present invention provides an encoding and decoding apparatus for a variable-length time string, including: at least one processor and memory;
the memory stores computer-executable instructions;
The at least one processor executing computer-executable instructions stored in the memory causes the at least one processor to perform the method of encoding a variable length time string as claimed in any one of the first aspects or causes the at least one processor to perform the method of decoding a variable length time string as claimed in any one of the second aspects.
In a sixth aspect, an embodiment of the present invention provides a computer-readable storage medium, in which computer-executable instructions are stored, which when executed by a processor, implement a method for encoding a variable-length time string according to any one of the first aspects, or implement a method for decoding a variable-length time string according to any one of the second aspects.
According to the method and the device for encoding and decoding the variable-length time string, the corresponding type encoding value is obtained through the character length of the time string to be encoded, the time stamp is obtained according to the time length from the time string to the time of the time string, the binary encoding value of the time string is obtained through encoding according to the preset encoding length, the type encoding value and the time stamp, the length of the binary encoding value is the preset encoding length after the time string is converted into the binary encoding value, and the corresponding preset encoding length is consistent after the time strings with different character lengths are converted into the binary encoding value, so that when the time string information in the binary format on the storage device is required to be decoded, only the bytes with the preset encoding length are required to be read.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions of the prior art, the drawings that are needed in the embodiments or the description of the prior art will be briefly described below, it will be obvious that the drawings in the following description are some embodiments of the present invention, and that other drawings can be obtained according to these drawings without inventive effort to a person skilled in the art.
FIG. 1 is a schematic diagram of a system for encoding and decoding variable-length time strings according to an embodiment of the present invention;
FIG. 2 is a schematic diagram of encoding a time series according to the prior art;
FIG. 3 is a flow chart of a method for encoding and compressing variable-length time strings according to an embodiment of the present invention;
FIG. 4 is a flowchart of a method for decoding variable-length time strings according to an embodiment of the present invention;
FIG. 5 is a schematic diagram of a device for encoding variable-length time strings according to an embodiment of the present invention;
fig. 6 is a schematic structural diagram of a decoding apparatus for variable-length time strings according to an embodiment of the present invention;
fig. 7 is a schematic hardware structure of an encoding and decoding apparatus for variable-length time strings according to an embodiment of the present invention.
Detailed Description
For the purpose of making the objects, technical solutions and advantages of the embodiments of the present invention more apparent, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention, and it is apparent that the described embodiments are some embodiments of the present invention, but not all embodiments of the present invention. All other embodiments, which can be made by those skilled in the art based on the embodiments of the invention without making any inventive effort, are intended to be within the scope of the invention.
Fig. 1 is a schematic diagram of an encoding and decoding system for variable-length time strings according to an embodiment of the present invention, where as shown in fig. 1, the system includes a server 11, a client 12 and a monitoring device 13, where the server 11 has computing and storage capabilities, the client 12 may be an intelligent terminal such as a mobile phone, a tablet or a computer, and the client 12 and the server 11 may be connected through a wired or wireless network. The monitoring device 13 is a device containing time data, and the monitoring device 13 and the server 11 may be connected through a wired or wireless network. For example, when the monitoring device 13 monitors a certain area, the video data of the monitored area has corresponding time, and the occurrence of each moment of the monitored area can be obtained according to the time data and the monitoring video.
In the present system the monitoring device 13 is only one possible means of containing time data, but other means of containing time data may be used instead, as applicable. In particular, when the server 11 and the client 12 are integrated in the monitoring device 13, the monitoring device 13 is a device with storage and operation capabilities, and the encoding and decoding of time data and the viewing of time data can be performed on the monitoring device 13.
When the time string needs to be stored, for example, the monitoring device 13 needs to store a monitoring video, and the monitoring video contains time data of each moment, at this time, the monitoring device 13 sends the time data to the server 11 in the form of time string, and the server 11 encodes and stores the time string. When the user wishes to acquire time data of the monitoring video, a request is transmitted to the server 11 through the client 12, the server 11 decodes the encoded corresponding time string according to the request, and transmits the decoded time data to the client 12.
The following describes the technical scheme of the present application and how the technical scheme of the present application solves the above technical problems in detail with specific embodiments. The following embodiments may be combined with each other, and the same or similar concepts or processes may not be described in detail in some embodiments. Embodiments of the present application will be described below with reference to the accompanying drawings.
As information technology is popular in life, business, and industry, the amount of data that needs to be stored in a computer increases exponentially. The data are usually written into the database in the form of characters and values, which can visually describe the information, and the finally written data are to be persisted on the storage device in the form of binary format, which requires the use of a suitable encoding method to encode the written characters and values format data, convert them into the binary format as small as possible, and at the same time ensure that the original data can be completely restored by the corresponding decoding method.
The time string is an expression form of time data, namely, "yyyy-MM-dd HH: MM: ss (year-month-day: minutes: seconds)", and the variable length time string means that the length of the time string is variable, for example, time string 1 is "yyyy-MM-dd HH: MM: ss", time string 2 is "yyyy-MM-dd HH: MM", and time string 3 is "yyy-MM-dd HH", and the lengths of time string 1, time string 2, and time string 3 are different.
In the conventional encoding and decoding of a variable-length time string, the method of ISO8895-1, UTF-8, unicode, GBK, or the like is generally adopted, and one byte is allocated to each number and symbol of the time string in the encoding method. Fig. 2 is a schematic diagram of encoding a time string provided in the prior art, as shown in fig. 2, taking a time string "2019" as an example, the conversion of four decimal numbers of 2, 0, 1, 9 into binary digits is respectively:
2------10,
0------0,
1------01,
9------1001,
The existing coding mode allocates one byte, namely eight bits, for each digit, so that "2019" is specifically:
2------00000010,
0------00000000,
1------00000001,
9------00001001,
thus "2019" becomes "00000010000000000000000100001001" after encoding, occupying four bytes. Table 1 is the number of bytes occupied by different time-series types under the existing coding scheme:
TABLE 1
yyyy-MM-dd HH:mm:ss 19 bytes
yyyy-MM-dd HH:mm 16 bytes
yyyy-MM-dd HH 13 bytes
yyyy-MM-dd 10 bytes
yyyy-MM 7 bytes
yyyy 4 bytes
In practical use, the longer the length of the time string is, the larger the time string is, because the time string has 12 months in 1 year, and 30 days … … of the longest 'yyyyy-MM-dd HH: MM: ss' has the length of 19 bytes after being converted into a binary format, and if the data volume is larger, only the time data occupies a large storage space. In practical application, if the time data are uniformly distributed, the storage space of each time string will exceed 11 bytes on average, and memory is wasted.
Accordingly, embodiments of the present invention provide a method of encoding compression and decoding a variable length time series that addresses this problem.
Fig. 3 is a flow chart of a method for encoding and compressing variable-length time strings according to an embodiment of the present invention, as shown in fig. 3, including:
s31, acquiring a type code value corresponding to a time string according to the character length of the time string to be coded, wherein the type code value is used for indicating the character length of the time string;
S32, acquiring a corresponding time stamp according to the time string, wherein the time stamp is the time length from the reference time to the time indicated by the time string;
s33, coding according to a preset coding length, the type coding value and the time stamp to obtain a binary coding value of the time string, wherein the length of the binary coding value is the preset coding length, and the time strings with different character lengths correspond to the same preset coding length.
In the embodiment of the invention, the time string is subjected to type coding, and the basis of the type coding is the character length of the time string. The types of time series are five in total, namely "yyyy" (year), "yyyy-MM" (year-month), "yyyy-MM-dd" (year-month-day), "yyyy-MM-dd HH: MM" (year-month-day: minute), and "yyyyy-MM-dd HH: MM: ss" (year-month-day: minute: second), and the character lengths of the five time series types are 4, 7, 10, 13, 16, and 19, respectively.
After the types of the time strings are obtained, the types of the time strings may be type-coded, for example, the type-coded values from small to large are set according to the character lengths of the time strings, such as 1,2,3,4,5, and 6, the type-coded values from large to small may be set, such as 6,5,4,3,2,1, or the type-coded values from large to small may not be arranged in the order of magnitude, such as 3,6,1,5,2,4, etc., but the type-coded values corresponding to the types of each time string are unique, and the type-coded values corresponding to the types of the time strings may be stored in the server.
After the type code value is obtained, the corresponding time stamp is needed to be obtained according to the time string, the time stamp is a metering number, the time stamp is calculated to need a reference time, and the time stamp corresponding to the time string is the difference value between the reference time and the time string. For example, the reference time is set to be 1970, 01, 00 minutes, 00 seconds, and the time series is 1970-01-01, 00, and the corresponding time stamp of the time series is 1 minute, namely 60 seconds.
After the type codes and the time stamps of the time strings are obtained, the time strings are coded by adopting a preset coding length, and the preset coding length can meet the access requirement of the time stamps. The encoded binary code value contains both a type code of the time string and time stamp information, from which the difference between the time string and the reference time can be known, from which the type and character length of the time string can be known. In the embodiment of the invention, the time string is stored by adopting the preset coding length for any time data, and the preset coding length is fixed, so that the number of bytes to be decoded can be obtained when decoding, and the time string information can be obtained.
According to the method for encoding the variable-length time string, the corresponding type encoding value is obtained through the character length of the time string to be encoded, the time stamp is obtained according to the time length from the time string to the time of the time string, the binary encoding value of the time string is obtained through encoding according to the preset encoding length, the type encoding value and the time stamp, the length of the binary encoding value is the preset encoding length after the time string is converted into the binary encoding value, and the corresponding preset encoding length is consistent after the time strings with different character lengths are converted into the binary encoding value, so that when the time string information in the binary format on the storage device is required to be decoded, only a plurality of bytes in the preset encoding length are required to be read.
The following describes the embodiment of the present application in detail with reference to a specific example.
The embodiment of the application provides a coding scheme for a variable-length time string in order to solve the problems that after the variable-length time string is converted into a binary format, the occupied storage space is large, and the binary format cannot be matched and decoded due to unfixed length.
First, for the character length of the time string, corresponding type code values are allocated for different lengths, table 2 is one possible allocation scheme:
TABLE 2
Character length Time string type Type coded value
4 yyyy 0
7 yyyy-MM 1
10 yyyy-MM-dd 2
13 yyyy-MM-dd HH 3
16 yyyy-MM-dd HH:mm 4
19 yyyy-MM-dd HH:mm:ss 5
The above type codes are only one possible code value, and specific type code values may be set according to actual needs, and are not particularly limited herein.
After the type codes are allocated, the corresponding time stamps are acquired according to the time strings. In the embodiment of the application, the reference time is set to be greenwich time of the place where the time data is acquired (greenwich time of 1970, 01 month, 01 day, 00 minutes and 00 seconds) (Beijing time of 1970, 01 month, 01 day, 08 minutes and 00 seconds), and the time string is also greenwich time of the place where the time data is acquired, and the two time data are conveniently calculated by taking the greenwich time as a standard, so that errors caused by different measurement standards are prevented.
In order to obtain the time stamp conveniently, namely, the difference between the time string and Greenwich mean time 1970, 01, 00 minutes and 00 seconds is obtained, the embodiment of the invention provides a concise method. And when the character length of the time string is smaller than the preset length, performing expansion processing on the time string to obtain the time string with the preset length. Specifically, the corresponding time stamp is obtained according to the time string, namely, from 1970-01-01:00:00 to the number of seconds elapsed by the time string, and the time string length is less than 19 bits is complemented by the following rule:
time string type: "yyyy", the complement is: "yyyy-01-01:00:00";
time string type: "yyyy-MM" with the following additions: "yyyy-MM-01:00:00";
time string type: "yyyy-MM-dd", the supplements are: "yyyy-MM-dd 00:00:00";
time string type: "yyyy-MM-dd HH", the complement is: "yyyy-MM-dd HH:00:00";
time string type: "yyyy-MM-dd HH: MM", the supplements are: "yyyy-MM-dd HH: MM:00".
For example, if the encoding time string "2019-03-21" is desired, the "2019-03-21" is first padded as "2019-03-21 00:00:00". After the time strings are expanded, the lengths of the time strings are kept consistent and are 19 bits, and at the moment, the time strings with the character length of 19 bits of the preset length are converted according to a preset conversion tool, so that the time stamp is obtained. In the embodiment of the invention, the preset conversion tool can be set according to the requirement, most programming languages are provided with tool types for acquiring the time stamp through the time string, and the preset conversion tool can also be written by a programmer. And the time string with the character length smaller than the preset length is subjected to expansion processing, so that the length of the converted time string is ensured to be consistent, and the time stamp is conveniently acquired by a preset conversion tool.
In the following, an example will be described in which the reference time is set to 1970, 01, 00 minutes, 00 seconds, the time series is 1970-01-01, 00:01, the type code value of the time series is set to 4, since the time string is less than 19 bits in length, the time string is subjected to expansion processing, so that the time string 1970-01-01:01:00 after expansion processing is obtained, and the corresponding time stamp of the time string is 1 minute, namely 60 seconds.
After the type code value and the timestamp are obtained, the type code value and the timestamp are coded to obtain a decimal code value, and concretely, the timestamp and a preset numerical value are subjected to multiplication to obtain the timestamp after multiplication, wherein the preset numerical value is larger than 0 and is an integer multiple of 10. And then, adding the time stamp after the multiplication and the type code value to obtain a decimal code value. For example, the time string is encoded according to the timestamp 60 and the type code value 4, and first, a preset value is selected, where the preset value may be 10, 20, 30, etc., in the embodiment of the present invention, 10 is selected as the preset value, and is multiplied with the timestamp 60 to obtain a timestamp 600 after multiplication, and then, the timestamp 600 after multiplication and the type code value 4 are summed to obtain a decimal code value 604. The last bit of the code value thus indicates the type of the time string, and the remaining bits carry the time information of the time string.
After the decimal coded value is obtained, the decimal coded value is converted into a binary coded value according to the preset coded length. In the embodiment of the present invention, the preset encoding length is 5 bytes, and the 5 byte length is selected because:
the binary code of 4 bytes can be maximally represented by 11111111111111111111111111111111, is converted into decimal 4294967295, the last bit is a type coded value according to the rule, the maximum time stamp which can be represented by the rest bit is 429496729, the time string which can be represented by the time stamp is maximally 1983, and the time string after 1983 cannot be represented, so that the time string obviously does not meet the requirement; the maximum of 5 bytes of binary codes can be represented as 1111111111111111111111111111111111111111, the binary codes are converted into 1099511627775 decimal codes, the last bit is a type coded value according to the rule, the maximum time stamp which can be represented by the remaining bits is 109951162777, and the time string which can be represented by the time stamp is 5454 years, so that the requirement of the existing time data storage can be met. The encoding length of more than 5 bytes can also meet the requirement, but 5 bytes are selected, so that the storage space is saved, and the preset encoding length is 5 bytes in the embodiment of the invention.
After the decimal coded value 604 is obtained, it is converted into a binary coded value. The decimal number 604 is 1001011100 converted into a binary number, and the binary number is converted according to a preset coding length to obtain a binary code value 0000 0000 0000 0000 0000 0000 0000 0010 0101 1100. Thus, the method provided by the embodiment of the invention successfully uses five bytes to encode the time series 1970-01-01:01 with the length of five bytes for storage.
According to the method for encoding the variable-length time string, the corresponding type encoding value is obtained through the character length of the time string to be encoded, the time stamp is obtained according to the time length from the time string to the time of the time string, the binary encoding value of the time string is obtained through encoding according to the preset encoding length, the type encoding value and the time stamp, the length of the binary encoding value is the preset encoding length after the time string is converted into the binary encoding value, and the corresponding preset encoding length is consistent after the time strings with different character lengths are converted into the binary encoding value, so that when the time string information in the binary format on the storage device is required to be decoded, only a plurality of bytes in the preset encoding length are required to be read. Meanwhile, the method provided by the embodiment of the invention can uniformly encode the variable-length time series into the binary codes of 5 bytes for storage, and the storage space of each time data exceeds 11 bytes on average under the condition that the time data types are uniformly distributed in the existing encoding mode.
After the time string is encoded and stored according to a certain rule, when the time string needs to be checked, the encoded time string needs to be decoded. Fig. 4 is a flow chart of a decoding method for variable-length time strings according to an embodiment of the present invention, as shown in fig. 4, including:
s41, acquiring a binary code value of a time string to be decoded according to a preset code length; the length of the binary code value is the preset code length, and the time strings of different character lengths correspond to the same preset code length;
s42, obtaining a type code value and a time stamp of the time string according to the binary code value, wherein the time stamp is the time length from the reference time to the time indicated by the time string, and the type code value is used for indicating the character length of the time string;
s43, decoding according to the type coding value and the time stamp to obtain the time string.
After the time string is encoded according to the rule, the storage forms of the time string are binary code values, and the preset code lengths are the same. The decoding process corresponds to the encoding process, and the preset encoding length, the preset numerical value and the type encoding value selected during encoding are correspondingly consistent during decoding. For example, when encoding, the preset encoding length is 5 bytes, and when decoding, only 5 bytes are needed to be read from the starting position backwards, so that the binary encoding value of the time string to be decoded can be obtained. The length of the binary coded value is 5 bytes.
For example, in the above embodiment, binary coded values are stored:
0000 0000 0000 0000 0000 0000 0000 0010 0101 1100,
the length of the time string is 5 bytes, and then a type code value and a time stamp of the time string to be decoded are needed to be obtained according to the binary code value, wherein the time stamp is the time length from the reference time to the time indicated by the time string, and the type code value indicates the character length of the time string.
First, binary coded values are converted into decimal coded values, for example, binary coded values:
0000 0000 0000 0000 0000 0000 0000 0010 0101 1100,
the result of the conversion to decimal coded values is 604, and then the type coded values and time stamps of the time series are derived from the decimal coded values 604. In the embodiment of the invention, after the decimal coded value is obtained, the decimal coded value is subjected to modulo processing according to a preset numerical value to obtain the type coded value, and then the last bit of the decimal coded value is removed to obtain the timestamp. The preset value is greater than 0 and is an integer multiple of 10, and in the process of encoding and decoding, the preset value is correspondingly equal, and the time string can be successfully decoded only if the preset value taken in the process of encoding is correspondingly equal in the process of decoding.
For example, in the encoding, the preset value takes 10 to obtain the corresponding decimal encoded value 604, so that the decimal encoded value 604 is subjected to modulo processing according to the preset value 10, that is, the decimal encoded value 604 is divided by 10, so that the remainder 4,4 is the corresponding type encoded value. The last bit of the decimal coded value is removed, namely the last bit 4 of the decimal coded value 604 is removed, and the last bit is converted into 60, and 60 is the corresponding time stamp.
Decoding is carried out according to the type coding value and the time stamp, and a time string is obtained. For example, 60 is a time stamp, indicating that the time series is 60 seconds, i.e., 1 minute, from Greenwich mean time 1970, 01, month 01, day 00, minute 00, and therefore the time series is 1970-01-01, 00:01:00. It is also known from the corresponding type code value 4 that the corresponding character length of the time string is 16, and thus the decoded time string is 1970-01-00:01 according to 1970-01-01:01:00.
According to the decoding method for the variable-length time string, firstly, binary code values of the time string are read according to the preset code length, then, type code values and time stamps of the time string are obtained according to the binary code values, and finally, decoding is conducted according to the type code values and the time stamps to obtain the time string. In decoding a time string, the storage length is 5 bytes no matter what length the time string is, so that the time string data is easy to identify and extract in decoding.
Fig. 5 is a schematic structural diagram of an encoding apparatus for variable-length time strings according to an embodiment of the present invention, as shown in fig. 5, including a first obtaining module 51, a second obtaining module 52, and an encoding module 53, where:
the first obtaining module 51 is configured to obtain a type code value corresponding to a time string according to a character length of the time string to be encoded, where the type code value is used to indicate the character length of the time string;
The second obtaining module 52 is configured to obtain a corresponding timestamp according to the time string, where the timestamp is a duration from a reference time to a time indicated by the time string;
the encoding module 53 is configured to encode according to a preset encoding length, the type encoding value and the timestamp to obtain a binary encoding value of the time string, where the length of the binary encoding value is the preset encoding length, and the time strings with different character lengths correspond to the same preset encoding length.
In one possible design, the encoding module 53 is specifically configured to:
encoding the type code value and the timestamp to obtain a decimal code value;
and converting the decimal coded value into the binary coded value according to the preset coding length.
In one possible design, the duration is a second-level duration, and the encoding module 53 is specifically configured to:
multiplying the time stamp with a preset numerical value to obtain a time stamp after multiplication, wherein the preset numerical value is greater than 0 and is an integer multiple of 10;
and adding the time stamp after the multiplication processing and the type code value to obtain the decimal code value.
In one possible design, the second acquisition module 52 is specifically configured to:
if the character length of the time string is smaller than the preset length, expanding the time string to obtain a time string with the character length of the preset length;
and converting the time string with the character length being the preset length according to a preset conversion tool to obtain the time stamp.
Fig. 6 is a schematic structural diagram of a decoding apparatus for variable-length time strings according to an embodiment of the present invention, as shown in fig. 6, including an acquisition module 61, a processing module 62 and a decoding module 63, where:
the obtaining module 61 is configured to obtain a binary code value of a time string to be decoded according to a preset code length; the length of the binary code value is the preset code length, and the time strings of different character lengths correspond to the same preset code length;
the processing module 62 is configured to obtain, according to the binary code value, a type code value of the time string and a timestamp, where the timestamp is a duration from a reference time to a time indicated by the time string, and the type code value is used to indicate a character length of the time string;
the decoding module 63 is configured to decode according to the type encoded value and the timestamp, so as to obtain the time string.
In one possible design, the processing module 62 is specifically configured to:
converting the binary coded value to a decimal coded value;
and obtaining the type code value and the time stamp of the time string according to the decimal code value.
In one possible design, the processing module 62 is specifically configured to:
performing modulo processing on the decimal coded value according to a preset numerical value to obtain the type coded value, wherein the preset numerical value is greater than 0 and is an integer multiple of 10;
and removing the last bit of the decimal coded value to obtain the timestamp.
The device provided by the embodiment of the invention can be used for executing the technical scheme of the embodiment of the method, and the implementation principle and the technical effect are similar, and are not repeated here.
Fig. 7 is a schematic hardware configuration diagram of an apparatus for encoding and decoding variable-length time strings according to an embodiment of the present invention, as shown in fig. 7, the apparatus for encoding and decoding variable-length time strings includes: at least one processor 71 and a memory 72. Wherein the processor 71 and the memory 72 are connected by a bus 73.
Optionally, the model determination further comprises a communication component. For example, the communication component may include a receiver and/or a transmitter.
In a specific implementation, the at least one processor 71 executes the computer-executable instructions stored in the memory 72, so that the at least one processor 71 performs the method for encoding the variable-length time series as described above, or so that the at least one processor 71 performs the method for decoding the variable-length time series as described above.
The specific implementation process of the processor 71 may be referred to the above method embodiment, and its implementation principle and technical effects are similar, and this embodiment will not be described herein again.
In the embodiment shown in fig. 7, it should be understood that the processor may be a central processing unit (english: central Processing Unit, abbreviated as CPU), or may be other general purpose processors, digital signal processors (english: digital Signal Processor, abbreviated as DSP), application specific integrated circuits (english: application Specific Integrated Circuit, abbreviated as ASIC), or the like. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like. The steps of a method disclosed in connection with the present invention may be embodied directly in a hardware processor for execution, or in a combination of hardware and software modules in a processor for execution.
The memory may comprise high speed RAM memory or may further comprise non-volatile storage NVM, such as at least one disk memory.
The bus may be an industry standard architecture (Industry Standard Architecture, ISA) bus, an external device interconnect (Peripheral Component, PCI) bus, or an extended industry standard architecture (Extended Industry Standard Architecture, EISA) bus, among others. The buses may be divided into address buses, data buses, control buses, etc. For ease of illustration, the buses in the drawings of the present application are not limited to only one bus or to one type of bus.
The present application also provides a computer-readable storage medium having stored therein computer-executable instructions that, when executed by a processor, implement the encoding and decoding methods for variable-length time strings as described above.
The computer readable storage medium described above may be implemented by any type of volatile or non-volatile memory device or combination thereof, such as Static Random Access Memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk, or optical disk. A readable storage medium can be any available medium that can be accessed by a general purpose or special purpose computer.
An exemplary readable storage medium is coupled to the processor such the processor can read information from, and write information to, the readable storage medium. In the alternative, the readable storage medium may be integral to the processor. The processor and the readable storage medium may reside in an application specific integrated circuit (Application Specific Integrated Circuits, ASIC for short). The processor and the readable storage medium may reside as discrete components in a device.
The division of the units is merely a logic function division, and there may be another division manner when actually implemented, for example, a plurality of units or components may be combined or may be integrated into another system, or some features may be omitted or not performed. Alternatively, the coupling or direct coupling or communication connection shown or discussed with each other may be an indirect coupling or communication connection via some interfaces, devices or units, which may be in electrical, mechanical or other form.
The units described as separate units may or may not be physically separate, and units shown as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
In addition, each functional unit in the embodiments of the present invention may be integrated in one processing unit, or each unit may exist alone physically, or two or more units may be integrated in one unit.
The functions, if implemented in the form of software functional units and sold or used as a stand-alone product, may be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention may be embodied essentially or in a part contributing to the prior art or in a part of the technical solution, in the form of a software product stored in a storage medium, comprising several instructions for causing a computer device (which may be a personal computer, a server, a network device, etc.) to perform all or part of the steps of the method according to the embodiments of the present invention. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read-Only Memory (ROM), a random access Memory (RAM, random Access Memory), a magnetic disk, or an optical disk, or other various media capable of storing program codes.
Those of ordinary skill in the art will appreciate that: all or part of the steps for implementing the method embodiments described above may be performed by hardware associated with program instructions. The foregoing program may be stored in a computer readable storage medium. The program, when executed, performs steps including the method embodiments described above; and the aforementioned storage medium includes: various media that can store program code, such as ROM, RAM, magnetic or optical disks.
Finally, it should be noted that: the above embodiments are only for illustrating the technical solution of the present invention, and not for limiting the same; although the invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical scheme described in the foregoing embodiments can be modified or some or all of the technical features thereof can be replaced by equivalents; such modifications and substitutions do not depart from the spirit of the invention.

Claims (8)

1. A method of encoding a variable length time train, comprising:
acquiring a type coding value corresponding to a time string according to the character length of the time string to be coded, wherein the type coding value is used for indicating the character length of the time string;
acquiring a corresponding time stamp according to the time string, wherein the time stamp is the time length from the reference time to the time indicated by the time string, and the time length is the second-level time length;
multiplying the time stamp with a preset numerical value to obtain a time stamp after multiplication, wherein the preset numerical value is greater than 0 and is an integer multiple of 10;
adding the time stamp after the multiplication processing and the type code value to obtain a decimal code value;
And converting the decimal code value into a binary code value according to a preset code length, wherein the length of the binary code value is the preset code length, and the time strings of different character lengths correspond to the same preset code length.
2. The method of claim 1, wherein the obtaining the corresponding time stamp from the time string comprises:
if the character length of the time string is smaller than the preset length, expanding the time string to obtain a time string with the character length of the preset length;
and converting the time string with the character length being the preset length according to a preset conversion tool to obtain the time stamp.
3. A method according to claim 1 or 2, wherein the preset code length is 5 bytes.
4. A method of decoding a variable length time train, comprising:
acquiring a binary code value of a time string to be decoded according to a preset code length; the length of the binary code value is the preset code length, and the time strings of different character lengths correspond to the same preset code length;
converting the binary coded value to a decimal coded value;
Performing modulo processing on the decimal coded value according to a preset numerical value to obtain a type coded value of the time string, wherein the preset numerical value is greater than 0 and is an integer multiple of 10;
removing the last bit of the decimal coded value to obtain a time stamp of the time string, wherein the time stamp is the time length from the reference time to the time indicated by the time string, and the type coded value is used for indicating the character length of the time string;
and decoding according to the type code value and the time stamp to obtain the time string.
5. An apparatus for encoding a variable-length time string, comprising:
the first acquisition module is used for acquiring a type coding value corresponding to a time string according to the character length of the time string to be coded, wherein the type coding value is used for indicating the character length of the time string;
the second acquisition module is used for acquiring a corresponding time stamp according to the time string, wherein the time stamp is the time length from the reference time to the time indicated by the time string, and the time length is the second-level time length;
coding module for
Multiplying the time stamp with a preset numerical value to obtain a time stamp after multiplication, wherein the preset numerical value is greater than 0 and is an integer multiple of 10;
Adding the time stamp after the multiplication processing and the type code value to obtain a decimal code value;
and converting the decimal code value into a binary code value according to a preset code length, wherein the length of the binary code value is the preset code length, and the time strings of different character lengths correspond to the same preset code length.
6. A decoding apparatus for a variable-length time string, comprising:
the acquisition module is used for acquiring binary code values of the time string to be decoded according to the preset code length; the length of the binary code value is the preset code length, and the time strings of different character lengths correspond to the same preset code length;
a processing module for
Converting the binary coded value to a decimal coded value;
performing modulo processing on the decimal coded value according to a preset numerical value to obtain a type coded value of the time string, wherein the preset numerical value is greater than 0 and is an integer multiple of 10;
removing the last bit of the decimal coded value to obtain a time stamp of the time string, wherein the time stamp is the time length from the reference time to the time indicated by the time string, and the type coded value is used for indicating the character length of the time string;
And the decoding module is used for decoding according to the type coding value and the time stamp to obtain the time string.
7. An apparatus for encoding and decoding a variable-length time string, comprising: at least one processor and memory;
the memory stores computer-executable instructions;
the at least one processor executing computer-executable instructions stored in the memory causes the at least one processor to perform the method of encoding a variable length time string as claimed in any one of claims 1 to 3 or causes the at least one processor to perform the method of decoding a variable length time string as claimed in claim 4.
8. A computer-readable storage medium having stored therein computer-executable instructions which, when executed by a processor, implement the method of encoding a variable length time string according to any one of claims 1 to 3 or the method of decoding a variable length time string according to claim 4.
CN201910572006.9A 2019-06-28 2019-06-28 Method and device for encoding and decoding variable-length time string Active CN112152631B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201910572006.9A CN112152631B (en) 2019-06-28 2019-06-28 Method and device for encoding and decoding variable-length time string

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201910572006.9A CN112152631B (en) 2019-06-28 2019-06-28 Method and device for encoding and decoding variable-length time string

Publications (2)

Publication Number Publication Date
CN112152631A CN112152631A (en) 2020-12-29
CN112152631B true CN112152631B (en) 2023-08-15

Family

ID=73869055

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201910572006.9A Active CN112152631B (en) 2019-06-28 2019-06-28 Method and device for encoding and decoding variable-length time string

Country Status (1)

Country Link
CN (1) CN112152631B (en)

Families Citing this family (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111181569B (en) * 2019-12-31 2021-06-15 山东信通电子股份有限公司 Compression method, device and equipment of time sequence data
CN111669616B (en) * 2020-06-23 2022-11-04 杭州海康威视系统技术有限公司 Encoding and decoding method and device and computer storage medium

Citations (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7865525B1 (en) * 2007-08-02 2011-01-04 Amazon Technologies, Inc. High efficiency binary encoding
CN104252441A (en) * 2013-06-27 2014-12-31 镇江雅迅软件有限责任公司 Method for implementing encoding mechanism based on time stamp rule
CN105306065A (en) * 2015-11-10 2016-02-03 珠海多玩信息技术有限公司 Processing method and device for timestamps, extracting method and device for compressed character strings
CN108681528A (en) * 2018-04-03 2018-10-19 卓望数码技术(深圳)有限公司 A kind of sequence number generation method and system
CN108829589A (en) * 2018-05-30 2018-11-16 平安普惠企业管理有限公司 Date data exchange method, device, computer equipment and storage medium
CN109165144A (en) * 2018-09-06 2019-01-08 南京聚铭网络科技有限公司 A kind of security log compression storage and search method based on variable-length record
CN109871362A (en) * 2019-02-13 2019-06-11 北京航空航天大学 A kind of data compression method towards streaming time series data

Family Cites Families (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6803866B2 (en) * 2002-12-19 2004-10-12 International Business Machines Corporation Efficient representation of date/time information
US11010415B2 (en) * 2012-04-30 2021-05-18 Sap Se Fixed string dictionary

Patent Citations (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7865525B1 (en) * 2007-08-02 2011-01-04 Amazon Technologies, Inc. High efficiency binary encoding
CN104252441A (en) * 2013-06-27 2014-12-31 镇江雅迅软件有限责任公司 Method for implementing encoding mechanism based on time stamp rule
CN105306065A (en) * 2015-11-10 2016-02-03 珠海多玩信息技术有限公司 Processing method and device for timestamps, extracting method and device for compressed character strings
CN108681528A (en) * 2018-04-03 2018-10-19 卓望数码技术(深圳)有限公司 A kind of sequence number generation method and system
CN108829589A (en) * 2018-05-30 2018-11-16 平安普惠企业管理有限公司 Date data exchange method, device, computer equipment and storage medium
CN109165144A (en) * 2018-09-06 2019-01-08 南京聚铭网络科技有限公司 A kind of security log compression storage and search method based on variable-length record
CN109871362A (en) * 2019-02-13 2019-06-11 北京航空航天大学 A kind of data compression method towards streaming time series data

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
(美)(P.杜波依斯)Paul DuBois著;钟鸣,田晓涛等译.《MySQL网络数据库指南》,.机械工业出版社,2000,第78-82页. *

Also Published As

Publication number Publication date
CN112152631A (en) 2020-12-29

Similar Documents

Publication Publication Date Title
CN109409518B (en) Neural network model processing method and device and terminal
CN105684316B (en) Polar code encoding method and device
CN112152631B (en) Method and device for encoding and decoding variable-length time string
US9286155B1 (en) Systems and methods for generating soft information in a flash device
CN106849956B (en) Compression method, decompression method, device and data processing system
CN110995273B (en) Data compression method, device, equipment and medium for power database
CN107577432A (en) A kind of security bargain data compression method and device
CN110554878A (en) data conversion method, game data processing method and device and server
TWI273779B (en) Method and apparatus for optimized lossless compression using a plurality of coders
CN112559592A (en) Real-time data processing method, device and equipment
CN113297001A (en) RAID (redundant array of independent disks) coding and decoding method and coding and decoding circuit
CN110209598B (en) Cache memory, data read-write control method and system
CN113177045A (en) Data extraction method and device, computable storage equipment and data request equipment
WO2017185213A1 (en) Encoding method and encoding device
CN111046631A (en) Name storage method and device based on character conversion and computer equipment
CN112232025B (en) Character string storage method and device and electronic equipment
CN111147082B (en) Securities trade data compression method and device
CN114610792A (en) Data processing method, device and system and industrial equipment
CN113518113A (en) File storage method and device, computer equipment and storage medium
CN112000509A (en) Erasure code encoding method, system and device based on vector instruction
CN110958100A (en) Equipment control method and device
CN109960922B (en) Method and device for identifying newly activated equipment
US20240070169A1 (en) Medoid-based data compression
CN113641308B (en) Compressed file index increment updating method and device and electronic equipment
CN111669616B (en) Encoding and decoding method and device and computer storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant