javascript - 时间戳在本地机器和 gitlab 管道上的解释不同

标签 javascript node.js datetime jestjs gitlab-ci

我有以下代码将时间戳转换为文本格式。

export default function convertTime(time) {
    let date = new Date(time);
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    let hours = date.getHours();
    let minutes = "0" + date.getMinutes();
    return day + "." + month + "." + year + " " + hours + ':' + minutes.substr(-2);
}

我 Jest 测试如下:

从“../../../src/components/Base/TimeConverter”导入转换时间;

describe("Test time converter", function () {
    it("Time converter should return valid string for a certain hard coded time stamp in milliseconds", function () {
        const result = convertTime(1585575410 * 1000);
        const expected_result = "30.3.2020 15:36";
        expect(result).toBe(expected_result);
    });
});

在我的电脑上测试通过:

lara@dirk:~/Desktop/git/JS/kiwi$ npm run test

> kiwi@0.1.0 test /home/lara/Desktop/git/JS/kiwi
> env-cmd -f .env.dev jest --passWithNoTests

 PASS  tests/components/Base/TestTimeConverter.spec.js
 PASS  tests/others/TestEnvironmentVariables.spec.js

Test Suites: 2 passed, 2 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        1.349s
Ran all test suites.

但是在 gitlab 上它失败了:

$ npm run test
 > kiwi@0.1.0 test /builds/kiwi4/frontend
 > env-cmd -f .env.dev jest --passWithNoTests
 PASS tests/others/TestEnvironmentVariables.spec.js
 FAIL tests/components/Base/TestTimeConverter.spec.js
   ● Test time converter › Time converter should return valid string for a certain hard coded time stamp in milliseconds
     expect(received).toBe(expected) // Object.is equality
     Expected: "30.3.2020 15:36"
     Received: "30.3.2020 13:36"
       5 |         const result = convertTime(1585575410 * 1000);
       6 |         const expected_result = "30.3.2020 15:36";
     > 7 |         expect(result).toBe(expected_result);
         |                        ^
       8 |     });
       9 | });
       at Object.<anonymous> (tests/components/Base/TestTimeConverter.spec.js:7:24)
 Test Suites: 1 failed, 1 passed, 2 total
 Tests:       1 failed, 1 passed, 2 total

我的问题是为什么?

最佳答案

GitLab runner 实例和您的计算机有不同的时区。

引自 dzone.com

When you call getTime method on Date object you get the number of milliseconds from Unix epoch. Although your current Date object keeps time with some offset getTime gives seconds in UTC. Keep this in mind when creating timestamps if you are not living on zero-meridian.

var currentDate = selectedDate;          

var currentTime = currentDate.getTime(); 

This is pretty awkward, unexpected and unintuitive behavior but you have to keep in mind that all date and time calculations must use same time system to give appropriate results.

您需要获取 UTC 时区的属性或使用以下方法计算时区偏移量:

new Date(timestamp + (offset * 1000))

您可以使用以下方法计算客户的偏移量:

const offset = new Date().getTimezoneOffset();

关于javascript - 时间戳在本地机器和 gitlab 管道上的解释不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60932925/

相关文章:

javascript - 如何在 Xamarin iOS 项目中评估 JavaScript 并获得结果?

node.js - 使用 ES6 模块时 Node.js 中 __dirname 的替代方案

javascript - 为什么我在 Google Apps 脚本中的 Date 对象返回 NaN

python - 按季度生成时间序列,递增四分之一

javascript - 回调 NodeJS 并使用 mongoose 插入数据

javascript - 我的日期选择器始终在当月打开

javascript - Bootstrap 可折叠导航栏不会折叠

mysql - 如何在使用 Angular/Express 时处理具有多个参数的 MySQL 查询

node.js - 错误消息 : MongoError: bad auth Authentication failed through URI string

PHP:如何从模式 H:i (17:45) 创建时间戳?