Repository: yt-dlp/yt-dlp

Issue:
[extractor/radiko] Use timezone +09:00 (JST) to extract correct info for on-air radio programs
**IMPORTANT**: PRs without the template will be CLOSED

### Description of your *pull request* and other information

<!--

Explanation of your *pull request* in arbitrary form goes here. Please **make sure the description explains the purpose and effect** of your *pull request* and is worded well enough to be understood. Provide as much **context and examples** as possible

-->

Hello!

### What's wrong

I got wrong titles for on-air radio programs.

Now is "Thu Jan 19 23:22:37 JST 2023" and the [JORF station](https://radiko.jp/#!/live/JORF) is playing "_Ｌｉｓｔｅｎ　ｔｏ　ｔｈｅ　Ｍｕｓｉｃ_". However, yt-dlp tells me the program's name is "_加藤裕介の横浜ポップＪ(3)_".

### Why it occurs

The XML file "https://radiko.jp/v3/program/station/weekly/JORF.xml" has the following content:

```XML
<radiko>
  <ttl>1800</ttl>
  <srvtime>1674137355</srvtime>
  <stations>
    <station id="JORF">
    <name>ラジオ日本</name>
      <progs>
        <date>20230119</date>
          <prog id="9723911071" master_id="" ft="20230119140000" to="20230119145400" ftl="1400" tol="1454" dur="3240">
            <title>加藤裕介の横浜ポップＪ(3)</title>
            <...>
          </prog>
          <...>
          <prog id="9723938861" master_id="" ft="20230119230000" to="20230119233000" ftl="2300" tol="2330" dur="1800">
            <title>Ｌｉｓｔｅｎ　ｔｏ　ｔｈｅ　Ｍｕｓｉｃ</title>
```

Now the "music" program is still playing. Let's see what time it is now:

```console
$ date '+%s'
1674138288
$ TZ='Asia/Tokyo' date -r '1674138288'
Thu Jan 19 23:24:48 JST 2023
```

As you can see, the "```ft```" and "```to```" fields are datetime strings with +09:00 offset (JST, Japan Standard Time).

### Why also changed "RadikoIE"

The link for "RadikoIE" contains a datetime string (the "video_id"). It's also in JST timezone. To make the code look consistent and work correctly, I've changed all UNIX timestamp strings produced by "```unified_timestamp()```" from JST to UTC.

<details open><summary>Template</summary> <!-- OPEN is intentional -->

<!--

# PLEASE FOLLOW THE GUIDE BELOW

- You will be asked some questions, please read them **carefully** and answer honestly
- Put an `x` into all the boxes `[ ]` relevant to your *pull request* (like [x])
- Use *Preview* tab to see how your *pull request* will actually look like

-->

### Before submitting a *pull request* make sure you have:
- [x] At least skimmed through [contributing guidelines](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#developer-instructions) including [yt-dlp coding conventions](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#yt-dlp-coding-conventions)
- [x] [Searched](https://github.com/yt-dlp/yt-dlp/search?q=is%3Apr&type=Issues) the bugtracker for similar pull requests
- [x] Checked the code with [flake8](https://pypi.python.org/pypi/flake8) and [ran relevant tests](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#developer-instructions)

### In order to be accepted and merged into yt-dlp each piece of code must be in public domain or released under [Unlicense](http://unlicense.org/). Check one of the following options:
- [x] I am the original author of this code and I am willing to release it under [Unlicense](http://unlicense.org/)
- [ ] I am not the original author of this code but it is in public domain or released under [Unlicense](http://unlicense.org/) (provide reliable evidence)

### What is the purpose of your *pull request*?
- [x] Fix or improvement to an extractor (Make sure to add/update tests)
- [ ] New extractor ([Piracy websites will not be accepted](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#is-the-website-primarily-used-for-piracy))
- [ ] Core bug fix/improvement
- [ ] New feature (It is strongly [recommended to open an issue first](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#adding-new-feature-or-making-overarching-changes))

</details>



Discussion:
```
$ ./b -O title 'https://radiko.jp/#!/live/TBS'
WARNING: [RadikoRadio] Downloader will not stop at the end of the program! Press Ctrl+C to stop
荻上チキ・Session 2023-01-20 00:42
```
~~Damn, I thought offset is correctly considered but it's not! (the expected title is `ハライチのターン！`)~~

The major cause of this bug is that, `time_seconds(hours=9)` is returning the time without considering given timezone, while the parameters and RadikoRadioIE expect that.

https://github.com/yt-dlp/yt-dlp/blob/176a068cde4f2d9dfa0336168caead0b1edcb8ac/yt_dlp/extractor/radiko.py#L202

So the fix for it should go to `time_seconds` in utils.py, rather than offsetting all the time-related strings in radiko.py: (the epoch can be constant)

```diff
$ git diff
diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py
index ab3e56b00..1f5cc17d4 100644
--- a/yt_dlp/utils.py
+++ b/yt_dlp/utils.py
@@ -5694,8 +5694,12 @@ def time_millis():
 
 
 def time_seconds(**kwargs):
-    t = datetime.datetime.now(datetime.timezone(datetime.timedelta(**kwargs)))
-    return t.timestamp()
+    """
+    Returns TZ-aware time in seconds since the epoch (1970-01-01T00:00:00Z)
+    """
+    t = datetime.datetime.utcnow()
+    epoch = datetime.datetime(1970, 1, 1)
+    return (t - epoch + datetime.timedelta(**kwargs)).total_seconds()
 
 
 # create a JSON Web Signature (jws) with HS256 algorithm
```

Thoughts?

Converted to draft to avoid accidentally merge.

Resolving patch:
diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py
--- a/yt_dlp/utils.py
+++ b/yt_dlp/utils.py
@@ -5585,8 +5585,10 @@ def get_first(obj, keys, **kwargs):
 
 
 def time_seconds(**kwargs):
-    t = datetime.datetime.now(datetime.timezone(datetime.timedelta(**kwargs)))
-    return t.timestamp()
+    """
+    Returns TZ-aware time in seconds since the epoch (1970-01-01T00:00:00Z)
+    """
+    return time.time() + datetime.timedelta(**kwargs).total_seconds()
 
 
 # create a JSON Web Signature (jws) with HS256 algorithm


Test patch: