Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
1
1weather
Overview
Overview
Details
Activity
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dmitriy Stepanets
1weather
Commits
f2156b33
Commit
f2156b33
authored
Apr 23, 2021
by
Demid Merzlyakov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adjusted Alert Extended Info date parsing algorithm.
parent
e1ae935a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
13 deletions
+19
-13
1Weather/Network/Notifications/NWSAlertInfoParser.swift
+19
-13
No files found.
1Weather/Network/Notifications/NWSAlertInfoParser.swift
View file @
f2156b33
...
...
@@ -57,7 +57,7 @@ class NWSAlertInfoParser {
return
dateFormatter
}()
private
func
parse
(
dateString
:
String
,
shouldBe
InThePast
:
Bool
)
->
Date
?
{
private
func
parse
(
dateString
:
String
,
shouldBe
Before
beforeDate
:
Date
?,
after
afterDate
:
Date
?
)
->
Date
?
{
// There's no year here, so we've got to guess.
let
currentDate
=
Date
()
let
calendar
=
Calendar
(
identifier
:
.
gregorian
)
...
...
@@ -65,20 +65,26 @@ class NWSAlertInfoParser {
return
nil
}
let
dateStringWithCurrentYear
=
"
\(
currentYear
)
\(
dateString
)
"
if
shouldBeInThePast
{
guard
let
dateWithCurrentYear
=
NWSAlertInfoParser
.
dateFormatter
.
date
(
from
:
dateStringWithCurrentYear
)
else
{
return
nil
}
if
dateWithCurrentYear
>
current
Date
{
let
dateStringWithPreviousYear
=
"
\(
currentYear
-
1
)
\(
dateString
)
"
return
NWSAlertInfoParser
.
dateFormatter
.
date
(
from
:
dateStringWithPreviousYear
)
var
yearAdjustment
:
Int
?
=
nil
guard
let
dateWithCurrentYear
=
NWSAlertInfoParser
.
dateFormatter
.
date
(
from
:
dateStringWithCurrentYear
)
else
{
return
nil
}
if
let
beforeDate
=
before
Date
{
if
beforeDate
<
dateWithCurrentYear
{
yearAdjustment
=
-
1
}
else
{
return
dateWithCurrentYear
}
if
let
afterDate
=
afterDate
{
if
afterDate
>
dateWithCurrentYear
{
yearAdjustment
=
1
}
}
if
let
yearAdjustment
=
yearAdjustment
{
let
adjustedDateString
=
"
\(
currentYear
+
yearAdjustment
)
\(
dateString
)
"
return
NWSAlertInfoParser
.
dateFormatter
.
date
(
from
:
adjustedDateString
)
}
else
{
return
NWSAlertInfoParser
.
dateFormatter
.
date
(
from
:
dateStringWithCurrentYear
)
return
dateWithCurrentYear
}
}
...
...
@@ -106,11 +112,11 @@ class NWSAlertInfoParser {
}
result
.
title
=
(
firstLine
as
NSString
)
.
substring
(
with
:
firstMatch
.
range
(
withName
:
"title"
))
let
issuedDateString
=
(
firstLine
as
NSString
)
.
substring
(
with
:
firstMatch
.
range
(
withName
:
"issuedDate"
))
result
.
issueDate
=
parse
(
dateString
:
issuedDateString
,
shouldBe
InThePast
:
true
)
result
.
issueDate
=
parse
(
dateString
:
issuedDateString
,
shouldBe
Before
:
Date
(),
after
:
nil
)
let
expiryDateRange
=
firstMatch
.
range
(
withName
:
"expiryDate"
)
if
expiryDateRange
.
location
!=
NSNotFound
{
let
expiryDateString
=
(
firstLine
as
NSString
)
.
substring
(
with
:
expiryDateRange
)
result
.
expiryDate
=
parse
(
dateString
:
expiryDateString
,
shouldBe
InThePast
:
fals
e
)
result
.
expiryDate
=
parse
(
dateString
:
expiryDateString
,
shouldBe
Before
:
nil
,
after
:
result
.
issueDat
e
)
}
result
.
issuer
=
(
firstLine
as
NSString
)
.
substring
(
with
:
firstMatch
.
range
(
withName
:
"issuer"
))
return
result
...
...
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