Skip to content

Commit

Permalink
Initial commit with the basic functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
prantlf committed Sep 18, 2018
1 parent ca31221 commit d98d829
Show file tree
Hide file tree
Showing 30 changed files with 8,231 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": [
[
"@babel/preset-env",
{
"loose": true
}
]
]
}
2 changes: 2 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
service_name: travis-pro
repo_token: Z7iquGRhqAp0Ejh4Q0myiLEETJa0F0crH
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extends: standard
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage
dist
node_modules
18 changes: 18 additions & 0 deletions .jsdoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"source": {
"include": [
"src/convertToLocalTime.js",
"src/convertToTimeZone.js",
"src/parseFromTimeZone.js",
"src/formatToTimeZone.js"
]
},
"plugins": ["plugins/markdown"],
"opts": {
"encoding": "utf8",
"destination": "docs",
"recurse": true,
"readme": "README.md",
"template": "node_modules/minami"
}
}
7 changes: 7 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.editorconfig
.gitignore
.npmignore
.vscode
perf
rollup.config*
test
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
sudo: false
language: node_js
cache:
directories:
- ~/.npm
notifications:
email: false
node_js:
- '10'
- '8'
- '6'
after_success:
- npm run coverage
- npm run travis-deploy-once "npm run semantic-release"
branches:
except:
- /^v\d+\.\d+\.\d+$/
23 changes: 23 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
// Verwendet IntelliSense zum Ermitteln möglicher Attribute.
// Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen.
// Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Test",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"-i",
"--testPathIgnorePatterns",
"browser.test.js"
],
"internalConsoleOptions": "openOnSessionStart",
"skipFiles": [
"<node_internals>/**/*.js"
]
}
]
}
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"files.exclude": {
"coverage": true,
"docs": true,
"node_modules": true
},
"search.exclude": {
"**/src/lookup/data.js": true,
"**/dist/**": true,
"**/test/browser/**": true
}
}
33 changes: 33 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "npm",
"script": "build",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "build:debug",
"type": "npm",
"script": "build:debug",
"problemMatcher": []
},
{
"label": "test",
"type": "npm",
"script": "test",
"problemMatcher": [],
"group": {
"kind": "test",
"isDefault": true
}
},
]
}
230 changes: 229 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,230 @@
# date-fns-timezone
Parsing and formatting date strings using IANA time zones for date-fns.
[![NPM version](https://badge.fury.io/js/date-fns-timezone.png)](http://badge.fury.io/js/date-fns-timezone)
[![Build Status](https://travis-ci.org/prantlf/date-fns-timezone.png)](https://travis-ci.org/prantlf/date-fns-timezone)
[![Coverage Status](https://coveralls.io/repos/github/prantlf/date-fns-timezone/badge.svg?branch=master)](https://coveralls.io/github/prantlf/date-fns-timezone?branch=master)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/4bb0f2ef6c1b4212a4ed2dbf1f3e8b29)](https://www.codacy.com/app/prantlf/date-fns-timezone?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=prantlf/date-fns-timezone&amp;utm_campaign=Badge_Grade)
utm_content=prantlf/date-fns-timezone&amp;utm_campaign=Badge_Grade)
[![Dependency Status](https://david-dm.org/prantlf/date-fns-timezone.svg)](https://david-dm.org/prantlf/date-fns-timezone)
[![devDependency Status](https://david-dm.org/prantlf/date-fns-timezone/dev-status.svg)](https://david-dm.org/prantlf/date-fns-timezone#info=devDependencies)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

Provides parsing and formatting date strings and time zone conversions supporting [IANA time zones]. Date parsing, formatting and other queries and manipulations are provided by [date-fns]. List of canonical time zone names and time zone conversions are provided by [timezone-support].

- [Synopsis](#synopsis)
- [Installation and Loading](#installation-and-loading)
- [Specific Environments](#specific-environments)
- [Usage Scenarios](#usage-scenarios)
- [List all available time zones](#list-all-available-time-zones)
- [Parse a date from a string in a specific time zone](#parse-a-date-from-a-string-in-a-specific-time-zone)
- [Format a date to a string in specific time zone](#format-a-date-to-a-string-in-specific-time-zone)
- [Change the time zone of date](#change-the-time-zone-of-date)
- [API Reference](#api-reference)
- [Contributing](#contributing)
- [Release History](#release-history)
- [License](#license)

## Synopsis

```js
const { listTimeZones } = require('timezone-support')
const { parseFromTimeZone, formatToTimeZone } = require('date-fns-timezone')

// List canonical time zone names: [ 'Africa/Abidjan', ... ]
const timeZones = listTimeZones()

// Set the date to "2018-09-01T16:01:36.386Z"
const date = parseFromTimeZone('2018-09-01 18:01:36.386', { timeZone: 'Europe/Berlin' })

// Set the output to "1.9.2018 18:01:36.386 GMT+02:00 (CEST)"
const date = new Date('2018-09-01Z16:01:36.386Z')
const format = 'D.M.YYYY HH:mm:ss.SSS [GMT]Z (z)'
const output = formatToTimeZone(date, format, { timeZone: 'Europe/Berlin' })
```

## Installation and Loading

This module can be installed in your project using [NPM] or [Yarn]. Make sure, that you use [Node.js] version 6 or newer.

```sh
$ npm i date-fns-timezone --save
```

```sh
$ yarn add date-fns-timezone
```

### Specific Environments

Load the main module in an application using CommonJS modules:

```js
const {
convertToLocalTime, convertToTimeZone, parseFromTimeZone, formatToTimeZone
} = require('date-fns-timezone')
```

Load only specific modules in an application using CommonJS modules:

```js
const { convertToLocalTime } = require('date-fns-timezone/dist/convertToLocalTime')
const { convertToTimeZone } = require('date-fns-timezone/dist/convertToTimeZone')
const { parseFromTimeZone } = require('date-fns-timezone/dist/parseFromTimeZone')
const { formatToTimeZone } = require('date-fns-timezone/dist/formatToTimeZone')
```

Load the main module in an application using ES6 modules:

```js
import {
convertToLocalTime, convertToTimeZone, parseFromTimeZone, formatToTimeZone
} from './node_modules/date-fns-timezone/src/index.js'
```

Load only specific modules in an application using ES6 modules:

```js
import { convertToLocalTime } from './node_modules/date-fns-timezone/src/convertToLocalTime.js'
import { convertToTimeZone } from './node_modules/date-fns-timezone/src/convertToTimeZone.js'
import { parseFromTimeZone } from './node_modules/date-fns-timezone/src/parseFromTimeZone.js'
import { formatToTimeZone } from './node_modules/date-fns-timezone/src/formatToTimeZone.js'
```

Load the main module in the browser with plain JavaScript:

```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.29.0/date_fns.min.js"></script>
<script src="https://unpkg.com/timezone-support@1.2.1/dist/index.umd.js"></script>
<script src="https://unpkg.com/date-fns-timezone@0.0.1/dist/index.umd.js"></script>
<script>
(() => {
const {
convertToLocalTime, convertToTimeZone, parseFromTimeZone, formatToTimeZone
} = window.dateFnsTimezone
})()
</script>
```

Load the main module in the browser with [RequireJS]:

```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.29.0/date_fns.min.js"></script>
<script src="https://unpkg.com/timezone-support@1.2.1/dist/index.umd.js"></script>
<script src="https://unpkg.com/date-fns-timezone@0.0.1/dist/index.umd.js"></script>
<script>
require(['date-fns-timezone'], ({ parseFromTimeZone, formatToTimeZone }) => {
})
</script>
```

## Usage Scenarios

The minimal, but powerful API of this module provides functionality for the most usual usage scenarios in your web applications.

### List all available time zones

Users may need to choose the time zone, which they want to see and enter dates in. Time zones can be listed in a dropdown for the user to choose from, for example.

```js
const { listTimeZones } = require('timezone-support')
const timeZones = listTimeZones()
```

See the function [listTimeZones] for more information.

### Parse a date from a string in a specific time zone

Dates are usually entered in a time zone chosen by the user, but they are supposed to be stored in UTC. After the user input is parsed, the result date needs to be converted from the user time zone to the native `Date` object, which maintains the date in the browser-local time zone and UTC.

```js
const { parseZonedTime } = require('date-fns-timezone/dist/parseFromTimeZone')

const enteredTime = '2018-09-02 12:04:30.982'
const timeZone = 'Europe/Berlin'
const storedDate = parseFromTimeZone(enteredTime, { timeZone })
// Contains date "2018-09-02T10:04:30.982Z"
```

See the function [parseFromTimeZone](#parsefromtimezone) for more information.

### Format a date to a string in specific time zone

Dates are supposed to be stored in UTC, but they are usually displayed in a time zone chosen by the user. Before the date value is formatted, it needs to be converted to the user time zone.

```js
const { formatToTimeZone } = require('date-fns-timezone/dist/formatToTimeZone')

const storedDate = new Date('2018-09-02T10:04:30.982Z')
const format = 'D.M.YYYY H:mm:ss [GMT]Z (z)'
const timeZone = 'Europe/Berlin'
const displayedTime = formatToTimeZone(storedDate, format, { timeZone })
// Contains string "2.9.2018 12:04:30 GMT+02:00 (CEST)"
```

See the function [formatToTimeZone](#formattotimezone) for more information.

### Change the time zone of date

Date pickers usually supply the date, which the user selected, and the time zone is implied from the user settings. The local time in the date value is not the browser-local time and the UTC value cannot be used yet. The date has to be converted from the user time zone to the browser-local time to become valid.

```js
const { convertToLocalTime } = require('date-fns-timezone/dist/convertToLocalTime')

const chosenDate = new Date(2018, 8, 2, 12, 4, 30, 982)
const timeZone = 'Europe/Berlin'
const storedDate = convertToLocalTime(chosenDate, { timeZone })
// Contains date "2018-09-02T10:04:30.982Z"
```

Date pickers are usually initialized by a date in the time zone implied from the user settings. However, the browser-local time may be different. The date has to be converted from the browser-local time to the user time zone.

```js
const { convertToTimeZone } = require('date-fns-timezone/dist/convertToTimeZone')

const storedDate = new Date('2018-09-02T10:04:30.982Z')
const timeZone = 'Europe/Berlin'
const defaultDate = convertToTimeZone(storedDate, { timeZone })
// Contains date "2018-09-02 12:04:30.982"
```

See functions [convertToLocalTime](#converttolocaltime) and [convertToTimeZone](#converttotimezone) for more information.

## API Reference

The API consists of functions only. They are divided to three modules, which you can load separately or together depending on your usage scenario.

### date-fns-timezone/dist/index

Main package module for Node.js application. CommonJS format. Includes all functions from the other modules.

### date-fns-timezone/dist/index.umd.js

Main package module first of all for web browsers. UMD format, minified. Includes all functions from the other modules. Make sure, that you include `script` elements for `date-fns` and `timezone-support` on your web page before this one [as documented earlier](#specific-environments).

### convertToLocalTime
### convertToTimeZone
### parseFromTimeZone
### formatToTimeZone

## Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

## Release History

* 2018-09-17 v0.0.1 Initial release

## License

Copyright (c) 2018 Ferdinand Prantl

Licensed under the MIT license.

[IANA time zones]: https://www.iana.org/time-zones
[date-fns]: https://github.com/date-fns/date-fns
[timezone-support]: https://github.com/prantlf/timezone-support
[Node.js]: http://nodejs.org/
[NPM]: https://www.npmjs.com/
[Yarn]: https://yarnpkg.com/
[RequireJS]: https://requirejs.org/
[listTimeZones]: https://github.com/prantlf/timezone-support#listtimezones
Loading

0 comments on commit d98d829

Please sign in to comment.