Skip to content

Commit 1e8327f

Browse files
Update doc and apply default timezone in relative time parser
1 parent 31ae8b1 commit 1e8327f

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

README.md

+41-6
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,17 @@ dt, _ = dps.Parse(cfg, "miy 02-03-2016") // philippines, keep using MDY date ord
240240
By default, **dateparser** uses the timezone that is present in the date string. If the timezone is not present, then it will use timezone from the `CurrentTime`:
241241

242242
```go
243-
// Current time is 2015-06-01 12:00:00 WIB
244-
wib := time.FixedZone("WIB", 7*60*60)
243+
// Use case: I'm from Jakarta, parsing London's news article.
244+
// So, my current time use GMT+7, however the parser's default timezone is GMT.
245+
london, _ := time.LoadLocation("Europe/London")
246+
jakarta, _ := time.LoadLocation("Asia/Jakarta")
247+
245248
cfg := &dps.Configuration{
246-
CurrentTime: time.Date(2015, 6, 1, 12, 0, 0, 0, wib),
249+
DefaultTimezone: london,
250+
CurrentTime: time.Date(2015, 6, 1, 12, 0, 0, 0, jakarta),
247251
}
248252

253+
// Here the timezone is explicitly mentioned in string
249254
dt, _ = dps.Parse(cfg, "January 12, 2012 10:00 PM EST")
250255
// time: 2012-01-12 22:00:00 EST, confidence: Day
251256

@@ -258,10 +263,21 @@ dt, _ = dps.Parse(cfg, "2 hours ago EST")
258263
dt, _ = dps.Parse(cfg, "2 hours ago -0500")
259264
// time: 2015-05-31 22:00:00 UTC-05:00, confidence: Day
260265

261-
dt, _ = dps.Parse(cfg, "January 12, 2012 10:00 PM") // use tz from current time
266+
// Now timezone is not specified, so parser will use `DefaultTimezone`
267+
dt, _ = dps.Parse(cfg, "January 12, 2012 10:00 PM")
268+
// time: 2012-01-12 22:00:00 GMT, confidence: Day
269+
270+
dt, _ = dps.Parse(cfg, "2 hours ago")
271+
// time: 2015-06-01 04:00:00 BST, confidence: Day
272+
// notice the timezone become BST, because we move back to Daylight Saving Time.
273+
274+
// Now we remove the default timezone, so it use timezone from `CurrentTime`
275+
cfg.DefaultTimezone = nil
276+
277+
dt, _ = dps.Parse(cfg, "January 12, 2012 10:00 PM")
262278
// time: 2012-01-12 22:00:00 WIB, confidence: Day
263279

264-
dt, _ = dps.Parse(cfg, "2 hours ago") // use tz from current time
280+
dt, _ = dps.Parse(cfg, "2 hours ago")
265281
// time: 2015-06-01 10:00:00 WIB, confidence: Day
266282
```
267283

@@ -291,7 +307,7 @@ dt, _ = dps.Parse(cfg, "Sunday")
291307
// time: 2015-07-26 00:00:00 UTC (the closest Sunday from current time)
292308
```
293309

294-
You can change the behavior by using `PreferredDayOfMonth` and `PreferredDateSource` in `Configuration`:
310+
You can change the behavior by using `PreferredMonthOfYear`, `PreferredDayOfMonth` and `PreferredDateSource` in `Configuration`:
295311

296312
```go
297313
// Current time is 2015-07-10 12:00:00 UTC
@@ -312,6 +328,25 @@ dt, _ = dps.Parse(cfg, "December 2015")
312328
```go
313329
// Current time is 2015-10-10 12:00:00 UTC
314330

331+
cfg.PreferredDayOfMonth = dps.Current
332+
cfg.PreferredMonthOfYear = dps.CurrentMonth
333+
dt, _ = dps.Parse(cfg, "2020")
334+
// time: 2020-10-10 00:00:00 UTC
335+
336+
cfg.PreferredDayOfMonth = dps.Last
337+
cfg.PreferredMonthOfYear = dps.FirstMonth
338+
dt, _ = dps.Parse(cfg, "2020")
339+
// time: 2020-01-31 00:00:00 UTC
340+
341+
cfg.PreferredDayOfMonth = dps.First
342+
cfg.PreferredMonthOfYear = dps.LastMonth
343+
dt, _ = dps.Parse(cfg, "2015")
344+
// time: 2015-12-01 00:00:00 UTC
345+
```
346+
347+
```go
348+
// Current time is 2015-10-10 12:00:00 UTC
349+
315350
cfg.PreferredDateSource = dps.CurrentPeriod
316351
dt, _ = dps.Parse(cfg, "March")
317352
// time: 2015-03-10 00:00:00 UTC

internal/parser/relative/parser.go

+3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ func Parse(cfg *setting.Configuration, str string) date.Date {
3939
now = cfg.CurrentTime
4040
}
4141

42+
// Apply timezone
4243
if !tzData.IsZero() {
4344
loc := time.FixedZone(tzData.Name, tzData.Offset)
4445
now = now.In(loc)
46+
} else if cfg != nil && cfg.DefaultTimezone != nil {
47+
now = now.In(cfg.DefaultTimezone)
4548
}
4649

4750
// Get relative date

0 commit comments

Comments
 (0)