@aiursoft/utctime.js 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.cjs +32 -0
- package/.gitlab-ci.yml +36 -0
- package/.prettierrc +21 -0
- package/README.md +41 -0
- package/dist/esm/utctime.js +87 -0
- package/dist/esm/utctime.js.map +1 -0
- package/dist/types/utctime.d.ts +18 -0
- package/package.json +33 -0
- package/src/utctime.ts +104 -0
- package/tsconfig.json +13 -0
- package/tsconfig.types.json +8 -0
package/.eslintrc.cjs
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
root: true,
|
|
3
|
+
parser: '@typescript-eslint/parser',
|
|
4
|
+
plugins: ['@typescript-eslint'],
|
|
5
|
+
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
|
|
6
|
+
|
|
7
|
+
rules: {
|
|
8
|
+
// allow while(true) loops
|
|
9
|
+
'no-constant-condition': ['error', { checkLoops: false }],
|
|
10
|
+
'@typescript-eslint/naming-convention': [
|
|
11
|
+
'error',
|
|
12
|
+
{
|
|
13
|
+
selector: ['default'],
|
|
14
|
+
format: ['strictCamelCase'],
|
|
15
|
+
leadingUnderscore: 'allow',
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
selector: ['variable'],
|
|
19
|
+
format: ['strictCamelCase', 'UPPER_CASE'],
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
selector: ['objectLiteralProperty'],
|
|
23
|
+
format: ['strictCamelCase', 'UPPER_CASE'],
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
selector: ['typeLike'],
|
|
27
|
+
format: ['PascalCase'],
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
'multiline-comment-style': ['error', 'starred-block'],
|
|
31
|
+
},
|
|
32
|
+
}
|
package/.gitlab-ci.yml
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
stages:
|
|
2
|
+
- check
|
|
3
|
+
- publish
|
|
4
|
+
|
|
5
|
+
before_script:
|
|
6
|
+
- 'cat /etc/lsb-release && uname -a && pwd && ls -ashl'
|
|
7
|
+
- 'echo "Hostname: $(hostname)"'
|
|
8
|
+
- 'echo "Uptime: $(uptime -p)"'
|
|
9
|
+
- 'echo "Memory: $(free -h | awk "/^Mem:/{print \$2}")"'
|
|
10
|
+
- 'echo "CPU Cores: $(nproc)"'
|
|
11
|
+
- 'echo "Available Disk Space: $(df -h --output=avail / | tail -1)"'
|
|
12
|
+
- 'echo "IP Address: $(hostname -I)"'
|
|
13
|
+
- 'npm --version'
|
|
14
|
+
- 'node --version'
|
|
15
|
+
|
|
16
|
+
build_locally:
|
|
17
|
+
stage: check
|
|
18
|
+
script:
|
|
19
|
+
- npm clean-install
|
|
20
|
+
- npm run lint
|
|
21
|
+
- npm run build
|
|
22
|
+
|
|
23
|
+
deploy_to_npm:
|
|
24
|
+
needs:
|
|
25
|
+
- build_locally
|
|
26
|
+
stage: publish
|
|
27
|
+
environment: production
|
|
28
|
+
script:
|
|
29
|
+
- npm clean-install
|
|
30
|
+
- npm run clean
|
|
31
|
+
- npm run format
|
|
32
|
+
- npm run build
|
|
33
|
+
- echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >> .npmrc
|
|
34
|
+
- npm publish --access public || true
|
|
35
|
+
only:
|
|
36
|
+
- master
|
package/.prettierrc
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"semi": false,
|
|
3
|
+
"singleQuote": true,
|
|
4
|
+
"overrides": [
|
|
5
|
+
{
|
|
6
|
+
"files": "**/*.md",
|
|
7
|
+
"options": {
|
|
8
|
+
"parser": "markdown",
|
|
9
|
+
"proseWrap": "never",
|
|
10
|
+
"printWidth": 120
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"files": "**/*.json",
|
|
15
|
+
"options": {
|
|
16
|
+
"parser": "json",
|
|
17
|
+
"tabWidth": 2
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
]
|
|
21
|
+
}
|
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# UtcTime.js
|
|
2
|
+
|
|
3
|
+
[](https://gitlab.aiursoft.cn/aiursoft/utctime.js/-/blob/master/LICENSE) [](https://gitlab.aiursoft.cn/aiursoft/utctime.js/-/pipelines) [](https://gitlab.aiursoft.cn/aiursoft/utctime.js/-/commits/master?ref_type=heads) [](https://www.npmjs.com/package/@aiursoft/utctime.js) [](https://www.npmjs.com/package/@aiursoft/utctime.js)
|
|
4
|
+
|
|
5
|
+
`@aiursoft/utctime.js` is a JavaScript library which converts UTC time to local time easily.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Run the following command to install the most recent version of `@aiursoft/utctime.js` from npm:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install @aiursoft/utctime.js
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Importing
|
|
16
|
+
|
|
17
|
+
## Reference JavaScript
|
|
18
|
+
|
|
19
|
+
You also need to reference bootstrap's JavaScript file:
|
|
20
|
+
|
|
21
|
+
```html
|
|
22
|
+
<script type="module">
|
|
23
|
+
import UtcTime from './node_modules/@aiursoft/utctime.js/dist/esm/utctime.js'
|
|
24
|
+
new UtcTime({})
|
|
25
|
+
</script>
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## How to build locally
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install
|
|
32
|
+
npm run build
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## How to contribute
|
|
36
|
+
|
|
37
|
+
There are many ways to contribute to the project: logging bugs, submitting pull requests, reporting issues, and creating suggestions.
|
|
38
|
+
|
|
39
|
+
Even if you with push rights on the repository, you should create a personal fork and create feature branches there when you need them. This keeps the main repository clean and your workflow cruft out of sight.
|
|
40
|
+
|
|
41
|
+
We're also interested in your feedback on the future of this project. You can submit a suggestion or feature request through the issue tracker. To make this process more effective, we're asking that these include more information to help define them more clearly.
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
class UtcTime {
|
|
2
|
+
defaultSettings = {
|
|
3
|
+
attr: 'data-utc-time',
|
|
4
|
+
daysAgo: ' days ago',
|
|
5
|
+
hoursAgo: ' hours ago',
|
|
6
|
+
minutesAgo: ' minutes ago',
|
|
7
|
+
secondsAgo: ' seconds ago',
|
|
8
|
+
disableAgo: false,
|
|
9
|
+
onSet: function (obj) { },
|
|
10
|
+
disableAutoUpdate: false,
|
|
11
|
+
};
|
|
12
|
+
constructor(settings) {
|
|
13
|
+
Object.assign(this.defaultSettings, settings);
|
|
14
|
+
const loop = () => {
|
|
15
|
+
this.initTime(this.defaultSettings);
|
|
16
|
+
if (!this.defaultSettings.disableAutoUpdate) {
|
|
17
|
+
setTimeout(loop, 1000);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
loop();
|
|
21
|
+
}
|
|
22
|
+
timeSince(date, settings) {
|
|
23
|
+
const seconds = Math.floor((new Date().getTime() - date.getTime()) / 1000);
|
|
24
|
+
let interval = Math.floor(seconds / 2592000);
|
|
25
|
+
if (interval >= 1) {
|
|
26
|
+
return date.toLocaleDateString();
|
|
27
|
+
}
|
|
28
|
+
interval = Math.floor(seconds / 86400);
|
|
29
|
+
if (interval >= 1) {
|
|
30
|
+
return interval + settings.daysAgo;
|
|
31
|
+
}
|
|
32
|
+
interval = Math.floor(seconds / 3600);
|
|
33
|
+
if (interval >= 1) {
|
|
34
|
+
return interval + settings.hoursAgo;
|
|
35
|
+
}
|
|
36
|
+
interval = Math.floor(seconds / 60);
|
|
37
|
+
if (interval >= 1) {
|
|
38
|
+
return interval + settings.minutesAgo;
|
|
39
|
+
}
|
|
40
|
+
if (interval >= 0) {
|
|
41
|
+
return Math.floor(seconds) + settings.secondsAgo;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
return date.toLocaleDateString();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
getDate(inputString) {
|
|
48
|
+
if (inputString.endsWith(' UTC') || inputString.endsWith('Z')) {
|
|
49
|
+
return new Date(inputString);
|
|
50
|
+
}
|
|
51
|
+
if (!inputString.endsWith(' UTC')) {
|
|
52
|
+
const timeValue = inputString + ' UTC';
|
|
53
|
+
const date = new Date(timeValue);
|
|
54
|
+
if (!isNaN(date.getTime())) {
|
|
55
|
+
return date;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (!inputString.endsWith('Z')) {
|
|
59
|
+
const timeValue = inputString + 'Z';
|
|
60
|
+
const date = new Date(timeValue);
|
|
61
|
+
if (!isNaN(date.getTime())) {
|
|
62
|
+
return date;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
console.warn(`jquery-utc-time: Input string ${inputString} can not be converted to UTC time and try to parse as typical date string.`);
|
|
66
|
+
return new Date(inputString);
|
|
67
|
+
}
|
|
68
|
+
initTime(settings) {
|
|
69
|
+
document
|
|
70
|
+
.querySelectorAll('*[' + settings.attr + ']')
|
|
71
|
+
.forEach((timeField) => {
|
|
72
|
+
const sourceValue = timeField.getAttribute(settings.attr);
|
|
73
|
+
const date = this.getDate(sourceValue);
|
|
74
|
+
let text = this.timeSince(date, settings);
|
|
75
|
+
if (settings.disableAgo) {
|
|
76
|
+
text = date.toLocaleDateString();
|
|
77
|
+
}
|
|
78
|
+
timeField.innerHTML = text;
|
|
79
|
+
timeField.setAttribute('data-toggle', 'tooltip');
|
|
80
|
+
timeField.setAttribute('data-trigger', 'hover');
|
|
81
|
+
timeField.setAttribute('data-title', date.toLocaleString());
|
|
82
|
+
settings.onSet(timeField);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
export default UtcTime;
|
|
87
|
+
//# sourceMappingURL=utctime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utctime.js","sourceRoot":"","sources":["../../src/utctime.ts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boolean;
|
|
8
|
+
onSet: (obj: Element) => void;
|
|
9
|
+
disableAutoUpdate: boolean;
|
|
10
|
+
}
|
|
11
|
+
declare class UtcTime {
|
|
12
|
+
defaultSettings: UtcTimeSettings;
|
|
13
|
+
constructor(settings: UtcTimeSettings);
|
|
14
|
+
timeSince(date: Date, settings: typeof this.defaultSettings): string;
|
|
15
|
+
getDate(inputString: string): Date;
|
|
16
|
+
initTime(settings: typeof this.defaultSettings): void;
|
|
17
|
+
}
|
|
18
|
+
export default UtcTime;
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aiursoft/utctime.js",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Auto utc time",
|
|
5
|
+
"main": "dist/cjs/utctime.js",
|
|
6
|
+
"module": "dist/esm/utctime.js",
|
|
7
|
+
"types": "dist/types/utctime.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"clean": "rm -rf ./dist",
|
|
10
|
+
"build-js": "tsc -b ./tsconfig.json ./tsconfig.types.json",
|
|
11
|
+
"build": "npm run build-js",
|
|
12
|
+
"format": "prettier --write .",
|
|
13
|
+
"lint": "eslint src/ --ext .ts && prettier --check ."
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://gitlab.aiursoft.cn/aiursoft/utctime.js"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"bootstrap": "^4.6.2"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"esbuild": "^0.19.11",
|
|
24
|
+
"@typescript-eslint/eslint-plugin": "^6.16.0",
|
|
25
|
+
"@typescript-eslint/parser": "^6.16.0",
|
|
26
|
+
"eslint": "^8.56.0",
|
|
27
|
+
"node-sass": "^9.0.0",
|
|
28
|
+
"prettier": "^3.1.1",
|
|
29
|
+
"typescript": "^5.3.3"
|
|
30
|
+
},
|
|
31
|
+
"author": "Anduin Xue <anduin@aiursoft.com>",
|
|
32
|
+
"license": "MIT"
|
|
33
|
+
}
|
package/src/utctime.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
interface UtcTimeSettings {
|
|
3
|
+
attr: string
|
|
4
|
+
daysAgo: string
|
|
5
|
+
hoursAgo: string
|
|
6
|
+
minutesAgo: string
|
|
7
|
+
secondsAgo: string
|
|
8
|
+
disableAgo: boolean
|
|
9
|
+
onSet: (obj: Element) => void
|
|
10
|
+
disableAutoUpdate: boolean
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
class UtcTime {
|
|
14
|
+
defaultSettings: UtcTimeSettings = {
|
|
15
|
+
attr: 'data-utc-time',
|
|
16
|
+
daysAgo: ' days ago',
|
|
17
|
+
hoursAgo: ' hours ago',
|
|
18
|
+
minutesAgo: ' minutes ago',
|
|
19
|
+
secondsAgo: ' seconds ago',
|
|
20
|
+
disableAgo: false,
|
|
21
|
+
onSet: function (obj: Element) {},
|
|
22
|
+
disableAutoUpdate: false,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
constructor(settings: UtcTimeSettings) {
|
|
26
|
+
Object.assign(this.defaultSettings, settings)
|
|
27
|
+
const loop = () => {
|
|
28
|
+
this.initTime(this.defaultSettings)
|
|
29
|
+
if (!this.defaultSettings.disableAutoUpdate) {
|
|
30
|
+
setTimeout(loop, 1000)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
loop()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
timeSince(date: Date, settings: typeof this.defaultSettings): string {
|
|
37
|
+
const seconds = Math.floor((new Date().getTime() - date.getTime()) / 1000)
|
|
38
|
+
let interval = Math.floor(seconds / 2592000)
|
|
39
|
+
if (interval >= 1) {
|
|
40
|
+
return date.toLocaleDateString()
|
|
41
|
+
}
|
|
42
|
+
interval = Math.floor(seconds / 86400)
|
|
43
|
+
if (interval >= 1) {
|
|
44
|
+
return interval + settings.daysAgo
|
|
45
|
+
}
|
|
46
|
+
interval = Math.floor(seconds / 3600)
|
|
47
|
+
if (interval >= 1) {
|
|
48
|
+
return interval + settings.hoursAgo
|
|
49
|
+
}
|
|
50
|
+
interval = Math.floor(seconds / 60)
|
|
51
|
+
if (interval >= 1) {
|
|
52
|
+
return interval + settings.minutesAgo
|
|
53
|
+
}
|
|
54
|
+
if (interval >= 0) {
|
|
55
|
+
return Math.floor(seconds) + settings.secondsAgo
|
|
56
|
+
} else {
|
|
57
|
+
return date.toLocaleDateString()
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
getDate(inputString: string): Date {
|
|
62
|
+
if (inputString.endsWith(' UTC') || inputString.endsWith('Z')) {
|
|
63
|
+
return new Date(inputString)
|
|
64
|
+
}
|
|
65
|
+
if (!inputString.endsWith(' UTC')) {
|
|
66
|
+
const timeValue = inputString + ' UTC'
|
|
67
|
+
const date = new Date(timeValue)
|
|
68
|
+
if (!isNaN(date.getTime())) {
|
|
69
|
+
return date
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (!inputString.endsWith('Z')) {
|
|
73
|
+
const timeValue = inputString + 'Z'
|
|
74
|
+
const date = new Date(timeValue)
|
|
75
|
+
if (!isNaN(date.getTime())) {
|
|
76
|
+
return date
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
console.warn(
|
|
80
|
+
`jquery-utc-time: Input string ${inputString} can not be converted to UTC time and try to parse as typical date string.`,
|
|
81
|
+
)
|
|
82
|
+
return new Date(inputString)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
initTime(settings: typeof this.defaultSettings): void {
|
|
86
|
+
document
|
|
87
|
+
.querySelectorAll('*[' + settings.attr + ']')
|
|
88
|
+
.forEach((timeField) => {
|
|
89
|
+
const sourceValue = timeField.getAttribute(settings.attr)
|
|
90
|
+
const date = this.getDate(sourceValue!)
|
|
91
|
+
let text = this.timeSince(date, settings)
|
|
92
|
+
if (settings.disableAgo) {
|
|
93
|
+
text = date.toLocaleDateString()
|
|
94
|
+
}
|
|
95
|
+
timeField.innerHTML = text
|
|
96
|
+
timeField.setAttribute('data-toggle', 'tooltip')
|
|
97
|
+
timeField.setAttribute('data-trigger', 'hover')
|
|
98
|
+
timeField.setAttribute('data-title', date.toLocaleString())
|
|
99
|
+
settings.onSet(timeField)
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export default UtcTime
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"esModuleInterop": true,
|
|
4
|
+
"moduleResolution": "node",
|
|
5
|
+
"removeComments": false,
|
|
6
|
+
"sourceMap": true,
|
|
7
|
+
"strict": true,
|
|
8
|
+
"target": "ESNext",
|
|
9
|
+
"outDir": "./dist/esm",
|
|
10
|
+
"module": "esnext"
|
|
11
|
+
},
|
|
12
|
+
"include": ["src/**/*"]
|
|
13
|
+
}
|