Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
model-entity
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
rust common
model-entity
Commits
4c9dbaf2
Commit
4c9dbaf2
authored
Aug 28, 2024
by
xuwencheng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
time
parent
606f9b71
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
98 additions
and
5 deletions
+98
-5
Cargo.toml
Cargo.toml
+9
-0
lib.rs
src/lib.rs
+5
-3
mod.rs
src/login/mod.rs
+12
-2
id.rs
src/tool/id.rs
+0
-0
mod.rs
src/tool/mod.rs
+3
-0
time.rs
src/tool/time.rs
+69
-0
No files found.
Cargo.toml
View file @
4c9dbaf2
...
...
@@ -9,6 +9,7 @@ socket = ["rmp", "rmp-serde", "bytes", "flate2"]
paidui
=
[]
login
=
[]
client
=
["socket"]
time_tool
=
[
"time"
,
"compact_str"
]
[dependencies]
anyhow
=
{
version
=
"1"
,
features
=
["std"]
,
default-features
=
false
}
...
...
@@ -27,3 +28,11 @@ rmp = { version = "0.8", default-features = false, optional = true }
rmp-serde
=
{
version
=
"1"
,
default-features
=
false
,
optional
=
true
}
flate2
=
{
version
=
"1"
,
features
=
["rust_backend"]
,
default-features
=
false
,
optional
=
true
}
compact_str
=
{
version
=
"0.7"
,
features
=
[
"serde"
,
],
default-features
=
false
,
optional
=
true
}
time
=
{
version
=
"0.3"
,
features
=
[
"formatting"
,
"macros"
,
],
default-features
=
false
,
optional
=
true
}
src/lib.rs
View file @
4c9dbaf2
pub
use
tool
::
id
;
#[cfg(feature
=
"client"
)]
pub
mod
client
;
pub
mod
event
;
pub
mod
id
;
#[cfg(feature
=
"login"
)]
pub
mod
login
;
#[cfg(feature
=
"paidui"
)]
...
...
@@ -7,8 +10,7 @@ pub mod paidui;
pub
mod
serde
;
#[cfg(feature
=
"socket"
)]
pub
mod
socket
;
#[cfg(feature
=
"client"
)]
pub
mod
client
;
pub
mod
tool
;
#[cfg(test)]
mod
tests
{
...
...
src/login/mod.rs
View file @
4c9dbaf2
...
...
@@ -16,7 +16,7 @@ pub struct LoginInfo {
impl
LoginInfo
{
pub
fn
is_dinner
(
&
self
)
->
bool
{
self
.brand_type
==
70
self
.brand_type
.is_dinner
()
}
}
...
...
@@ -34,7 +34,7 @@ pub struct StoreInfo {
impl
StoreInfo
{
pub
fn
is_dinner
(
&
self
)
->
bool
{
self
.brand_type
==
70
self
.brand_type
.is_dinner
()
}
pub
fn
login_info
(
&
self
,
token
:
impl
AsRef
<
str
>
)
->
LoginInfo
{
...
...
@@ -48,3 +48,13 @@ impl StoreInfo {
}
}
}
pub
trait
BrandType
{
fn
is_dinner
(
&
self
)
->
bool
;
}
impl
BrandType
for
i32
{
fn
is_dinner
(
&
self
)
->
bool
{
70
.eq
(
self
)
}
}
\ No newline at end of file
src/id.rs
→
src/
tool/
id.rs
View file @
4c9dbaf2
File moved
src/tool/mod.rs
0 → 100644
View file @
4c9dbaf2
pub
mod
id
;
#[cfg(feature
=
"time_tool"
)]
pub
mod
time
;
src/tool/time.rs
0 → 100644
View file @
4c9dbaf2
use
anyhow
::
Result
;
use
compact_str
::
CompactString
;
use
time
::{
error
::
ComponentRange
,
format_description
::
FormatItem
,
macros
::
format_description
,
Duration
,
OffsetDateTime
,
UtcOffset
,
};
const
OFFSET
:
Result
<
UtcOffset
,
ComponentRange
>
=
UtcOffset
::
from_hms
(
8
,
0
,
0
);
pub
fn
datetime
()
->
Result
<
CompactString
>
{
#[rustfmt
::
skip]
const
FORMAT
:
&
[
FormatItem
<
'static
>
]
=
format_description!
(
"[year]-[month]-[day] [hour]:[minute]:[second]:[subsecond]"
);
Ok
(
OffsetDateTime
::
now_utc
()
.to_offset
(
OFFSET
?
)
.format
(
FORMAT
)
.map
(
CompactString
::
from
)
?
)
}
pub
fn
date
()
->
Result
<
CompactString
>
{
const
FORMAT
:
&
[
FormatItem
<
'static
>
]
=
format_description!
(
"[year]-[month]-[day]"
);
Ok
(
OffsetDateTime
::
now_utc
()
.to_offset
(
OFFSET
?
)
.format
(
FORMAT
)
.map
(
CompactString
::
from
)
?
)
}
pub
fn
date_by_offset
(
days
:
i64
)
->
Result
<
CompactString
>
{
const
FORMAT
:
&
[
FormatItem
<
'static
>
]
=
format_description!
(
"[year]-[month]-[day]"
);
Ok
(
OffsetDateTime
::
now_utc
()
.to_offset
(
OFFSET
?
)
.checked_add
(
Duration
::
days
(
days
))
.ok_or_else
(||
anyhow
::
anyhow!
(
"Date offset failed"
))
?
.format
(
FORMAT
)
.map
(
CompactString
::
from
)
?
)
}
// 计算指定天数前的毫秒时间戳
pub
fn
date_millisecond
(
days
:
i64
)
->
Result
<
u64
>
{
milliseconds
(
Duration
::
days
(
days
))
}
// 计算指定小时前的毫秒时间戳
pub
fn
hour_millisecond
(
hours
:
i64
)
->
Result
<
u64
>
{
milliseconds
(
Duration
::
hours
(
hours
))
}
pub
fn
milliseconds
(
duration
:
Duration
)
->
Result
<
u64
>
{
let
offset_date_time
=
OffsetDateTime
::
now_utc
()
.to_offset
(
OFFSET
?
)
.checked_add
(
duration
)
.ok_or_else
(||
anyhow
::
anyhow!
(
"Date offset failed"
))
?
;
let
millis
=
offset_date_time
.unix_timestamp
()
*
1000
+
offset_date_time
.millisecond
()
as
i64
;
Ok
(
millis
as
u64
)
}
pub
fn
slice_to_array_4
<
T
>
(
slice
:
&
[
T
])
->
Result
<&
[
T
;
4
]
>
{
if
slice
.len
()
==
4
{
let
ptr
=
slice
.as_ptr
()
as
*
const
[
T
;
4
];
unsafe
{
Ok
(
&*
ptr
)
}
}
else
{
Err
(
anyhow
::
anyhow!
(
"Data length is not consistent with the expected length"
))
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment