CN112152631A - Method and device for coding and decoding variable-length time string - Google Patents

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

Info

Publication number
CN112152631A
CN112152631A CN201910572006.9A CN201910572006A CN112152631A CN 112152631 A CN112152631 A CN 112152631A CN 201910572006 A CN201910572006 A CN 201910572006A CN 112152631 A CN112152631 A CN 112152631A
Authority
CN
China
Prior art keywords
time
length
value
string
coding
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.)
Granted
Application number
CN201910572006.9A
Other languages
Chinese (zh)
Other versions
CN112152631B (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

Images

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 coding and decoding a variable-length time string, wherein the coding method comprises the following steps: acquiring a type coding value corresponding to a time string to be coded according to the character length of the time string, wherein the type coding value is used for indicating the character length of the time string; acquiring a corresponding timestamp according to the time string, wherein the timestamp is the time length from the reference time to the time indicated by the time string; and coding according to a preset coding length, the type coding value and the timestamp 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 coding 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 variable-length time strings.
Background
With the popularization of information technology in the fields of life, business and industry, the amount of data required to be stored in a computer is exponentially increased. In data storage and analysis of today, time data is very 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, and the time data is saved, so that the 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 the time.
One expression of the time data is a time string type, i.e., "yyyy-MM-dd HH: MM: ss (year-month-day: minute: second)", and in practical use, the time string type may be long, i.e., the length of the time string may be different. E.g. a volume up operation based on time data: the second-level data is scrolled to be minute-level data, and the time string is 'yyyy-MM-dd HH: MM'; the minute-level data is scrolled to be hour-level data, and the time string is 'yyyy-MM-dd HH'; the hourly data is scrolled to day data and the time series is "yyyy-MM-dd" up to "yyyyy". When the existing coding mode codes the time series with variable length, the length of the time series with different lengths is converted into the binary format is also different.
When the binary format on the storage device needs to be accessed and decoded into time string information, the time string information cannot be accurately decoded from multiple 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 coding and decoding a variable-length time string, which aim to solve the problem that time string information cannot be accurately decoded from a plurality of bytes due to the fact that the length of the time string is not known in the existing coding mode.
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 to be coded according to the character length of the time string, wherein the type coding value is used for indicating the character length of the time string;
acquiring a corresponding timestamp according to the time string, wherein the timestamp is the time length from the reference time to the time indicated by the time string;
and coding according to a preset coding length, the type coding value and the timestamp 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:
coding the type coding value and the timestamp to obtain a decimal coding value;
and converting the decimal code value into the binary code value according to the preset code length.
In one possible design, the duration is in seconds, and the encoding the type code value and the timestamp to obtain a decimal code value includes:
multiplying the time stamp by a preset value to obtain the time stamp after multiplication, wherein the preset value is greater than 0 and is an integral multiple of 10;
and adding the time stamp subjected to the multiplication processing and the type code value to obtain the decimal code value.
In one possible design, the obtaining the corresponding timestamp from the time string includes:
if the character length of the time string is smaller than a preset length, performing expansion processing on the time string to obtain the time string with the character length being 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 timestamp.
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 coding value of a time string to be decoded according to a preset coding length; the length of the binary code value is the preset code length, and time strings with different character lengths correspond to the same preset code length;
obtaining a type coding value and a timestamp of the time string according to the binary coding value, wherein the timestamp is the time length from the reference time to the time indicated by the time string, and the type coding value is used for indicating the character length of the time string;
and decoding according to the type coding value and the timestamp to obtain the time string.
In one possible design, the obtaining the type-coded value and the timestamp of the time string according to the binary-coded value includes:
converting the binary coded value to a decimal coded value;
and obtaining the type coding value and the time stamp of the time string according to the decimal coding value.
In one possible design, the obtaining the type-coded value and the timestamp of the time string according to the decimal-coded value includes:
performing modulus processing on the decimal code value according to a preset value to obtain the type code value, wherein the preset value is greater than 0 and is an integral multiple of 10;
removing the last digit of the decimal encoded 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 device comprises a first obtaining module, a second obtaining module and a third obtaining module, wherein the first obtaining module is used for obtaining a type code value corresponding to a time string to be coded according to the character length of the time string, and the type code value is used for indicating the character length of the time string;
the second acquisition module is used for acquiring a corresponding timestamp according to the time string, wherein the timestamp is the time length from the reference time to the time indicated by the time string;
and the coding module is used for coding according to a preset coding length, the type coding value and the timestamp 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 module is specifically configured to:
coding the type coding value and the timestamp to obtain a decimal coding value;
and converting the decimal code value into the binary code value according to the preset code length.
In one possible design, the duration is in the order of seconds, and the encoding module is specifically configured to:
multiplying the time stamp by a preset value to obtain the time stamp after multiplication, wherein the preset value is greater than 0 and is an integral multiple of 10;
and adding the time stamp subjected to the multiplication processing and the type code value to obtain the decimal code value.
In one possible design, the second obtaining module is specifically configured to:
if the character length of the time string is smaller than a preset length, performing expansion processing on the time string to obtain the time string with the character length being 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 timestamp.
In a fourth aspect, an embodiment of the present invention provides an apparatus for decoding a variable-length time string, including:
the acquisition module is used for 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 time strings with different character lengths correspond to the same preset code length;
the processing module is used for obtaining a type coding value and a timestamp of the time string according to the binary coding value, the timestamp is the time length from reference time to the time indicated by the time string, and the type coding 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 timestamp 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 coding value and the time stamp of the time string according to the decimal coding value.
In one possible design, the processing module is specifically configured to:
performing modulus processing on the decimal code value according to a preset value to obtain the type code value, wherein the preset value is greater than 0 and is an integral multiple of 10;
removing the last digit of the decimal encoded value to obtain the timestamp.
In a fifth aspect, an embodiment of the present invention provides an apparatus for encoding and decoding a variable-length time string, including: at least one processor and memory;
the memory stores computer-executable instructions;
the at least one processor executing the computer-executable instructions stored by the memory causes the at least one processor to perform the method of encoding a variable-length time string according to any one of the first aspect or causes the at least one processor to perform the method of decoding a variable-length time string according to any one of the second aspect.
In a sixth aspect, an embodiment of the present invention provides a computer-readable storage medium, where a computer executes instructions, and when a processor executes the computer to execute the instructions, the method for encoding a variable-length time string according to any one of the first aspect is implemented, or the method for decoding a variable-length time string according to any one of the second aspect is implemented.
The method and the device for coding and decoding the variable-length time string provided by the embodiment of the invention firstly obtain the corresponding type coding value through the character length of the time string to be coded, then obtain the time stamp according to the time length from the reference time to the time string time, finally carry out coding according to the preset coding length, the type coding value and the time stamp to obtain the binary coding value of the time string, convert the time string into the binary coding value, the length of the binary coding value is the preset coding length, and after the time strings with different character lengths are converted into the binary coding value, the corresponding preset coding lengths are consistent, therefore, when the time string information of the binary format on the storage device needs to be decoded, only the bytes with the preset coding length need to be read, the method provided by the embodiment of the invention can accurately obtain the length of the binary coding value, thereby accurately decoding the time string.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings needed to be used in the description of the embodiments or the prior art will be briefly introduced below, and it is obvious that the drawings in the following description are some embodiments of the present invention, and for those skilled in the art, other drawings can be obtained according to these drawings without creative efforts.
FIG. 1 is a diagram of a system for encoding and decoding variable-length time strings according to an embodiment of the present invention;
FIG. 2 is a diagram of encoding a time string according to the prior art;
FIG. 3 is a flowchart illustrating a method for compressing codes of variable-length time strings according to an embodiment of the present invention;
FIG. 4 is a flowchart illustrating a method for decoding a variable-length time string according to an embodiment of the present invention;
FIG. 5 is a schematic structural diagram of an apparatus for encoding variable-length time strings according to an embodiment of the present invention;
FIG. 6 is a schematic structural diagram of an apparatus for decoding variable-length time strings according to an embodiment of the present invention;
fig. 7 is a schematic diagram of a hardware structure of an apparatus for encoding and decoding a variable-length time string according to an embodiment of the present invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are some, but not all, embodiments of the present invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
Fig. 1 is a schematic diagram of a system for encoding and decoding a variable-length time string according to an embodiment of the present invention, as shown in fig. 1, the system includes a server 11, a client 12, and a monitoring device 13, 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 by a wired or wireless network. The monitoring device 13 is a device containing time data, and the monitoring device 13 and the server 11 can 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 what happens at each moment in the monitored area can be acquired according to the time data and the monitoring video.
In the present system, the monitoring device 13 is only one possible device containing time data, and when applied, the device can be replaced by other devices containing time data. In a specific implementation, the server 11 and the client 12 may be integrated in the monitoring device 13, or may be disposed outside the monitoring device 13, and 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 computation capabilities, on which encoding and decoding of time data and viewing of time data may be performed.
When the time string needs to be stored, for example, the monitoring device 13 needs to store a section of monitoring video, the monitoring video includes time data of each time, at this time, the monitoring device 13 sends the time data to the server 11 in the form of the time string, and the server 11 encodes and stores the time string. When a user wishes to acquire time data of a monitoring video, a request is sent to the server 11 through the client 12, the server 11 decodes the encoded corresponding time string according to the request, and sends the decoded time data to the client 12.
The technical solution of the present invention and how to solve the above technical problems will be described in detail with specific examples. The following several specific embodiments may be combined with each other, and details of the same or similar concepts or processes may not be repeated in some embodiments. Embodiments of the present invention will be described below with reference to the accompanying drawings.
With the popularization of information technology in the fields of life, business and industry, the amount of data required to be stored in a computer is exponentially increased. The data are usually written into the database in a format of characters and numerical values which can visually describe information, and the finally written data are persisted on the storage device in a binary format, so that the written data in the format of the characters and the numerical values need to be encoded by using a proper encoding method and converted into the binary format as small as possible, and meanwhile, the original data can be completely restored by using a corresponding decoding method.
The time string is an expression form of time data, i.e., "yyyy-MM-dd HH: MM: ss (year-month-day: minute: second)", and the variable-length time string means that the length of the time string is varied, for example, the time string 1 is "yyyy-MM-dd HH: MM: ss", the time string 2 is "yyyyyy-MM-dd HH: MM", the time string 3 is "yyyyyyy-MM-dd HH", and the lengths of the time string 1, the time string 2, and the time string 3 are different from each other.
The existing coding and decoding of the time series with variable length generally adopts the modes of ISO8895-1, UTF-8, Unicode or GBK, etc., and the coding mode allocates one byte for each number and symbol of the time series. Fig. 2 is a schematic diagram of encoding a time string provided in the prior art, and as shown in fig. 2, taking a time string "2019" as an example, four decimal digits of 2, 0, 1, and 9 are converted into binary digits, respectively:
2------10,
0------0,
1------01,
9------1001,
the existing encoding method allocates one byte, i.e., eight bits, to each digit, so that "2019" is specifically, under the encoding in the prior art:
2------00000010,
0------00000000,
1------00000001,
9------00001001,
so "2019" becomes "00000010000000000000000100001001" after encoding, taking four bytes. Table 1 is the number of bytes occupied by different time string 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 applications, the longer time string occupation ratio is larger, because 12 months are provided for 1 year, and … … days are 30 days in one month, the longest "yyyy-MM-dd HH: MM: ss" is converted into binary format, and the length reaches 19 bytes, and if the data size is larger, only the time data occupies a large storage space. In practical applications, if the time data are uniformly distributed, the average storage space of each time string will exceed 11 bytes, which wastes memory.
Therefore, the embodiment of the invention provides a method for encoding, compressing and decoding variable-length time strings to solve the problem.
Fig. 3 is a flowchart illustrating a method for encoding and compressing a variable-length time string 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 to be coded according to the character length of the time string, wherein the type code value is used for indicating the character length of the time string;
s32, acquiring a corresponding timestamp according to the time string, wherein the timestamp is the time length from the reference time to the time indicated by the time string;
and S33, coding according to a preset coding length, the type coding value and the timestamp 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 type coding is based on the character length of the time string. The types of time strings are totally five types, which are "yyyy" (year), "yyyy-MM" (year-month), "yyyyyy-MM-dd" (year-month-day), "yyyyyyy-MM-dd HH" (year-month-day), "yyyyyyyyyy-MM-dd HH: MM" (year-month-day: minutes) and "yyyyyy-MM-dd HH: MM: ss" (year-month-day: minutes: seconds), respectively, and the character lengths of the five types of time strings are 4, 7, 10, 13, 16, and 19, respectively.
After the type of the time string is obtained, the type coding may be performed on the time string type, and the type coding value of each time string is unique, for example, for the type of the time string, a small-to-large type coding value, such as 1,2,3,4,5,6, may be set according to the character length of the time string, or a large-to-small type coding value, such as 6,5,4,3,2,1, may not be arranged in order of size, such as 3,6,1,5,2,4, etc., but the type coding value corresponding to each type of the time string is unique, and the type coding value corresponding to the type of the time string is stored in the server.
After the type coding value is obtained, a corresponding timestamp is acquired according to the time string, the timestamp has a metering number, a reference time is needed for calculating the timestamp, and the timestamp corresponding to the time string is a difference value between the reference time and the time string. For example, if the reference time is set to 00 minutes 00 seconds at 00 hours 01/0/01: 00 in 1970, and the time string is 1970 to 01 to 0100: 01:00, the time stamp corresponding to the time string is 1 minute, that is, 60 seconds.
And after the type code and the timestamp of the time string are obtained, the time string is coded by adopting the preset coding length, and the preset coding length can meet the access requirement of the timestamp. The encoded binary code value comprises the type code of the time string and the time stamp information, the difference value between the time string and the reference time can be known according to the time stamp information, and the type and the character length of the time string can be known according to the type code. In the embodiment of the invention, for any time data, the time string is stored by adopting the preset coding length, and the preset coding length is fixed, so that the time string information can be obtained by obtaining how many bytes for decoding when decoding.
The method for encoding a variable-length time string provided by the embodiment of the invention comprises the steps of firstly obtaining a corresponding type encoding value through the character length of a time string to be encoded, then obtaining the time length from reference time to the time string time according to the time string to obtain a time stamp, finally encoding according to the preset encoding length, the type encoding value and the time stamp to obtain a binary encoding value of the time string, converting the time string into the binary encoding value, wherein the length of the binary encoding value is the preset encoding length, and after the time strings with different character lengths are converted into the binary encoding values, the corresponding preset encoding lengths are consistent, so that when the time string information in a binary format on a storage device needs to be decoded, only bytes with the preset encoding length need to be read, and the method provided by the embodiment of the invention can accurately know the length of the binary encoding value, thereby accurately decoding the time string.
The following describes the embodiments of the present application in detail with reference to a specific example.
The embodiment of the invention provides a coding scheme aiming at the variable-length time string, and aims to solve the problems that after the variable-length time string is converted into the binary format, the occupied storage space is large, and the binary format length is not fixed, so that the matching decoding cannot be carried out.
First, for the character length of the time string, corresponding type code values are allocated to different lengths, and table 2 is a possible allocation scheme:
TABLE 2
Character length Time string type Type coding 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 type code above is only one possible code value, and the specific type code value can be set according to actual needs, and is not limited specifically here.
And after the type codes are distributed, acquiring the corresponding time stamp according to the time string. In the embodiment of the invention, the reference time is set as Greenwich time 1970, 01, 00 hours and 00 minutes and 00 seconds (Beijing time 1970, 01, 08 hours and 00 seconds), the time string also refers to the Greenwich time of the location where the time data is acquired, and both time data take the Greenwich time as a standard, so that the calculation is convenient, and the error caused by different metering standards is prevented.
In order to obtain the timestamp conveniently, the difference between the time string and greenwich time 1970, 01, 00 hours, 00 minutes and 00 seconds is obtained. 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-0100: 00:00 to the number of seconds passed by the time string, and the time string with the length less than 19 bits is filled by the following rules:
time string type: "yyyy", filled as: "yyyy-01-0100: 00: 00";
time string type: "yyyy-MM" in the same order: "yyyy-MM-0100: 00: 00";
time string type: "yyyy-MM-dd", in complement: "yyyy-MM-dd 00:00: 00: 00";
time string type: "yyyy-MM-dd HH", filled with: "yyyy-MM-dd HH:00: 00";
time string type: "yyyy-MM-dd HH: MM", complete as: "yyyy-MM-dd HH: MM: 00".
For example, if the time string "2019-03-21" needs to be encoded, first "2019-03-21" is filled in as "2019-03-2100: 00: 00". After the time string is expanded, the length of the time string is kept consistent and 19 bits, and at the moment, the time string with the character length of 19 bits is 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 as required, most programming languages are provided with tools for acquiring the time stamp through the time string, and programming personnel can write the preset conversion tool. 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 kept consistent, and the time stamp can be conveniently acquired by a preset conversion tool.
As an example, the reference time is set to 00 minutes 00 seconds at 01/00/1970, 1970-01-0100: 01, the type code value of the time string is set to 4, and the time string is extended to obtain the extended time string 1970-01-0100: 01:00, if the time stamp corresponding to the time string is 1 minute, that is, 60 seconds, because the length of the time string is less than 19 bits.
And after obtaining the type code value and the timestamp, coding the type code value and the timestamp to obtain a decimal code value, specifically, multiplying the timestamp by a preset value to obtain a multiplied timestamp, wherein the preset value is greater than 0 and is an integral multiple of 10. And then, adding the time stamp and the type code value after the multiplication processing to obtain a decimal code value. For example, the time string is encoded according to the timestamp 60 and the type code value 4, a preset value is taken first, and the preset value may be 10, 20, 30, or the like, in the embodiment of the present invention, 10 is selected as the preset value, and is multiplied with the timestamp 60 to obtain the timestamp 600 after multiplication, and then, the timestamp 600 after multiplication and the type code value 4 are added to obtain the decimal code value 604. Thus, the last bit of the coding value indicates the type of the time string, and the remaining bits carry the time information of the time string.
And after the decimal coded value is obtained, converting the decimal coded value into a binary coded value according to the preset coding length. In the embodiment of the present invention, the preset encoding length is 5 bytes, and the length of 5 bytes is selected because:
the binary code of 4 bytes can be expressed in 11111111111111111111111111111111 at maximum and is converted into decimal 4294967295, the last bit is a type coding value according to the above rule, the maximum time stamp which can be expressed by the residual bits is 429496729, the maximum time string which can be expressed by the time stamp can be expressed in 1983, and the time string after 1983 can not be expressed, which obviously does not meet the requirement; the binary code of 5 bytes can be expressed in 1111111111111111111111111111111111111111 at maximum, and is converted into 1099511627775, the last bit is the type coding value according to the above rule, the maximum time stamp which can be expressed by the residual bits is 109951162777, the time string which can be expressed by the time stamp is 5454 years, and 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 further saved, and therefore in the embodiment of the invention, the preset encoding length is 5 bytes.
After the decimal coded value 604 is obtained, it is converted to a binary coded value. The decimal number 604 is converted to a binary number 1001011100, which is converted according to a predetermined code length to obtain a binary code value 0000000000000000000000000000001001011100. Therefore, the method provided by the embodiment of the invention successfully uses five bytes, and the time string 1970-01-0100: 01 is stored after being encoded by the length of five bytes.
The method for encoding a variable-length time string provided by the embodiment of the invention comprises the steps of firstly obtaining a corresponding type encoding value through the character length of a time string to be encoded, then obtaining the time length from reference time to the time string time according to the time string to obtain a time stamp, finally encoding according to the preset encoding length, the type encoding value and the time stamp to obtain a binary encoding value of the time string, converting the time string into the binary encoding value, wherein the length of the binary encoding value is the preset encoding length, and after the time strings with different character lengths are converted into the binary encoding values, the corresponding preset encoding lengths are consistent, so that when the time string information in a binary format on a storage device needs to be decoded, only bytes with the preset encoding length need to be read, and the method provided by the embodiment of the invention can accurately know the length of the binary encoding value, thereby accurately decoding the time string. Meanwhile, the method provided by the embodiment of the invention can uniformly encode the variable-length time string into the binary code of 5 bytes for storage, and the storage space of each average time data exceeds 11 bytes 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 flowchart illustrating a method for decoding a variable-length time string according to an embodiment of the present invention, as shown in fig. 4, including:
s41, acquiring a binary code value 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 time strings with different character lengths correspond to the same preset code length;
s42, obtaining a type coding value and a time stamp of the time string according to the binary coding value, wherein the time stamp is the time length from reference time to the time indicated by the time string, and the type coding value is used for indicating the character length of the time string;
and S43, decoding according to the type coding value and the time stamp to obtain the time string.
After the time strings are coded according to the rules, the storage forms of the time strings are all binary coded values, and the preset coding lengths are all the same. The decoding process corresponds to the encoding process, and the preset encoding length, the preset numerical value and the type encoding value which are 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 need to be read backwards from the starting position, and the binary encoding value of the time string to be decoded can be acquired. The length of the binary code value is 5 bytes.
For example, in the above-described embodiment, binary coded values are stored:
0000 0000 0000 0000 0000 0000 0000 0010 0101 1100,
the length of the binary code is 5 bytes, and then a type code value and a time stamp of the time string to be decoded are 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, the binary coded value is converted into a decimal coded value, for example, the binary coded value:
0000 0000 0000 0000 0000 0000 0000 0010 0101 1100,
the conversion into a decimal encoded value is 604 and then the type encoded value of the time string and the time stamp are obtained from the decimal encoded value 604. In the embodiment of the invention, after the decimal code value is obtained, the decimal code value needs to be subjected to modulus processing according to the preset value to obtain the type code value, and then the last digit of the decimal code value is removed to obtain the timestamp. The preset value is greater than 0 and is an integral multiple of 10, the preset value is correspondingly equal in the encoding and decoding processes, and the time string can be successfully decoded only if the preset value is correspondingly equal in the encoding process.
For example, in the above encoding, the predetermined value is 10, and the corresponding decimal encoded value 604 is obtained, so that the decimal encoded value 604 is modulo-processed according to the predetermined value of 10, i.e. the decimal encoded value 604 is divided by 10, and the remainder 4, is the corresponding type encoded value. The removal of the last digit of the decimal coded value, i.e. the removal of the last digit 4 of the decimal coded value 604, translates it to 60, 60 being the corresponding timestamp.
And decoding according to the type coding value and the time stamp to obtain a time string. For example, a timestamp of 60 indicates that the time string differs from Greenwich mean time 1970 by 60 seconds, i.e., 1 minute, between 00 hours 01 month 01 day 00 hours, so the time string is 1970-01-0100: 01: 00. The character length of the time string is 16 according to the corresponding type code value 4, so that the decoded time string obtained according to 1970-01-0100: 01:00 is 1970-01-0100: 01.
The decoding method for the variable-length time string provided by the embodiment of the invention comprises the steps of firstly reading a binary coding value of the time string according to a preset coding length, then obtaining a type coding value and a time stamp of the time string according to the binary coding value, and finally decoding according to the type coding value and the time stamp to obtain the time string. When decoding the time string, the storage length is 5 bytes no matter what length the time string is, so that the time string data can be easily identified and extracted when decoding.
Fig. 5 is a schematic structural diagram of an encoding apparatus for a variable-length time string 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 to be coded according to a character length of the time string, 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 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:
coding the type coding value and the timestamp to obtain a decimal coding value;
and converting the decimal code value into the binary code value according to the preset code length.
In a possible design, the duration is a duration in the order of seconds, and the encoding module 53 is specifically configured to:
multiplying the time stamp by a preset value to obtain the time stamp after multiplication, wherein the preset value is greater than 0 and is an integral multiple of 10;
and adding the time stamp subjected to the multiplication processing and the type code value to obtain the decimal code value.
In one possible design, the second obtaining module 52 is specifically configured to:
if the character length of the time string is smaller than a preset length, performing expansion processing on the time string to obtain the time string with the character length being 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 timestamp.
Fig. 6 is a schematic structural diagram of a decoding apparatus for decoding a variable-length time string according to an embodiment of the present invention, as shown in fig. 6, including an obtaining 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 time strings with different character lengths correspond to the same preset code length;
the processing module 62 is configured to obtain a type code value and a timestamp of the time string according to the binary code value, where the timestamp is a time length 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 code 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 coding value and the time stamp of the time string according to the decimal coding value.
In one possible design, the processing module 62 is specifically configured to:
performing modulus processing on the decimal code value according to a preset value to obtain the type code value, wherein the preset value is greater than 0 and is an integral multiple of 10;
removing the last digit of the decimal encoded value to obtain the timestamp.
The apparatus provided in the embodiment of the present invention may be used to implement the technical solutions of the above method embodiments, and the implementation principles and technical effects are similar, which are not described herein again.
Fig. 7 is a schematic diagram of a hardware structure of an apparatus for encoding and decoding a variable-length time string according to an embodiment of the present invention, and as shown in fig. 7, the apparatus for encoding and decoding a variable-length time string includes: at least one processor 71 and a memory 72. 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 particular implementations, execution of computer-executable instructions stored by the memory 72 by the at least one processor 71 causes the at least one processor 71 to perform the above method of encoding a variable-length time string, or causes the at least one processor 71 to perform the above method of decoding a variable-length time string.
For a specific implementation process of the processor 71, reference may be made to the above method embodiments, which implement similar principles and technical effects, and details of this embodiment are not described herein again.
In the embodiment shown in fig. 7, it should be understood that the Processor may be a Central Processing Unit (CPU), other general-purpose processors, a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), etc. 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, or in a combination of the hardware and software modules within the processor.
The memory may comprise high speed RAM memory and may also include non-volatile storage NVM, such as at least one disk memory.
The bus may be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, an Extended ISA (EISA) bus, or the like. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, the buses in the figures of the present application are not limited to only one bus or one type of bus.
The present application further provides a computer-readable storage medium, in which computer-executable instructions are stored, and when a processor executes the computer-executable instructions, the method for encoding and decoding a variable-length time string as described above is implemented.
The computer-readable storage medium 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 or optical disk. Readable storage media can be any available media 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. Of course, the readable storage medium may also be an integral part of the processor. The processor and the readable storage medium may reside in an Application Specific Integrated Circuits (ASIC). Of course, the processor and the readable storage medium may also reside as discrete components in the apparatus.
The division of the units is only a logical division, and other divisions may be realized in practice, for example, a plurality of units or components may be combined or integrated into another system, or some features may be omitted or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, devices or units, and may be in an electrical, mechanical or other form.
The units described as separate parts may or may not be physically separate, and parts displayed 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 can be selected according to actual needs to achieve the purpose of the solution of the embodiment.
In addition, functional units in the embodiments of the present invention may be integrated into one processing unit, or each unit may exist alone physically, or two or more units are integrated into 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 such understanding, the technical solution of the present invention may be embodied in the form of a software product, which is stored in a storage medium and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute 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), a magnetic disk or an optical disk, and other various media capable of storing program codes.
Those of ordinary skill in the art will understand that: all or a portion of the steps of implementing the above-described method embodiments may be performed by hardware associated with program instructions. The program may be stored in a computer-readable storage medium. When executed, the program performs steps comprising the method embodiments described above; and the aforementioned storage medium includes: various media that can store program codes, such as ROM, RAM, magnetic or optical disks.
Finally, it should be noted that: the above embodiments are only used to illustrate the technical solution of the present invention, and not to limit the same; while the invention has been described in detail and with reference to the foregoing embodiments, it will be understood by those skilled in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some or all of the technical features may be equivalently replaced; and the modifications or the substitutions do not make the essence of the corresponding technical solutions depart from the scope of the technical solutions of the embodiments of the present invention.

Claims (12)

1. A method for encoding a variable-length time string, comprising:
acquiring a type coding value corresponding to a time string to be coded according to the character length of the time string, wherein the type coding value is used for indicating the character length of the time string;
acquiring a corresponding timestamp according to the time string, wherein the timestamp is the time length from the reference time to the time indicated by the time string;
and coding according to a preset coding length, the type coding value and the timestamp 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.
2. The method according to claim 1, wherein said encoding according to a preset encoding length, the type encoding value and the timestamp to obtain the binary encoding value of the time string comprises:
coding the type coding value and the timestamp to obtain a decimal coding value;
and converting the decimal code value into the binary code value according to the preset code length.
3. The method of claim 2, wherein the duration is in seconds, and wherein encoding the type code value and the timestamp to obtain a decimal code value comprises:
multiplying the time stamp by a preset value to obtain the time stamp after multiplication, wherein the preset value is greater than 0 and is an integral multiple of 10;
and adding the time stamp subjected to the multiplication processing and the type code value to obtain the decimal code value.
4. The method of claim 1, wherein obtaining the corresponding timestamp from the time string comprises:
if the character length of the time string is smaller than a preset length, performing expansion processing on the time string to obtain the time string with the character length being 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 timestamp.
5. The method according to any one of claims 1 to 4, wherein the predetermined code length is 5 bytes.
6. A method for decoding a variable-length time string, comprising:
acquiring a binary coding value of a time string to be decoded according to a preset coding length; the length of the binary code value is the preset code length, and time strings with different character lengths correspond to the same preset code length;
obtaining a type coding value and a timestamp of the time string according to the binary coding value, wherein the timestamp is the time length from the reference time to the time indicated by the time string, and the type coding value is used for indicating the character length of the time string;
and decoding according to the type coding value and the timestamp to obtain the time string.
7. The method of claim 6, wherein obtaining the type-encoded value and the timestamp of the time string according to the binary-encoded value comprises:
converting the binary coded value to a decimal coded value;
and obtaining the type coding value and the time stamp of the time string according to the decimal coding value.
8. The method of claim 7, wherein obtaining the type-coded value and the timestamp of the time string from the decimal-coded value comprises:
performing modulus processing on the decimal code value according to a preset value to obtain the type code value, wherein the preset value is greater than 0 and is an integral multiple of 10;
removing the last digit of the decimal encoded value to obtain the timestamp.
9. An apparatus for encoding a variable-length time string, comprising:
the device comprises a first obtaining module, a second obtaining module and a third obtaining module, wherein the first obtaining module is used for obtaining a type code value corresponding to a time string to be coded according to the character length of the time string, and the type code value is used for indicating the character length of the time string;
the second acquisition module is used for acquiring a corresponding timestamp according to the time string, wherein the timestamp is the time length from the reference time to the time indicated by the time string;
and the coding module is used for coding according to a preset coding length, the type coding value and the timestamp 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.
10. An apparatus for decoding a variable-length time string, comprising:
the acquisition module is used for 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 time strings with different character lengths correspond to the same preset code length;
the processing module is used for obtaining a type coding value and a timestamp of the time string according to the binary coding value, the timestamp is the time length from reference time to the time indicated by the time string, and the type coding 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 timestamp to obtain the time string.
11. 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 the computer-executable instructions stored by the memory causes the at least one processor to perform the method of encoding a variable-length-time string according to any one of claims 1 to 5 or causes the at least one processor to perform the method of decoding a variable-length-time string according to any one of claims 6 to 8.
12. A computer-readable storage medium having stored thereon computer-executable instructions which, when executed by a processor, implement a method of encoding a variable-length time string according to any one of claims 1 to 5 or a method of decoding a variable-length time string according to any one of claims 6 to 8.
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 true CN112152631A (en) 2020-12-29
CN112152631B 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)

Cited By (2)

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

Citations (9)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20040119617A1 (en) * 2002-12-19 2004-06-24 International Business Machines Corporation Efficient representation of date/time information
US7865525B1 (en) * 2007-08-02 2011-01-04 Amazon Technologies, Inc. High efficiency binary encoding
US20140222418A1 (en) * 2012-04-30 2014-08-07 Martin Richtarsky Fixed string dictionary
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

Patent Citations (9)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20040119617A1 (en) * 2002-12-19 2004-06-24 International Business Machines Corporation Efficient representation of date/time information
US7865525B1 (en) * 2007-08-02 2011-01-04 Amazon Technologies, Inc. High efficiency binary encoding
US20140222418A1 (en) * 2012-04-30 2014-08-07 Martin Richtarsky Fixed string dictionary
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网络数据库指南》,", 机械工业出版社, pages: 78 - 82 *

Cited By (3)

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

Also Published As

Publication number Publication date
CN112152631B (en) 2023-08-15

Similar Documents

Publication Publication Date Title
CN105684316B (en) Polar code encoding method and device
US3656178A (en) Data compression and decompression system
CN112152631B (en) Method and device for encoding and decoding variable-length time string
CN104579360B (en) A kind of method and apparatus of data processing
CN113132416B (en) Data packet detection method and device
CN112559592A (en) Real-time data processing method, device and equipment
CN110545106A (en) Method and device for coding time series data
CN114640354A (en) Data compression method and device, electronic equipment and computer readable storage medium
CN110874346A (en) Compression scheme for floating point values
CN113177045A (en) Data extraction method and device, computable storage equipment and data request equipment
CN109831544B (en) Code storage method and system applied to email address
CN112232025B (en) Character string storage method and device and electronic equipment
CN111198857A (en) Data compression method and system based on full flash memory array
CN111552689B (en) Method, device and equipment for calculating deduplication index of fund audit
CN112395179B (en) Model training method, disk prediction method, device and electronic equipment
CN110287147B (en) Character string sorting method and device
CN111384974B (en) Confidence quantization method, device and decoder for multi-system LDPC code
CN111949604A (en) File storage method and device, electronic equipment and readable storage medium
CN111147082A (en) Security transaction data compression method and device
CN111597154A (en) Structured transaction data compression method, related method and related device
CN114328283B (en) Counting address acquisition method, counting method, device, equipment and storage medium
CN115934730B (en) Data processing method and device, medium and computer equipment
CN111669616B (en) Encoding and decoding method and device and computer storage medium
CN114946144B (en) Low density parity check code encoding method and encoder
US11928134B1 (en) Medoid-based data compression

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