Compare commits

...

103 commits
v0.8.4 ... main

Author SHA1 Message Date
f2d5290d28
Add 0dependencies.dev badge 2024-10-31 11:55:20 -07:00
8db2211a2f
Update minified assets 2024-06-11 17:02:47 -07:00
774e3ce102
Update contributors and copyright year 2024-06-11 17:02:16 -07:00
e61895af6c
Add changelog and bump versions to 0.10.3 (#90) 2024-06-11 16:58:56 -07:00
8a335897ae
Merge pull request #91 from samsonjs/fix/88-chinese-months
Fix Chinese months
2024-06-11 16:56:46 -07:00
db1c066f13
Fix Chinese months
Change as described by @mogando668, closes #88
2024-06-11 16:55:58 -07:00
6cf7d59a46
Merge pull request #89 from samsonjs/87-using-let-requires-es6-since-v0102
Stop using `let`, closes #87
2024-05-28 12:17:07 -07:00
53e26ba77d
Stop using let, closes #87 2024-05-28 12:16:26 -07:00
a0a62db076
Release v0.10.2 2023-05-24 21:05:07 -07:00
52753479c8
Tweak code style and minify 2023-05-24 21:02:03 -07:00
67c02b5352
Merge pull request #86 from aryan-debug/master
Fixed issue #84
2023-05-24 08:28:05 -07:00
aryan-debug
b50d512dee Fixed issue #84 2023-05-21 22:14:09 -07:00
aryan-debug
6157e8a650 Fixed test case for %c 2023-05-21 21:53:03 -07:00
aryan-debug
51a6ade03d Fixed issue #84 2023-04-17 01:54:01 -07:00
93aaf1eac6 Add locale identifier to example in readme 2021-12-12 10:53:40 -08:00
12088240fc Fix npm shield version 2021-12-12 10:51:05 -08:00
3af75444e1 Add Yusuke Kawasaki @kawanet to contributors 2021-12-12 10:48:47 -08:00
1bee290ce5 v0.10.1 2021-12-12 10:43:22 -08:00
dee584fda4 Improve support for v8's Date.toString time zone 2021-12-12 10:43:07 -08:00
5a1db7ba03 Update copyright and link in package.json 2021-12-12 10:41:47 -08:00
000143999d Fix order of expected and actual values in assertions 2021-12-12 10:40:45 -08:00
ce80bc39e3
Merge pull request #83 from douglasep/master
fix it_IT shortMonths locale
2021-12-11 23:37:33 -08:00
Douglas E. Pereira
e0780d045f fix it_IT shortMonths locale 2021-11-29 22:34:47 -03:00
08a9a4b738
Merge pull request #77 from kawanet/issue/76
re #76: X: equivalent to %r in en_US
2017-12-04 16:53:43 -08:00
Yusuke Kawasaki
06107d286a re #76: X: equivalent to %r in en_US 2017-11-27 16:29:03 +09:00
8a06a30115
add keywords to package.json 2017-03-10 09:55:10 -08:00
52720f64f5
link the changelog from readme 2016-12-28 12:29:34 -08:00
d3a2095ca3
fix a typo 2016-12-28 12:27:42 -08:00
793ecfb7b4 v0.10.0 2016-12-28 12:21:09 -08:00
0fd891c2ab update minified code 2016-12-28 12:09:55 -08:00
5e674f0a17 alphabetize contributors and update the list in package.json 2016-12-28 12:02:06 -08:00
3ed3100018 remove deprecated API, closes #55 2016-12-28 11:58:09 -08:00
c4967d51e0 fix tests broken by changes to the default locale format 2016-12-28 11:57:36 -08:00
fd648f7f19 document bundled locales and how to use them 2016-12-28 11:44:27 -08:00
efcd086585 update contributors 2016-12-28 11:33:04 -08:00
2375b9732f add en_CA and zh_CN locales 2016-12-28 11:33:00 -08:00
335ef2ebe8 warn when trying to use a non-existent locale 2016-12-28 11:23:33 -08:00
1e41e85146 fix a bug with ordinal localization fallback 2016-12-28 11:23:19 -08:00
e571efe7bb fix indentation and capitalize Locales constant 2016-12-28 11:23:08 -08:00
71737c8004 Merge pull request #70 from iFixit/master
Localizations
2016-12-28 11:02:58 -08:00
a54410d663
allow localizing ordinals 2016-12-28 10:44:44 -08:00
f6c88d0629
fix dates crossing DST boundaries for UTC, closes #71 2016-12-28 10:32:43 -08:00
02586fa41e
update minified code 2016-12-27 11:03:29 -08:00
4c747ffa2a
update contributors and copyrights 2016-12-27 11:03:09 -08:00
b4e57a851e Merge pull request #68 from Kevin-Jin/patch-1
Pass through unrecognized directives
2016-12-27 10:58:05 -08:00
2294caa44b
add a comment about mis-detecting DateJS dates as locales 2016-12-27 10:57:37 -08:00
65aa4d5ea9 Merge pull request #69 from stiang/master
Make strftime play nice with dates that have been extended by DateJS
2016-12-27 10:52:20 -08:00
Alexandr Nikitin
f8e5b11c9b Merge pull request #72 from IOAyman/patch-1
Added yarn install cmd
2016-12-08 15:58:33 +02:00
Ayman Nedjmeddine
82a8bfae79 Added yarn install cmd 2016-12-05 21:58:44 +01:00
Andrew Pirondini
fc16548517 Merge pull request #3 from iFixit/fix-typo-in-portuguese-locale
Fix typo in Portuguese locale
2016-10-03 11:11:59 -07:00
Andrew Pirondini
0d0f90f7eb Fix typo in Portuguese locale 2016-10-03 10:12:53 -07:00
b1629fa333 add badges to readme 2016-09-06 18:26:12 -07:00
Stian Grytøyr
84bad921ab Make strftime play nice with dates that have been extended by DateJS 2015-12-31 01:25:55 +01:00
Kevin Jin
f5bb098221 Consistency with existing code
Add test for unrecognized directives to test.js.
Add braces around if statement.
2015-10-02 21:39:02 -04:00
Kevin Jin
58f5c3fee8 Support literal percent sign
By the spec for strftime(3), '%%' in the format string should be replaced with '%' in the resultString.
2015-09-27 11:03:32 -04:00
Kevin Jin
72a2f418c8 Pass through unrecognized directives
For compatibility with strftime() on Unix, preserve the percent sign for unrecognized directives when appending to resultString.
2015-09-27 10:41:58 -04:00
a133ab1a4e fix readme example output, closes #66 2015-08-30 16:44:18 -07:00
Andrew Pirondini
b0659a5f78 Add definitions for nine locales.
This adds the definitions for nine locales in the strftime file. It also
provides a function for getting a strftime localized by the locale
identifier.
2015-08-05 14:07:57 -07:00
Andrew Pirondini
870e4e3771 Merge pull request #1 from iFixit/update-strftime-for-localization
Update strftime for localization
2015-07-20 11:41:11 -07:00
d6f6e74351 fix example code 2015-06-11 09:53:06 -07:00
b852daca88 v0.9.2 2015-05-29 17:52:48 -07:00
93326a4d73 fix cached timestamps, closes #63 2015-05-29 17:45:20 -07:00
a213d716a6 Merge pull request #62 from pdehaan/patch-1
Update license attribute
2015-05-23 17:04:18 -07:00
Peter deHaan
29111d3bbb Update license attribute
specifying the type and URL is deprecated:

https://docs.npmjs.com/files/package.json#license
http://npm1k.org/
2015-05-23 16:08:48 -07:00
8adfd97924 Merge pull request #60 from samsonjs/test-times
always use GMT to construct times in tests
2015-03-31 17:52:36 -07:00
48d143eb90 always use GMT to construct times in tests 2015-03-29 14:16:12 -07:00
fe2892ebfa Merge pull request #59 from samsonjs/Issue56_BugInWeekNumber
Fixes #56 (Bug in %U)
2015-03-29 13:26:50 -07:00
Alexandr Nikitin
aee0e19edc Revert "Revert "Fixes #56 (Bug in %U)""
This reverts commit ab3d4a052e.
2015-03-29 11:38:17 +03:00
22ddd8982f Merge pull request #58 from samsonjs/revert-57-Issue56_BugInWeekNumber
Revert "Fixes #56 (Bug in %U)"
2015-03-26 14:56:35 -07:00
ab3d4a052e Revert "Fixes #56 (Bug in %U)" 2015-03-26 14:44:26 -07:00
603a716bf8 Merge pull request #57 from alexandrnikitin/Issue56_BugInWeekNumber
Fixes #56 (Bug in %U)
2015-03-26 14:35:46 -07:00
Alexandr Nikitin
067ae77ad9 Fix day number calculation taking DST into account
Based on the SO answer http://stackoverflow.com/a/15289883/974487
2015-03-26 21:43:57 +02:00
Alexandr Nikitin
002ac8ca4c Add field report tests for bug in %U in #56 2015-03-26 21:40:46 +02:00
1d4365f3d5 try to fix syntax highlighting on GitHub 2015-03-16 18:07:52 -07:00
30fd95dbba vapourize some stray semicolons 2015-03-16 18:02:54 -07:00
8ba1f03a71 warn about possible misuse of %:: or %::: modifiers 2015-03-16 18:02:34 -07:00
dcc5d54c1b v0.9.1 2015-03-16 17:48:04 -07:00
765f3f1946 minify 2015-03-16 17:48:04 -07:00
4676d05afe add a changelog 2015-03-16 17:47:11 -07:00
437beabe43 add missing contributors to readme & package.json 2015-03-16 17:33:08 -07:00
7b1151c7c0 re-fix issue #38 which was lost in the v0.9 merge 2015-03-16 17:32:56 -07:00
4dcc655477 minify 2015-03-15 17:02:08 -07:00
f4a2c852c3 readme formatting and improved deprecation warnings 2015-03-15 16:57:35 -07:00
affbd8e1cd improve backwards compatibility, fix a couple bugs 2015-03-15 16:45:59 -07:00
543e81ca83 add install instructions for bower and component 2015-03-15 16:19:27 -07:00
e6b696cabd minify 2015-03-15 15:46:07 -07:00
1403c1e6a6 don't ignore browsers in the readme 2015-03-15 15:46:07 -07:00
c6464d0130 make the CommonJS / Node export backwards compatible
also improves deprecation notices
2015-03-15 15:38:16 -07:00
bfe34d92b5 polyfill Date.now for old browsers 2015-03-15 15:20:32 -07:00
23cd5feb98 code formatting / nitpicks 2015-03-15 15:20:31 -07:00
6bf19b4454 remove test directory 2015-03-05 18:29:15 -08:00
ff9b94b3d9 minify & update bower.json 2015-03-05 17:02:53 -08:00
e3cfa8c497 Merge pull request #42 from samsonjs/v0.9
v0.9 rewrite - all about API and performance
2015-03-05 16:59:05 -08:00
e631d81206 Merge branch 'master' into v0.9
Conflicts:
	component.json
	package.json
2015-03-05 16:55:51 -08:00
c029f7a79e Merge branch 'master' into v0.9
Conflicts:
	strftime.js
	test/test.js
2015-03-05 16:49:32 -08:00
603326eaed code style and copyright 2015-02-07 20:59:15 -08:00
6bc6888fff add support for %c, %x, and %X (closes #43)
based on @alexandrnikitin's commit here: f25c89bb0f
2015-02-07 20:59:15 -08:00
e16f49f4c0 update readme for 0.9 API 2015-02-07 20:59:15 -08:00
6ab81c89cb code style 2015-02-07 20:59:15 -08:00
f1581a3c9d bump version to 0.9.0 2015-02-07 20:59:14 -08:00
f521749967 add @alexandrnikitin to contributors 2015-02-07 20:58:54 -08:00
f36819ad99 merge V2 code and deprecate the old API 2015-02-07 20:58:53 -08:00
Alexandr Nikitin
016223fac4 Added optimized version 2015-02-07 20:57:56 -08:00
11 changed files with 1587 additions and 649 deletions

294
Changelog.md Normal file
View file

@ -0,0 +1,294 @@
v0.10.3 on 2024-06-11
---------------------
- Stop using `let` to avoid breaking backwards compatibility with ES5. Thanks to [Maximilian Herold](https://github.com/mherold) for the report #87
- Drop '份' suffix from Chinese month names. Thanks to [@mogando668](https://github.com/mogando668) for the report #88
v0.10.2 on 2023-05-24
---------------------
- Fix test case for %c in current versions of node.js
- Fix %-y so it omits the leading zero
Thanks to [Aryan Arora](https://github.com/aryan-debug) for both fixes!
v0.10.1 on 2021-12-12
---------------------
- Fix it_IT shortMonths locale
Thanks to [Douglas de Espindola](https://github.com/douglasep)
- Try to get a short time zone name for %Z, falling back to the long one. This attempts to work around a change to Date.toString in v8 (node v10+, Chrome 66+). Unfortunately sometimes Date.toLocaleString returns a time zone like GMT+2 and we fall back to the long name in that case.
v0.10.0 on 2016-12-28
---------------------
- Remove the old API deprecated in v0.9.
- Bundle some locales that you can use with `strftime.localizeByIdentifier('nl_NL')`. Full list available in the [readme](https://github.com/samsonjs/strftime/blob/master/Readme.md).
Thanks to [Andrew Pirondini](https://github.com/andrewjpiro) of [iFixit](https://ifixit.com)
- Add instructions to install with Yarn.
Thanks to [Ayman Nedjmeddine](https://github.com/IOAyman)
- Unrecognized format specifiers preserve the % sign, e.g. `strftime('%K') => "%K"`.
Thanks to [Kevin Jin](https://github.com/Kevin-Jin)
- Make `strftime` work with dates that have been extended by [DateJS](http://www.datejs.com).
Thanks to [Stian Grytøyr](https://github.com/stiang)
- Add instructions to install with Yarn.
Thanks to [Ayman Nedjmeddine](https://github.com/IOAyman)
- Fix UTC formatting of dates that cross a DST boundary.
Thanks to [ray007](https://github.com/ray007)
- Allow localizing ordinals.
Thanks to [Simon Liétar](https://github.com/Sim9760)
v0.9.2 on 2015-05-29
--------------------
- fix a caching bug, which was a regression in 0.9.0 ([issue #63](https://github.com/samsonjs/strftime/issues/63))
- update license attribute in package.json as required by npm
Thanks to [Peter deHaan](https://github.com/pdehaan)
- construct GMT times used in tests in a more robust way
- fix a bug calculating week numbers ([issue #56](https://github.com/samsonjs/strftime/issues/56))
Thanks to [Alexandr Nikitin](https://github.com/alexandrnikitin)
- warn about possible misuse of %:: or %::: modifiers
v0.9.1 on 2015-03-16
--------------------
- re-fix [issue #38](https://github.com/samsonjs/strftime/pull/38) which was lost in the v0.9 merge
- add this changelog
v0.9.0 on 2015-03-15
--------------------
This release marks the final run up to v1.0, which should be released by March 2016.
The headline feature is a huge performance boost resulting from [this contest](http://hola.org/challenge_js). [Alexandr Nikitin](https://github.com/alexandrnikitin) has essentially [rewritten](https://github.com/samsonjs/strftime/pull/41) the code and [the results](http://jsperf.com/strftime-optimization/2) speak for themselves.
Along with this the API has been unified and cleaned up. `strftimeTZ`, `strftimeUTC`, and `localizedStrftime` have all been deprecated in favour of the following functions: `timezone(tz)`, `utc()`, and `localize(locale)`. You use them like so:
```JavaScript
var strftime = require('strftime'); // not required in web browsers
var strftimeIT = strftime.localize(anItalianLocale);
var strftimePST = strftime.timezone('-0800');
var strftimeUTC = strftime.utc();
// You can combine them
var strftimeIT_PST = strftimeIT.timezone('-0800');
// And chain them all at once
var strftimeIT_PST = strftime.localize(anItalianLocale).timezone('-0800');
```
The previous API is deprecated and will be removed for v1.0. The good news is that the previous API is supported by adapting the new API, so you get most of the performance benefits before you even update your code to use the new API.
The new API does not support passing in a custom timezone or locale on each call to `strftime`. This is a regression so if you really need this use case [let us know](https://github.com/samsonjs/strftime/issues/new) and we'll figure something out.
Thanks to all contributors that have helped to improve this library over the past 4 years.
v0.8.4 on 2015-03-05
--------------------
- fix conversion of dates to UTC
Thanks to [Alexandr Nikitin](https://github.com/alexandrnikitin)
- extend `%z` with a colon separator in timezone offsets, `"[+-]HH:MM"`
Thanks to [Cory Heslip](https://github.com/cheslip)
- ignore irrelevant files in bower.json
v0.8.3 on 2015-02-08
--------------------
First release for [Bower](http://bower.io), and only released for Bower.
v0.8.2 on 2014-08-08
--------------------
- fix `%e` which is supposed to be padded with a space
v0.8.1 on 2014-06-17
--------------------
- fix `%Z` when the timezone contains spaces
Thanks to [w0den](https://github.com/w0den)
- fix examples using `%Y` in the readme
Thanks to [Ryan Regalado](https://github.com/d48)
- fix a bug when specifying minutes in the timezone
Thanks to [Alexandr Nikitin](https://github.com/alexandrnikitin)
v0.8.0 on 2014-01-29
--------------------
- allow timezones to be specified as strings of the form `"+0100"` or `"-0800"` (`[+-]HHMM`)
- fix a bug running tests where the environment variable `TZ` is empty and the system timezone is not PST/PDT
v0.7.0 on 2013-11-08
--------------------
- add support for passing in explicit timezones with `strftimeTZ` which accepts numeric offsets from GMT, in minutes
v0.6.2 on 2013-08-29
--------------------
- expose `strftimeUTC` and `localizedStrftime` properly in browsers
v0.6.1 on 2013-06-13
--------------------
- fix a bug where `RequiredDateMethods` was created as a global
v0.6.0 on 2013-05-15
--------------------
- add `%o` to get the day of the month as an ordinal (in English)
v0.5.2 on 2013-04-07
--------------------
- add some Ruby extension prefixes: `-`, `_`, and `0`, and they work like so:
`strftime('%-d') // => "7"`
`strftime('%_d') // => " 7"`
`strftime('%0d') // => "07"`
- fix padding the day-of-year in `%j`
- add a minified version of the code to the repo for easy distribution with some package managers
v0.5.1 on 2013-03-07
--------------------
- remove deprecated `getLocalizedStrftime` function
- make `%C` pad the century with spaces, like C
- list all supported specifiers in the readme, it's no longer fair to say "look at `man 3 strftime`"
- use fixed dates in the readme instead of "now", so people can execute the examples and see the same results
Thanks to [John Zwinck](https://github.com/jzwinck)
- fix `%z` for timezones greater than GMT
- support any `Date`-like objects instead of checking for actual instances of `Date`
v0.5.0 on 2013-01-05
--------------------
- add week numbers `%U` and `%W`
- add support for [component](https://github.com/componentjs/component)
Thanks to [TJ Holowaychuk](https://github.com/tj)
v0.4.8 on 2012-11-13
--------------------
- add `%j` and `%C`, thanks to [Ryan Stafford](https://github.com/ryanstafford)
v0.4.7 on 2012-06-08
--------------------
- add `%P` which is "am" or "pm", like `%p` but lowercase
(this makes no sense, and I am sorry for propagating this madness)
Thanks to [Rob Colburn](https://github.com/robcolburn)
- export the `strftime` function directly in [node](https://nodejs.org) so you can write `strftime = require('strftime')` instead of `strftime = require('strftime').strftime`
- added contributors to the readme and package.json
v0.4.6 on 2011-06-13
--------------------
- rename `getLocalizedStrftime(locale)` to `localizedStrftime(locale)`
The old name is deprecated and will stick around until v0.5 or v0.6.
- add tests for locales
v0.4.5 on 2011-06-08
--------------------
- fix the sign of `%z`, which is something like "+0100" or "-0800" (`[+-]HHMM`)
- improve test coverage
v0.4.4 on 2011-06-07
--------------------
- fix `%L` for values < 100
- convert tests from CoffeeScript to JavaScript
(nothing personal, just keeping the dependencies trim)
v0.4.3 on 2011-06-05
--------------------
This release was all [Andrew Schaaf](https://github.com/andrewschaaf).
- add some tests
- fix `%s` which is seconds since the Unix epoch, but was in milliseconds
- add `%L` for 3-digit milliseconds
v0.4.2 on 2011-06-05
--------------------
- add `strftimeUTC` for ignoring timezones
Thanks to [Andrew Schaaf](https://github.com/andrewschaaf)
- support exporting to the top level object in ES5 strict mode
v0.4.1 on 2011-06-02
--------------------
- fix `%y` for years outside the range [1900, 2099]
v0.4.0 on 2011-04-28
--------------------
- add support for localization
v0.3.0 on 2010-12-17
--------------------
- fix export for browsers
v0.2.3 on 2010-12-15
--------------------
- set required [node](https://nodejs.org) version to 0.2 instead of 0.3 in package.json
v0.2.2 on 2010-11-14
--------------------
- fix module export
v0.2.1 on 2010-11-11
--------------------
- bug fix for recursive formats
v0.2.0 on 2010-11-11
--------------------
- use `String.prototype.replace` instead of a `for` loop
v0.1.0 on 2010-11-11
--------------------
Initial release.

View file

@ -2,14 +2,14 @@ minify: real-minify test-minified
real-minify: strftime.js real-minify: strftime.js
rm -f strftime-min.js rm -f strftime-min.js
closure <strftime.js >|strftime-min.js closure-compiler <strftime.js >|strftime-min.js
test: test:
TZ=America/Vancouver node test/test.js TZ=America/Vancouver node test.js
TZ=CET node test/test.js TZ=Europe/Amsterdam node test.js
test-minified: minify test-minified: minify
TZ=America/Vancouver node test/test.js ../strftime-min.js TZ=America/Vancouver node test.js ./strftime-min.js
TZ=CET node test/test.js ../strftime-min.js TZ=Europe/Amsterdam node test.js ./strftime-min.js
.PHONY: test test-minified .PHONY: test test-minified

180
Readme.md
View file

@ -1,64 +1,110 @@
strftime strftime
======== ========
strftime for JavaScript, works in Node.js and browsers, supports localization. strftime for JavaScript. Works in (at least) node.js and browsers. Supports localization and timezones. Most standard specifiers from C are supported as well as some other extensions from Ruby.
Most standard specifiers from C are supported as well as some other extensions
from Ruby.
[![version 0.10.3 on npm](https://img.shields.io/badge/npm-0.10.3-brightgreen.svg?style=flat)](https://www.npmjs.com/package/strftime)
[![node version 0.2 and up](https://img.shields.io/badge/node->=0.2-brightgreen.svg?style=flat)](https://nodejs.org)
[![MIT License](https://img.shields.io/badge/License-MIT-blue.svg?style=flat)](https://sjs.mit-license.org)
[![0 dependencies!](https://0dependencies.dev/0dependencies.svg)](https://0dependencies.dev)
Installation Installation
============ ============
npm install strftime [node](https://nodejs.org):
npm install strftime
[bower](http://bower.io):
bower install strftime
[component](https://github.com/componentjs/component):
component install samsonjs/strftime
[yarn](https://yarnpkg.com):
yarn add strftime
Or you can copy [strftime.js](https://github.com/samsonjs/strftime/blob/master/strftime.js) wherever you want to use it, whether that's with a &lt;script&gt; tag or `require` or anything else.
Changelog
=========
[View the changelog](https://github.com/samsonjs/strftime/blob/master/Changelog.md).
Usage Usage
===== =====
var strftime = require('strftime') ```JavaScript
var strftime = require('strftime') // not required in browsers
console.log(strftime('%B %d, %Y %H:%M:%S')) // => April 28, 2011 18:21:08 console.log(strftime('%B %d, %Y %H:%M:%S')) // => April 28, 2011 18:21:08
console.log(strftime('%F %T', new Date(1307472705067))) // => 2011-06-07 18:51:45 console.log(strftime('%F %T', new Date(1307472705067))) // => 2011-06-07 18:51:45
```
If you want to localize it: If you want to localize it:
var strftime = require('strftime') ```JavaScript
var strftime = require('strftime') // not required in browsers
var it_IT = { var it_IT = {
days: [ 'domenica', 'lunedi', 'martedi', 'mercoledi', 'giovedi', 'venerdi', 'sabato' ], identifier: 'it-IT',
shortDays: [ 'dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab' ], days: ['domenica', 'lunedi', 'martedi', 'mercoledi', 'giovedi', 'venerdi', 'sabato'],
shortDays: ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'],
months: [ 'gennaio', 'febbraio', 'marzo', 'aprile', 'maggio', 'giugno', 'luglio', months: ['gennaio', 'febbraio', 'marzo', 'aprile', 'maggio', 'giugno', 'luglio', 'agosto', 'settembre', 'ottobre', 'novembre', 'dicembre'],
'agosto', 'settembre', 'ottobre', 'novembre', 'dicembre' ], shortMonths: ['gen', 'feb', 'mar', 'apr', 'mag', 'giu', 'lug', 'ago', 'set', 'ott', 'nov', 'dic'],
shortMonths: [ 'gen', 'feb', 'mar', 'apr', 'mag', 'giu', 'lug', 'ago',
'set', 'ott', 'nov', 'dic' ],
AM: 'AM', AM: 'AM',
PM: 'PM' PM: 'PM',
am: 'am',
pm: 'pm',
formats: {
D: '%m/%d/%y',
F: '%Y-%m-%d',
R: '%H:%M',
X: '%T',
c: '%a %b %d %X %Y',
r: '%I:%M:%S %p',
T: '%H:%M:%S',
v: '%e-%b-%Y',
x: '%D'
}
} }
console.log(strftime('%B %d, %Y %H:%M:%S', it_IT)) // => aprile 28, 2011 18:21:08 var strftimeIT = strftime.localize(it_IT)
console.log(strftime('%B %d, %Y %H:%M:%S', new Date(1307472705067), it_IT)) // => giugno 7, 2011 18:51:45 console.log(strftimeIT('%B %d, %Y %H:%M:%S')) // => aprile 28, 2011 18:21:08
console.log(strftimeIT('%B %d, %Y %H:%M:%S', new Date(1307472705067))) // => giugno 7, 2011 18:51:45
```
And if you don't want to pass a localization object every time you can get a localized `strftime` function like so: Some locales are bundled and can be used like so:
var strftime = require('strftime') ```JavaScript
var it_IT = { /* same as above */ } var strftime = require('strftime') // not required in browsers
var strftime_IT = strftime.localizedStrftime(it_IT) var strftimeIT = strftime.localizeByIdentifier('it_IT')
console.log(strftime_IT('%B %d, %Y %H:%M:%S')) // aprile 28, 2011 18:21:08 console.log(strftimeIT('%B %d, %Y %H:%M:%S')) // => aprile 28, 2011 18:21:08
console.log(strftimeIT('%B %d, %Y %H:%M:%S', new Date(1307472705067))) // => giugno 7, 2011 18:51:45
```
_The [full list of bundled locales](#locales) is below._
Time zones can be passed in as an offset from GMT in minutes. Time zones can be passed in as an offset from GMT in minutes.
var strftimeTZ = require('strftime').strftimeTZ ```JavaScript
console.log(strftimeTZ('%B %d, %y %H:%M:%S', new Date(1307472705067), -420)) // => June 07, 11 11:51:45 var strftime = require('strftime') // not required in browsers
console.log(strftimeTZ('%F %T', new Date(1307472705067), 120)) // => 2011-06-07 20:51:45 var strftimePDT = strftime.timezone(-420)
var strftimeCEST = strftime.timezone(120)
console.log(strftimePDT('%B %d, %y %H:%M:%S', new Date(1307472705067))) // => June 07, 11 11:51:45
console.log(strftimeCEST('%F %T', new Date(1307472705067))) // => 2011-06-07 20:51:45
```
Alternatively you can use the timezone format used by ISO 8601, `+HHMM` or `-HHMM`. Alternatively you can use the timezone format used by ISO 8601, `+HHMM` or `-HHMM`.
var strftimeTZ = require('strftime').strftimeTZ ```JavaScript
console.log(strftimeTZ('', new Date(1307472705067), '-0700')) // => June 07, 11 11:51:45 var strftime = require('strftime') // not required in browsers
console.log(strftimeTZ('%F %T', new Date(1307472705067), '+0200')) // => 2011-06-07 20:51:45 var strftimePDT = strftime.timezone('-0700')
var strftimeCEST = strftime.timezone('+0200')
console.log(strftimePDT('%F %T', new Date(1307472705067))) // => 2011-06-07 11:51:45
console.log(strftimeCEST('%F %T', new Date(1307472705067))) // => 2011-06-07 20:51:45
```
Supported Specifiers Supported Specifiers
==================== ====================
@ -73,10 +119,11 @@ e.g. `%q` becomes `q`. Use `%%` to get a literal `%` sign.
- B: full month name - B: full month name
- b: abbreviated month name - b: abbreviated month name
- C: AD century (year / 100), padded to 2 digits - C: AD century (year / 100), padded to 2 digits
- D: equivalent to `%m/%d/%y` - c: equivalent to `%a %b %d %X %Y %Z` in en_US (based on locale)
- D: equivalent to `%m/%d/%y` in en_US (based on locale)
- d: day of the month, padded to 2 digits (01-31) - d: day of the month, padded to 2 digits (01-31)
- e: day of the month, padded with a leading space for single digit values (1-31) - e: day of the month, padded with a leading space for single digit values (1-31)
- F: equivalent to `%Y-%m-%d` - F: equivalent to `%Y-%m-%d` in en_US (based on locale)
- H: the hour (24-hour clock), padded to 2 digits (00-23) - H: the hour (24-hour clock), padded to 2 digits (00-23)
- h: the same as %b (abbreviated month name) - h: the same as %b (abbreviated month name)
- I: the hour (12-hour clock), padded to 2 digits (01-12) - I: the hour (12-hour clock), padded to 2 digits (01-12)
@ -88,49 +135,78 @@ e.g. `%q` becomes `q`. Use `%%` to get a literal `%` sign.
- m: the month, padded to 2 digits (01-12) - m: the month, padded to 2 digits (01-12)
- n: newline character - n: newline character
- o: day of the month as an ordinal (without padding), e.g. 1st, 2nd, 3rd, 4th, ... - o: day of the month as an ordinal (without padding), e.g. 1st, 2nd, 3rd, 4th, ...
- P: "am" or "pm" in lowercase [Ruby extension] - P: "am" or "pm" in lowercase (Ruby extension, based on locale)
- p: "AM" or "PM" - p: "AM" or "PM" (based on locale)
- R: equivalent to `%H:%M` - R: equivalent to `%H:%M` in en_US (based on locale)
- r: equivalent to `%I:%M:%S %p` - r: equivalent to `%I:%M:%S %p` in en_US (based on locale)
- S: the second, padded to 2 digits (00-60) - S: the second, padded to 2 digits (00-60)
- s: the number of seconds since the Epoch, UTC - s: the number of seconds since the Epoch, UTC
- T: equivalent to `%H:%M:%S` - T: equivalent to `%H:%M:%S` in en_US (based on locale)
- t: tab character - t: tab character
- U: week number of the year, Sunday as the first day of the week, padded to 2 digits (00-53) - U: week number of the year, Sunday as the first day of the week, padded to 2 digits (00-53)
- u: the weekday, Monday as the first day of the week (1-7) - u: the weekday, Monday as the first day of the week (1-7)
- v: equivalent to `%e-%b-%Y` - v: equivalent to `%e-%b-%Y` in en_US (based on locale)
- W: week number of the year, Monday as the first day of the week, padded to 2 digits (00-53) - W: week number of the year, Monday as the first day of the week, padded to 2 digits (00-53)
- w: the weekday, Sunday as the first day of the week (0-6) - w: the weekday, Sunday as the first day of the week (0-6)
- X: equivalent to `%T` or `%r` in en_US (based on locale)
- x: equivalent to `%D` in en_US (based on locale)
- Y: the year with the century - Y: the year with the century
- y: the year without the century (00-99) - y: the year without the century, padded to 2 digits (00-99)
- Z: the time zone name, replaced with an empty string if it is not found - Z: the time zone name, replaced with an empty string if it is not found
- z: the time zone offset from UTC, with a leading plus sign for UTC and zones east - z: the time zone offset from UTC, with a leading plus sign for UTC and zones east
of UTC and a minus sign for those west of UTC, hours and minutes follow each of UTC and a minus sign for those west of UTC, hours and minutes follow each
padded to 2 digits and with no delimiter between them padded to 2 digits and with no delimiter between them
For more detail see `man 3 strftime` as the format specifiers should behave For more detail see `man 3 strftime` as the format specifiers should behave identically. If behaviour differs please [file a bug](https://github.com/samsonjs/strftime/issues/new).
identically. If behaviour differs please [file a bug](https://github.com/samsonjs/strftime/issues/new).
Any specifier can be modified with `-`, `_`, `0`, or `:` as well, as in Ruby. Any specifier can be modified with `-`, `_`, `0`, or `:` as well, as in Ruby. Using `%-` will omit any leading zeroes or spaces, `%_` will force spaces for padding instead of the default, and `%0` will force zeroes for padding. There's some redundancy here as `%-d` and `%e` have the same result, but it solves some awkwardness with formats like `%l`. Using `%:` for time zone offset, as in `%:z` will insert a colon as a delimiter.
Using `%-` will omit any leading zeroes or spaces, `%_` will force spaces
for padding instead of the default, and `%0` will force zeroes for padding. <a name="locales"></a>
There's some redundancy here as `%-d` and `%e` have the same result, but it Bundled Locales
solves some awkwardness with formats like `%l`. Using `%:` for time zone offset, ===============
as in `%:z` will insert a colon as a delimiter.
- de\_DE
- en\_CA
- en\_US
- es\_MX
- fr\_FR
- it\_IT
- nl\_NL
- pt\_BR
- ru\_RU
- tr\_TR
- zh\_CN
Contributors Contributors
============ ============
* [Sami Samhuri](https://github.com/samsonjs) * [Alexandr Nikitin](https://github.com/alexandrnikitin)
* [Andrew Pirondini](https://github.com/andrewjpiro) of [iFixit](https://github.com/iFixit)
* [Andrew Schaaf](https://github.com/andrewschaaf) * [Andrew Schaaf](https://github.com/andrewschaaf)
* [Aryan Arora](https://github.com/aryan-debug)
* [Ayman Nedjmeddine](https://github.com/IOAyman)
* [Cory Heslip](https://github.com/cheslip)
* [Douglas de Espindola](https://github.com/douglasep)
* [Forbes Lindesay](https://github.com/ForbesLindesay)
* [John Zwinck](https://github.com/jzwinck)
* [Joost Hietbrink](https://github.com/joost)
* [Kevin Jin](https://github.com/Kevin-Jin)
* [Maximilian Herold](https://github.com/mherold)
* [Michael J.](https://github.com/michaeljayt)
* [@mogando668](https://github.com/mogando668)
* [Peter deHaan](https://github.com/pdehaan)
* [Rob Colburn](https://github.com/robcolburn) * [Rob Colburn](https://github.com/robcolburn)
* [Ryan Regalado](https://github.com/d48)
* [Ryan Stafford](https://github.com/ryanstafford) * [Ryan Stafford](https://github.com/ryanstafford)
* [Sami Samhuri](https://github.com/samsonjs)
* [Stian Grytøyr](https://github.com/stiang)
* [TJ Holowaychuk](https://github.com/tj)
* [@w0den](https://github.com/w0den)
* [Yusuke Kawasaki](https://github.com/kawanet)
License License
======= =======
Copyright 2010 - 2013 Sami Samhuri sami@samhuri.net Copyright 2010 - 2024 Sami Samhuri sami@samhuri.net
[MIT license](http://sjs.mit-license.org)
[MIT license](https://sjs.mit-license.org)

View file

@ -1,6 +1,6 @@
{ {
"name": "strftime", "name": "strftime",
"version": "0.8.4", "version": "0.10.3",
"main": "strftime.js", "main": "strftime.js",
"ignore": [ "ignore": [
"Readme.md", "Readme.md",

View file

@ -2,8 +2,15 @@
"name": "strftime", "name": "strftime",
"repo": "samsonjs/strftime", "repo": "samsonjs/strftime",
"description": "strftime date formatting", "description": "strftime date formatting",
"keywords": ["strftime", "format", "date", "time"], "keywords": [
"version": "0.8.4", "strftime",
"format",
"date",
"time"
],
"version": "0.10.3",
"main": "strftime.js", "main": "strftime.js",
"scripts": ["strftime.js"] "scripts": [
"strftime.js"
]
} }

17
package-lock.json generated Normal file
View file

@ -0,0 +1,17 @@
{
"name": "strftime",
"version": "0.10.3",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "strftime",
"version": "0.10.3",
"license": "MIT",
"devDependencies": {},
"engines": {
"node": ">=0.2.0"
}
}
}
}

View file

@ -1,14 +1,38 @@
{ {
"name": "strftime", "name": "strftime",
"description": "strftime for JavaScript", "description": "strftime for JavaScript",
"version": "0.8.4", "version": "0.10.3",
"homepage": "http://samhuri.net/proj/strftime", "homepage": "https://samhuri.net/projects/strftime",
"author": "Sami Samhuri <sami@samhuri.net>", "author": "Sami Samhuri <sami@samhuri.net>",
"contributors": [ "contributors": [
"Sami Samhuri <sami@samhuri.net> (http://samhuri.net)", "Alexandr Nikitin (https://github.com/alexandrnikitin)",
"Andrew Schaaf <andrew@andrewschaaf.com> (http://andrewschaaf.com/)", "Andrew Pirondini of iFixit (https://github.com/andrewjpiro)",
"Rob Colburn <rob@robcolburn.com> (http://robcolburn.com/)", "Andrew Schaaf <andrew@andrewschaaf.com> (https://github.com/andrewschaaf)",
"Ryan Stafford (http://droffats.net/)" "Ayman Nedjmeddine (https://github.com/IOAyman)",
"Cory Heslip (https://github.com/cheslip)",
"Douglas de Espindola (https://github.com/douglasep)",
"Forbes Lindesay (https://github.com/ForbesLindesay)",
"John Zwinck (https://github.com/jzwinck)",
"Joost Hietbrink (https://github.com/joost)",
"Kevin Jin (https://github.com/Kevin-Jin)",
"Michael J. (https://github.com/michaeljayt)",
"Peter deHaan (https://github.com/pdehaan)",
"Rob Colburn <rob@robcolburn.com> (https://github.com/robcolburn)",
"Ryan Regalado (https://github.com/d48)",
"Ryan Stafford (https://github.com/ryanstafford)",
"Sami Samhuri <sami@samhuri.net> (https://github.com/samsonjs)",
"Stian Grytøyr (https://github.com/stiang)",
"TJ Holowaychuk (https://github.com/tj)",
"w0den (https://github.com/w0den)",
"Yusuke Kawasaki (https://github.com/kawanet)",
"Aryan Arora (https://github.com/aryan-debug)"
],
"keywords": [
"strftime",
"format",
"string",
"time",
"date"
], ],
"repository": { "repository": {
"type": "git", "type": "git",
@ -22,12 +46,7 @@
"engines": { "engines": {
"node": ">=0.2.0" "node": ">=0.2.0"
}, },
"licenses": [ "license": "MIT",
{
"type": "MIT",
"url": "http://sjs.mit-license.org"
}
],
"dependencies": {}, "dependencies": {},
"devDependencies": {}, "devDependencies": {},
"optionalDependencies": {} "optionalDependencies": {}

29
strftime-min.js vendored
View file

@ -1,8 +1,21 @@
(function(){function k(c,a,b){return h(c,a,b)}function h(c,a,b,l){l=l||{};a&&!p(a)&&(b=a,a=void 0);a=a||new Date;b=b||q;b.formats=b.formats||{};var k=a.getTime(),i=l.timezone,d=typeof i;if(l.utc||d=="number"||d=="string")a=r(a);if(i){if(d=="string")var m=i[0]=="-"?-1:1,s=parseInt(i.slice(1,3),10),t=parseInt(i.slice(3,5),10),i=m*(60*s+t);d&&(a=new Date(a.getTime()+i*6E4))}return c.replace(/%([-_0:]?.)/g,function(c,d){var j,e,g;if(d.length==2){j=d[0];if(j=="-")e="";else if(j=="_")e=" ";else if(j=="0")e= (function(){function w(e,h,q){function l(g,a,c,f){for(var b="",d=null,r=!1,J=g.length,x=!1,v=0;v<J;v++){var n=g.charCodeAt(v);if(!0===r)if(45===n)d="";else if(95===n)d=" ";else if(48===n)d="0";else if(58===n)x&&A("[WARNING] detected use of unsupported %:: or %::: modifiers to strftime"),x=!0;else{switch(n){case 37:b+="%";break;case 65:b+=c.days[a.getDay()];break;case 66:b+=c.months[a.getMonth()];break;case 67:b+=k(Math.floor(a.getFullYear()/100),d);break;case 68:b+=l(c.formats.D,a,c,f);break;case 70:b+=
"0";else if(j==":")g=!0;else return c;d=d[1]}switch(d){case "A":return b.days[a.getDay()];case "a":return b.shortDays[a.getDay()];case "B":return b.months[a.getMonth()];case "b":return b.shortMonths[a.getMonth()];case "C":return f(Math.floor(a.getFullYear()/100),e);case "D":return h(b.formats.D||"%m/%d/%y",a,b);case "d":return f(a.getDate(),e);case "e":return f(a.getDate(),e==null?" ":e);case "F":return h(b.formats.F||"%Y-%m-%d",a,b);case "H":return f(a.getHours(),e);case "h":return b.shortMonths[a.getMonth()]; l(c.formats.F,a,c,f);break;case 72:b+=k(a.getHours(),d);break;case 73:b+=k(B(a.getHours()),d);break;case 76:b+=C(Math.floor(f%1E3));break;case 77:b+=k(a.getMinutes(),d);break;case 80:b+=12>a.getHours()?c.am:c.pm;break;case 82:b+=l(c.formats.R,a,c,f);break;case 83:b+=k(a.getSeconds(),d);break;case 84:b+=l(c.formats.T,a,c,f);break;case 85:b+=k(D(a,"sunday"),d);break;case 87:b+=k(D(a,"monday"),d);break;case 88:b+=l(c.formats.X,a,c,f);break;case 89:b+=a.getFullYear();break;case 90:t&&0===m?b+="GMT":(d=
case "I":return f(n(a),e);case "j":return g=new Date(a.getFullYear(),0,1),g=Math.ceil((a.getTime()-g.getTime())/864E5),f(g,3);case "k":return f(a.getHours(),e==null?" ":e);case "L":return f(Math.floor(k%1E3),3);case "l":return f(n(a),e==null?" ":e);case "M":return f(a.getMinutes(),e);case "m":return f(a.getMonth()+1,e);case "n":return"\n";case "o":return String(a.getDate())+u(a.getDate());case "P":return a.getHours()<12?b.am:b.pm;case "p":return a.getHours()<12?b.AM:b.PM;case "R":return h(b.formats.R|| a.toString().match(/\(([\w\s]+)\)/),b+=d&&d[1]||"");break;case 97:b+=c.shortDays[a.getDay()];break;case 98:b+=c.shortMonths[a.getMonth()];break;case 99:b+=l(c.formats.c,a,c,f);break;case 100:b+=k(a.getDate(),d);break;case 101:b+=k(a.getDate(),null==d?" ":d);break;case 104:b+=c.shortMonths[a.getMonth()];break;case 106:d=new Date(a.getFullYear(),0,1);d=Math.ceil((a.getTime()-d.getTime())/864E5);b+=C(d);break;case 107:b+=k(a.getHours(),null==d?" ":d);break;case 108:b+=k(B(a.getHours()),null==d?" ":d);
"%H:%M",a,b);case "r":return h(b.formats.r||"%I:%M:%S %p",a,b);case "S":return f(a.getSeconds(),e);case "s":return Math.floor(k/1E3);case "T":return h(b.formats.T||"%H:%M:%S",a,b);case "t":return"\t";case "U":return f(o(a,"sunday"),e);case "u":return g=a.getDay(),g==0?7:g;case "v":return h(b.formats.v||"%e-%b-%Y",a,b);case "W":return f(o(a,"monday"),e);case "w":return a.getDay();case "Y":return a.getFullYear();case "y":return g=String(a.getFullYear()),g.slice(g.length-2);case "Z":return l.utc?"GMT": break;case 109:b+=k(a.getMonth()+1,d);break;case 110:b+="\n";break;case 111:d=a.getDate();b=c.ordinalSuffixes?b+(String(d)+(c.ordinalSuffixes[d-1]||E(d))):b+(String(d)+E(d));break;case 112:b+=12>a.getHours()?c.AM:c.PM;break;case 114:b+=l(c.formats.r,a,c,f);break;case 115:b+=Math.floor(f/1E3);break;case 116:b+="\t";break;case 117:d=a.getDay();b+=0===d?7:d;break;case 118:b+=l(c.formats.v,a,c,f);break;case 119:b+=a.getDay();break;case 120:b+=l(c.formats.x,a,c,f);break;case 121:b+=k(a.getFullYear()%100,
(g=a.toString().match(/\(([\w\s]+)\)/))&&g[1]||"";case "z":return l.utc?g?"+00:00":"+0000":(j=typeof i=="number"?i:-a.getTimezoneOffset(),(j<0?"-":"+")+f(Math.floor(Math.abs(j)/60))+(g?":":"")+f(Math.abs(j)%60));default:return d}})}function r(c){var a=c.getUTCFullYear(),c=new Date(a,c.getUTCMonth(),c.getUTCDate(),c.getUTCHours(),c.getUTCMinutes(),c.getUTCSeconds(),c.getUTCMilliseconds());c.getFullYear()!=a&&c.setFullYear(a);return c}function p(c){for(var a=0,b=m.length,a=0;a<b;++a)if(typeof c[m[a]]!= d);break;case 122:t&&0===m?b+=x?"+00:00":"+0000":(d=0!==m?m/6E4:-a.getTimezoneOffset(),r=x?":":"",n=Math.abs(d%60),b+=(0>d?"-":"+")+k(Math.floor(Math.abs(d/60)))+r+k(n));break;default:r&&(b+="%"),b+=g[v]}d=null;r=!1}else 37===n?r=!0:b+=g[v]}return b}var y=e||F,m=h||0,t=q||!1,u=0,z,p=function(g,a){if(a){var c=a.getTime();if(t){var f=6E4*(a.getTimezoneOffset()||0);a=new Date(c+f+m);6E4*(a.getTimezoneOffset()||0)!==f&&(a=6E4*(a.getTimezoneOffset()||0),a=new Date(c+a+m))}}else c=Date.now(),c>u?(u=c,z=
"function")return!1;return!0}function f(c,a,b){typeof a==="number"&&(b=a,a="0");a==null&&(a="0");b=b||2;c=String(c);if(a)for(;c.length<b;)c=a+c;return c}function n(c){c=c.getHours();c==0?c=12:c>12&&(c-=12);return c}function u(c){var a=c%10;c%=100;if(c>=11&&c<=13||a===0||a>=4)return"th";switch(a){case 1:return"st";case 2:return"nd";case 3:return"rd"}}function o(c,a){var a=a||"sunday",b=c.getDay();a=="monday"&&(b==0?b=6:b--);var d=new Date(c.getFullYear(),0,1);return Math.floor(((c-d)/864E5+7-b)/7)} new Date(u),c=u,t&&(z=new Date(u+6E4*(z.getTimezoneOffset()||0)+m))):c=u,a=z;return l(g,a,y,c)};p.localize=function(g){return new w(g||y,m,t)};p.localizeByIdentifier=function(g){var a=G[g];return a?p.localize(a):(A('[WARNING] No locale found with identifier "'+g+'".'),p)};p.timezone=function(g){var a=m,c=t,f=typeof g;if("number"===f||"string"===f)c=!0,"string"===f?(a="-"===g[0]?-1:1,f=parseInt(g.slice(1,3),10),g=parseInt(g.slice(3,5),10),a=a*(60*f+g)*6E4):"number"===f&&(a=6E4*g);return new w(y,a,
var d;d=typeof module!=="undefined"?module.exports=k:function(){return this||(0,eval)("this")}();var q={days:"Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),shortDays:"Sun Mon Tue Wed Thu Fri Sat".split(" "),months:"January February March April May June July August September October November December".split(" "),shortMonths:"Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" "),AM:"AM",PM:"PM",am:"am",pm:"pm"};d.strftime=k;d.strftimeTZ=k.strftimeTZ=function(c,a,b,d){if((typeof b== c)};p.utc=function(){return new w(y,m,!0)};return p}function k(e,h){if(""===h||9<e)return""+e;null==h&&(h="0");return h+e}function C(e){return 99<e?e:9<e?"0"+e:"00"+e}function B(e){return 0===e?12:12<e?e-12:e}function D(e,h){h=h||"sunday";var q=e.getDay();"monday"===h&&(0===q?q=6:q--);h=Date.UTC(e.getFullYear(),0,1);e=Date.UTC(e.getFullYear(),e.getMonth(),e.getDate());return Math.floor((Math.floor((e-h)/864E5)+7-q)/7)}function E(e){var h=e%10;e%=100;if(11<=e&&13>=e||0===h||4<=h)return"th";switch(h){case 1:return"st";
"number"||typeof b=="string")&&d==null)d=b,b=void 0;return h(c,a,b,{timezone:d})};d.strftimeUTC=k.strftimeUTC=function(c,a,b){return h(c,a,b,{utc:!0})};d.localizedStrftime=k.localizedStrftime=function(c){return function(a,b){return h(a,b,c)}};var m=["getTime","getTimezoneOffset","getDay","getDate","getMonth","getFullYear","getYear","getHours","getMinutes","getSeconds"]})(); case 2:return"nd";case 3:return"rd"}}function A(e){"undefined"!==typeof console&&"function"==typeof console.warn&&console.warn(e)}var G={de_DE:{identifier:"de-DE",days:"Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag".split(" "),shortDays:"So Mo Di Mi Do Fr Sa".split(" "),months:"Januar Februar M\u00e4rz April Mai Juni Juli August September Oktober November Dezember".split(" "),shortMonths:"Jan Feb M\u00e4r Apr Mai Jun Jul Aug Sep Okt Nov Dez".split(" "),AM:"AM",PM:"PM",am:"am",pm:"pm",
formats:{c:"%a %d %b %Y %X %Z",D:"%d.%m.%Y",F:"%Y-%m-%d",R:"%H:%M",r:"%I:%M:%S %p",T:"%H:%M:%S",v:"%e-%b-%Y",X:"%T",x:"%D"}},en_CA:{identifier:"en-CA",days:"Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),shortDays:"Sun Mon Tue Wed Thu Fri Sat".split(" "),months:"January February March April May June July August September October November December".split(" "),shortMonths:"Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" "),ordinalSuffixes:"st nd rd th th th th th th th th th th th th th th th th th st nd rd th th th th th th th st".split(" "),
AM:"AM",PM:"PM",am:"am",pm:"pm",formats:{c:"%a %d %b %Y %X %Z",D:"%d/%m/%y",F:"%Y-%m-%d",R:"%H:%M",r:"%I:%M:%S %p",T:"%H:%M:%S",v:"%e-%b-%Y",X:"%r",x:"%D"}},en_US:{identifier:"en-US",days:"Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),shortDays:"Sun Mon Tue Wed Thu Fri Sat".split(" "),months:"January February March April May June July August September October November December".split(" "),shortMonths:"Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" "),ordinalSuffixes:"st nd rd th th th th th th th th th th th th th th th th th st nd rd th th th th th th th st".split(" "),
AM:"AM",PM:"PM",am:"am",pm:"pm",formats:{c:"%a %d %b %Y %X %Z",D:"%m/%d/%y",F:"%Y-%m-%d",R:"%H:%M",r:"%I:%M:%S %p",T:"%H:%M:%S",v:"%e-%b-%Y",X:"%r",x:"%D"}},es_MX:{identifier:"es-MX",days:"domingo lunes martes mi\u00e9rcoles jueves viernes s\u00e1bado".split(" "),shortDays:"dom lun mar mi\u00e9 jue vie s\u00e1b".split(" "),months:"enero febrero marzo abril mayo junio julio agosto septiembre octubre noviembre diciembre".split(" "),shortMonths:"ene feb mar abr may jun jul ago sep oct nov dic".split(" "),
AM:"AM",PM:"PM",am:"am",pm:"pm",formats:{c:"%a %d %b %Y %X %Z",D:"%d/%m/%Y",F:"%Y-%m-%d",R:"%H:%M",r:"%I:%M:%S %p",T:"%H:%M:%S",v:"%e-%b-%Y",X:"%T",x:"%D"}},fr_FR:{identifier:"fr-FR",days:"dimanche lundi mardi mercredi jeudi vendredi samedi".split(" "),shortDays:"dim. lun. mar. mer. jeu. ven. sam.".split(" "),months:"janvier f\u00e9vrier mars avril mai juin juillet ao\u00fbt septembre octobre novembre d\u00e9cembre".split(" "),shortMonths:"janv. f\u00e9vr. mars avril mai juin juil. ao\u00fbt sept. oct. nov. d\u00e9c.".split(" "),
AM:"AM",PM:"PM",am:"am",pm:"pm",formats:{c:"%a %d %b %Y %X %Z",D:"%d/%m/%Y",F:"%Y-%m-%d",R:"%H:%M",r:"%I:%M:%S %p",T:"%H:%M:%S",v:"%e-%b-%Y",X:"%T",x:"%D"}},it_IT:{identifier:"it-IT",days:"domenica luned\u00ec marted\u00ec mercoled\u00ec gioved\u00ec venerd\u00ec sabato".split(" "),shortDays:"dom lun mar mer gio ven sab".split(" "),months:"gennaio febbraio marzo aprile maggio giugno luglio agosto settembre ottobre novembre dicembre".split(" "),shortMonths:"gen feb mar apr mag giu lug ago set ott nov dic".split(" "),
AM:"AM",PM:"PM",am:"am",pm:"pm",formats:{c:"%a %d %b %Y %X %Z",D:"%d/%m/%Y",F:"%Y-%m-%d",R:"%H:%M",r:"%I:%M:%S %p",T:"%H:%M:%S",v:"%e-%b-%Y",X:"%T",x:"%D"}},nl_NL:{identifier:"nl-NL",days:"zondag maandag dinsdag woensdag donderdag vrijdag zaterdag".split(" "),shortDays:"zo ma di wo do vr za".split(" "),months:"januari februari maart april mei juni juli augustus september oktober november december".split(" "),shortMonths:"jan feb mrt apr mei jun jul aug sep okt nov dec".split(" "),AM:"AM",PM:"PM",
am:"am",pm:"pm",formats:{c:"%a %d %b %Y %X %Z",D:"%d-%m-%y",F:"%Y-%m-%d",R:"%H:%M",r:"%I:%M:%S %p",T:"%H:%M:%S",v:"%e-%b-%Y",X:"%T",x:"%D"}},pt_BR:{identifier:"pt-BR",days:"domingo segunda ter\u00e7a quarta quinta sexta s\u00e1bado".split(" "),shortDays:"Dom Seg Ter Qua Qui Sex S\u00e1b".split(" "),months:"janeiro fevereiro mar\u00e7o abril maio junho julho agosto setembro outubro novembro dezembro".split(" "),shortMonths:"Jan Fev Mar Abr Mai Jun Jul Ago Set Out Nov Dez".split(" "),AM:"AM",PM:"PM",
am:"am",pm:"pm",formats:{c:"%a %d %b %Y %X %Z",D:"%d-%m-%Y",F:"%Y-%m-%d",R:"%H:%M",r:"%I:%M:%S %p",T:"%H:%M:%S",v:"%e-%b-%Y",X:"%T",x:"%D"}},ru_RU:{identifier:"ru-RU",days:"\u0412\u043e\u0441\u043a\u0440\u0435\u0441\u0435\u043d\u044c\u0435 \u041f\u043e\u043d\u0435\u0434\u0435\u043b\u044c\u043d\u0438\u043a \u0412\u0442\u043e\u0440\u043d\u0438\u043a \u0421\u0440\u0435\u0434\u0430 \u0427\u0435\u0442\u0432\u0435\u0440\u0433 \u041f\u044f\u0442\u043d\u0438\u0446\u0430 \u0421\u0443\u0431\u0431\u043e\u0442\u0430".split(" "),
shortDays:"\u0412\u0441 \u041f\u043d \u0412\u0442 \u0421\u0440 \u0427\u0442 \u041f\u0442 \u0421\u0431".split(" "),months:"\u042f\u043d\u0432\u0430\u0440\u044c \u0424\u0435\u0432\u0440\u0430\u043b\u044c \u041c\u0430\u0440\u0442 \u0410\u043f\u0440\u0435\u043b\u044c \u041c\u0430\u0439 \u0418\u044e\u043d\u044c \u0418\u044e\u043b\u044c \u0410\u0432\u0433\u0443\u0441\u0442 \u0421\u0435\u043d\u0442\u044f\u0431\u0440\u044c \u041e\u043a\u0442\u044f\u0431\u0440\u044c \u041d\u043e\u044f\u0431\u0440\u044c \u0414\u0435\u043a\u0430\u0431\u0440\u044c".split(" "),
shortMonths:"\u044f\u043d\u0432 \u0444\u0435\u0432 \u043c\u0430\u0440 \u0430\u043f\u0440 \u043c\u0430\u0439 \u0438\u044e\u043d \u0438\u044e\u043b \u0430\u0432\u0433 \u0441\u0435\u043d \u043e\u043a\u0442 \u043d\u043e\u044f \u0434\u0435\u043a".split(" "),AM:"AM",PM:"PM",am:"am",pm:"pm",formats:{c:"%a %d %b %Y %X",D:"%d.%m.%y",F:"%Y-%m-%d",R:"%H:%M",r:"%I:%M:%S %p",T:"%H:%M:%S",v:"%e-%b-%Y",X:"%T",x:"%D"}},tr_TR:{identifier:"tr-TR",days:"Pazar Pazartesi Sal\u0131 \u00c7ar\u015famba Per\u015fembe Cuma Cumartesi".split(" "),
shortDays:"Paz Pzt Sal \u00c7r\u015f Pr\u015f Cum Cts".split(" "),months:"Ocak \u015eubat Mart Nisan May\u0131s Haziran Temmuz A\u011fustos Eyl\u00fcl Ekim Kas\u0131m Aral\u0131k".split(" "),shortMonths:"Oca \u015eub Mar Nis May Haz Tem A\u011fu Eyl Eki Kas Ara".split(" "),AM:"\u00d6\u00d6",PM:"\u00d6S",am:"\u00d6\u00d6",pm:"\u00d6S",formats:{c:"%a %d %b %Y %X %Z",D:"%d-%m-%Y",F:"%Y-%m-%d",R:"%H:%M",r:"%I:%M:%S %p",T:"%H:%M:%S",v:"%e-%b-%Y",X:"%T",x:"%D"}},zh_CN:{identifier:"zh-CN",days:"\u661f\u671f\u65e5 \u661f\u671f\u4e00 \u661f\u671f\u4e8c \u661f\u671f\u4e09 \u661f\u671f\u56db \u661f\u671f\u4e94 \u661f\u671f\u516d".split(" "),
shortDays:"\u65e5\u4e00\u4e8c\u4e09\u56db\u4e94\u516d".split(""),months:"\u4e00\u6708 \u4e8c\u6708 \u4e09\u6708 \u56db\u6708 \u4e94\u6708 \u516d\u6708 \u4e03\u6708 \u516b\u6708 \u4e5d\u6708 \u5341\u6708 \u5341\u4e00\u6708 \u5341\u4e8c\u6708".split(" "),shortMonths:"\u4e00\u6708 \u4e8c\u6708 \u4e09\u6708 \u56db\u6708 \u4e94\u6708 \u516d\u6708 \u4e03\u6708 \u516b\u6708 \u4e5d\u6708 \u5341\u6708 \u5341\u4e00\u6708 \u5341\u4e8c\u6708".split(" "),AM:"\u4e0a\u5348",PM:"\u4e0b\u5348",am:"\u4e0a\u5348",pm:"\u4e0b\u5348",
formats:{c:"%a %d %b %Y %X %Z",D:"%d/%m/%y",F:"%Y-%m-%d",R:"%H:%M",r:"%I:%M:%S %p",T:"%H:%M:%S",v:"%e-%b-%Y",X:"%r",x:"%D"}}},F=G.en_US,H=new w(F,0,!1);if("undefined"!==typeof module)var I=module.exports=H;else I=function(){return this||(0,eval)("this")}(),I.strftime=H;"function"!==typeof Date.now&&(Date.now=function(){return+new Date})})();

File diff suppressed because it is too large Load diff

271
test.js Executable file
View file

@ -0,0 +1,271 @@
#!/usr/bin/env node
// Based on CoffeeScript by andrewschaaf on github
//
// TODO:
// - past and future dates, especially < 1900 and > 2100
// - look for edge cases
var assert = require('assert'),
libFilename = process.argv[2] || './strftime.js',
strftime = require(libFilename),
strftimeUTC = strftime.utc(),
Time = new Date(1307472705067); // Tue, 07 Jun 2011 18:51:45 GMT
assert.fn = function(value, msg) {
assert.equal(typeof value, 'function', msg);
};
function assertFormat(time, format, expected, name, strftime) {
var actual = strftime(format, time);
assert.equal(actual, expected, name + '("' + format + '", ' + time + ') is ' + JSON.stringify(actual) + ', expected ' + JSON.stringify(expected));
}
assert.format = function(format, expected, expectedUTC, time) {
time = time || Time;
if (expected) { assertFormat(time, format, expected, 'strftime', strftime); }
assertFormat(time, format, expectedUTC || expected, 'strftime.utc()', strftimeUTC);
};
/// check exports
assert.fn(strftime.localize);
assert.fn(strftime.timezone);
assert.fn(strftime.utc);
ok('Exports');
/// time zones
if (process.env.TZ == 'America/Vancouver') {
testTimezone('America/Vancouver');
assert.format('%C', '01', '01', new Date(100, 0, 1));
assert.format('%X', '11:51:45 AM', '06:51:45 PM');
assert.format('%c', 'Tue 07 Jun 2011 11:51:45 AM Pacific Daylight Saving Time', 'Tue 07 Jun 2011 06:51:45 PM GMT');
assert.format('%j', '097', '098', new Date(1365390736236));
assert.format('%x', '06/07/11');
assert.format('%U', '12', null, new Date('2017-03-25 00:00:00 +0000'));
assert.format('%U', '12', '13', new Date('2017-03-26 00:00:00 +0000'));
assert.format('%U', '13', null, new Date('2017-03-27 00:00:00 +0000'));
assert.format('%U', '13', '14', new Date('2017-04-02 00:00:00 +0000'));
// Test dates crossing a DST boundary.
var dstStart = +new Date('2016-11-06 01:50:00');
assert.format('%T', '01:50:00', '08:50:00', new Date(dstStart))
assert.format('%T', '01:00:00', '09:00:00', new Date(dstStart + 10 * 60 * 1000))
ok('Time zones (' + process.env.TZ + ')');
}
else if (process.env.TZ == 'Europe/Amsterdam') {
testTimezone('Europe/Amsterdam');
assert.format('%C', '01', '00', new Date(100, 0, 1));
assert.format('%X', '08:51:45 PM', '06:51:45 PM');
assert.format('%c', 'Tue 07 Jun 2011 08:51:45 PM Central European Summer Time', 'Tue 07 Jun 2011 06:51:45 PM GMT');
assert.format('%j', '098', '098', new Date(1365390736236));
assert.format('%x', '06/07/11');
assert.format('%U', '12', null, new Date('2017-03-25 00:00:00 +0000'));
assert.format('%U', '13', null, new Date('2017-03-26 00:00:00 +0000'));
assert.format('%U', '13', null, new Date('2017-03-27 00:00:00 +0000'));
assert.format('%U', '14', null, new Date('2017-04-02 00:00:00 +0000'));
// Test dates crossing a DST boundary.
var dstStart = +new Date('2016-10-30 02:50:00');
assert.format('%T', '02:50:00', '00:50:00', new Date(dstStart))
assert.format('%T', '02:00:00', '01:00:00', new Date(dstStart + 10 * 60 * 1000))
ok('Time zones (' + process.env.TZ + ')');
}
else {
assert(false, '(Current timezone has no tests: ' + (process.env.TZ || 'none') + ')');
}
/// check all formats in GMT, most coverage
assert.format('%A', 'Tuesday');
assert.format('%a', 'Tue');
assert.format('%B', 'June');
assert.format('%b', 'Jun');
assert.format('%C', '20');
assert.format('%D', '06/07/11');
assert.format('%d', '07');
assert.format('%-d', '7');
assert.format('%_d', ' 7');
assert.format('%0d', '07');
assert.format('%e', ' 7');
assert.format('%F', '2011-06-07');
assert.format('%H', null, '18');
assert.format('%h', 'Jun');
assert.format('%I', null, '06');
assert.format('%-I', null, '6');
assert.format('%_I', null, ' 6');
assert.format('%0I', null, '06');
assert.format('%j', null, '158');
assert.format('%k', null, '18');
assert.format('%L', '067');
assert.format('%l', null, ' 6');
assert.format('%-l', null, '6');
assert.format("%-y", "1", null, new Date('2001-02-03T04:05:06'))
assert.format("%-y", "23", null, new Date('2023-02-03T04:05:06'))
assert.format('%_l', null, ' 6');
assert.format('%0l', null, '06');
assert.format('%M', null, '51');
assert.format('%m', '06');
assert.format('%n', '\n');
assert.format('%o', '7th');
assert.format('%P', null, 'pm');
assert.format('%p', null, 'PM');
assert.format('%R', null, '18:51');
assert.format('%r', null, '06:51:45 PM');
assert.format('%S', '45');
assert.format('%s', '1307472705');
assert.format('%T', null, '18:51:45');
assert.format('%t', '\t');
assert.format('%U', '23');
assert.format('%U', '24', null, new Date(+Time + 5 * 86400000));
assert.format('%u', '2');
assert.format('%v', ' 7-Jun-2011');
assert.format('%W', '23');
assert.format('%W', '23', null, new Date(+Time + 5 * 86400000));
assert.format('%w', '2');
assert.format('%Y', '2011');
assert.format('%y', '11');
assert.format('%Z', null, 'GMT');
assert.format('%z', null, '+0000');
assert.format('%:z', null, '+00:00');
assert.format('%%', '%'); // literal percent sign
assert.format('%g', '%g'); // unrecognized directive
assert.format('%F %T', null, '1970-01-01 00:00:00', new Date(0));
assert.format('%U', null, '12', new Date('2017-03-25 00:00:00 +0000'));
assert.format('%U', null, '13', new Date('2017-03-26 00:00:00 +0000'));
assert.format('%U', null, '13', new Date('2017-03-27 00:00:00 +0000'));
assert.format('%U', null, '14', new Date('2017-04-02 00:00:00 +0000'));
ok('GMT');
/// locales
var it_IT = {
days: words('domenica lunedi martedi mercoledi giovedi venerdi sabato'),
shortDays: words('dom lun mar mer gio ven sab'),
months: words('gennaio febbraio marzo aprile maggio giugno luglio agosto settembre ottobre novembre dicembre'),
shortMonths: words('gen feb mar apr mag giu lug ago set ott nov dic'),
AM: 'it$AM',
PM: 'it$PM',
am: 'it$am',
pm: 'it$pm',
formats: {
D: 'it$%m/%d/%y',
F: 'it$%Y-%m-%d',
R: 'it$%H:%M',
T: 'it$%H:%M:%S',
X: '%T',
c: '%a %b %d %X %Y',
r: 'it$%I:%M:%S %p',
v: 'it$%e-%b-%Y',
x: '%D'
}
};
var strftimeIT = strftime.localize(it_IT),
strftimeITUTC = strftimeIT.utc();
assert.format_it = function(format, expected, expectedUTC) {
if (expected) { assertFormat(Time, format, expected, 'strftime.localize(it_IT)', strftimeIT); }
assertFormat(Time, format, expectedUTC || expected, 'strftime.localize(it_IT).utc()', strftimeITUTC);
};
assert.format_it('%A', 'martedi');
assert.format_it('%a', 'mar');
assert.format_it('%B', 'giugno');
assert.format_it('%b', 'giu');
assert.format_it('%c', null, 'mar giu 07 it$18:51:45 2011');
assert.format_it('%D', 'it$06/07/11');
assert.format_it('%F', 'it$2011-06-07');
assert.format_it('%p', null, 'it$PM');
assert.format_it('%P', null, 'it$pm');
assert.format_it('%R', null, 'it$18:51');
assert.format_it('%r', null, 'it$06:51:45 it$PM');
assert.format_it('%T', null, 'it$18:51:45');
assert.format_it('%v', 'it$ 7-giu-2011');
assert.format_it('%x', null, 'it$06/07/11');
assert.format_it('%X', null, 'it$18:51:45');
ok('Localization');
/// timezones
assert.formatTZ = function(format, expected, tz, time) {
assertFormat(time || Time, format, expected, 'strftime.timezone(' + tz + ')', strftime.timezone(tz));
};
assert.formatTZ('%F %r %z', '2011-06-07 06:51:45 PM +0000', 0);
assert.formatTZ('%F %r %z', '2011-06-07 06:51:45 PM +0000', '+0000');
assert.formatTZ('%F %r %z', '2011-06-07 08:51:45 PM +0200', 120);
assert.formatTZ('%F %r %z', '2011-06-07 08:51:45 PM +0200', '+0200');
assert.formatTZ('%F %r %z', '2011-06-07 11:51:45 AM -0700', -420);
assert.formatTZ('%F %r %z', '2011-06-07 11:51:45 AM -0700', '-0700');
assert.formatTZ('%F %r %z', '2011-06-07 11:21:45 AM -0730', '-0730');
assert.formatTZ('%F %r %:z', '2011-06-07 11:21:45 AM -07:30', '-0730');
ok('Time zone offset');
/// caching
(function() {
// this test fails when the 2 calls cross a millisecond boundary, so try a number of times
var CacheAttempts = 10;
var MaxFailures = 1;
var failures = 0;
for (var i = 0; i < CacheAttempts; ++i) {
var expectedMillis = strftime('%L');
var millis = strftime('%L');
if (expectedMillis != millis) {
++failures;
}
}
if (failures > MaxFailures) {
assert.fail('timestamp caching appears to be broken (' + failures + ' failed attempts out of ' + CacheAttempts + ')');
}
}());
ok('Cached timestamps');
/// helpers
function words(s) { return (s || '').split(' '); }
function ok(s) { console.log('[ \033[32mOK\033[0m ] ' + s); }
// Pass a regex or string that matches the timezone abbrev, e.g. %Z above.
// Don't pass GMT! Every date includes it and it will fail.
// Be careful if you pass a regex, it has to quack like the default one.
function testTimezone(regex) {
regex = typeof regex === 'string' ? RegExp('\\((' + regex + ')\\)$') : regex;
var match = Time.toString().match(regex);
if (match) {
var off = Time.getTimezoneOffset(),
hourOff = off / 60,
hourDiff = Math.floor(hourOff),
hours = 18 - hourDiff,
padSpace24 = hours < 10 ? ' ' : '',
padZero24 = hours < 10 ? '0' : '',
hour24 = String(hours),
padSpace12 = (hours % 12) < 10 ? ' ' : '',
padZero12 = (hours % 12) < 10 ? '0' : '',
hour12 = String(hours % 12),
sign = hourDiff < 0 ? '+' : '-',
minDiff = Time.getTimezoneOffset() - (hourDiff * 60),
mins = String(51 - minDiff),
tz = match[1],
ampm = hour12 == hour24 ? 'AM' : 'PM',
R = hour24 + ':' + mins,
r = padZero12 + hour12 + ':' + mins + ':45 ' + ampm,
T = R + ':45';
assert.format('%H', padZero24 + hour24, '18');
assert.format('%I', padZero12 + hour12, '06');
assert.format('%k', padSpace24 + hour24, '18');
assert.format('%l', padSpace12 + hour12, ' 6');
assert.format('%M', mins);
assert.format('%P', ampm.toLowerCase(), 'pm');
assert.format('%p', ampm, 'PM');
assert.format('%R', R, '18:51');
assert.format('%r', r, '06:51:45 PM');
assert.format('%T', T, '18:51:45');
assert.format('%Z', tz, 'GMT');
assert.format('%z', sign + '0' + Math.abs(hourDiff) + '00', '+0000');
assert.format('%:z', sign + '0' + Math.abs(hourDiff) + ':00', '+00:00');
}
}

View file

@ -1,228 +0,0 @@
#!/usr/bin/env node
// Based on CoffeeScript by andrewschaaf on github
//
// TODO:
// - past and future dates, especially < 1900 and > 2100
// - look for edge cases
var assert = require('assert')
, libFilename = process.argv[2] || '../strftime.js'
, lib = require(libFilename)
// Tue, 07 Jun 2011 18:51:45 GMT
, TestTime = new Date(1307472705067)
assert.fn = function(value, msg) {
assert.equal('function', typeof value, msg)
}
assert.format = function(format, expected, expectedUTC, time) {
time = time || TestTime
function _assertFmt(expected, name) {
name = name || 'strftime'
var actual = lib[name](format, time)
assert.equal(expected, actual,
name + '("' + format + '", ' + time + ') is ' + JSON.stringify(actual)
+ ', expected ' + JSON.stringify(expected))
}
if (expected) _assertFmt(expected, 'strftime')
_assertFmt(expectedUTC || expected, 'strftimeUTC')
}
/// check exports
assert.fn(lib.strftime)
assert.fn(lib.strftimeUTC)
assert.fn(lib.localizedStrftime)
ok('Exports')
/// time zones
if (process.env.TZ == 'America/Vancouver') {
testTimezone('P[DS]T')
assert.format('%C', '01', '01', new Date(100, 0, 1))
assert.format('%j', '097', '098', new Date(1365390736236))
ok('Time zones (' + process.env.TZ + ')')
}
else if (process.env.TZ == 'CET') {
testTimezone('CES?T')
assert.format('%C', '01', '00', new Date(100, 0, 1))
assert.format('%j', '098', '098', new Date(1365390736236))
ok('Time zones (' + process.env.TZ + ')')
}
else {
console.log('(Current timezone has no tests: ' + (process.env.TZ || 'none') + ')')
}
/// check all formats in GMT, most coverage
assert.format('%A', 'Tuesday')
assert.format('%a', 'Tue')
assert.format('%B', 'June')
assert.format('%b', 'Jun')
assert.format('%C', '20')
assert.format('%D', '06/07/11')
assert.format('%d', '07')
assert.format('%-d', '7')
assert.format('%_d', ' 7')
assert.format('%0d', '07')
assert.format('%e', ' 7')
assert.format('%F', '2011-06-07')
assert.format('%H', null, '18')
assert.format('%h', 'Jun')
assert.format('%I', null, '06')
assert.format('%-I', null, '6')
assert.format('%_I', null, ' 6')
assert.format('%0I', null, '06')
assert.format('%j', null, '158')
assert.format('%k', null, '18')
assert.format('%L', '067')
assert.format('%l', null, ' 6')
assert.format('%-l', null, '6')
assert.format('%_l', null, ' 6')
assert.format('%0l', null, '06')
assert.format('%M', null, '51')
assert.format('%m', '06')
assert.format('%n', '\n')
assert.format('%o', '7th')
assert.format('%P', null, 'pm')
assert.format('%p', null, 'PM')
assert.format('%R', null, '18:51')
assert.format('%r', null, '06:51:45 PM')
assert.format('%S', '45')
assert.format('%s', '1307472705')
assert.format('%T', null, '18:51:45')
assert.format('%t', '\t')
assert.format('%U', '23')
assert.format('%U', '24', null, new Date(+TestTime + 5 * 86400000))
assert.format('%u', '2')
assert.format('%v', ' 7-Jun-2011')
assert.format('%W', '23')
assert.format('%W', '23', null, new Date(+TestTime + 5 * 86400000))
assert.format('%w', '2')
assert.format('%Y', '2011')
assert.format('%y', '11')
assert.format('%Z', null, 'GMT')
assert.format('%z', null, '+0000')
assert.format('%:z', null, '+00:00')
assert.format('%%', '%') // any other char
assert.format('%F %T', null, '1970-01-01 00:00:00', new Date(0))
ok('GMT')
/// locales
var it_IT =
{ days: words('domenica lunedi martedi mercoledi giovedi venerdi sabato')
, shortDays: words('dom lun mar mer gio ven sab')
, months: words('gennaio febbraio marzo aprile maggio giugno luglio agosto settembre ottobre novembre dicembre')
, shortMonths: words('gen feb mar apr mag giu lug ago set ott nov dic')
, AM: 'it$AM'
, PM: 'it$PM'
, am: 'it$am'
, pm: 'it$pm'
, formats: {
D: 'it$%m/%d/%y'
, F: 'it$%Y-%m-%d'
, R: 'it$%H:%M'
, r: 'it$%I:%M:%S %p'
, T: 'it$%H:%M:%S'
, v: 'it$%e-%b-%Y'
}
}
assert.format_it = function(format, expected, expectedUTC) {
function _assertFmt(expected, name) {
name = name || 'strftime'
var actual = lib[name](format, TestTime, it_IT)
assert.equal(expected, actual,
name + '("' + format + '", Time) is ' + JSON.stringify(actual)
+ ', expected ' + JSON.stringify(expected))
}
if (expected) _assertFmt(expected, 'strftime')
_assertFmt(expectedUTC || expected, 'strftimeUTC')
}
assert.format_it('%A', 'martedi')
assert.format_it('%a', 'mar')
assert.format_it('%B', 'giugno')
assert.format_it('%b', 'giu')
assert.format_it('%D', 'it$06/07/11')
assert.format_it('%F', 'it$2011-06-07')
assert.format_it('%p', null, 'it$PM')
assert.format_it('%P', null, 'it$pm')
assert.format_it('%R', null, 'it$18:51')
assert.format_it('%r', null, 'it$06:51:45 it$PM')
assert.format_it('%T', null, 'it$18:51:45')
assert.format_it('%v', 'it$ 7-giu-2011')
ok('Localization')
/// timezones
assert.formatTZ = function(format, expected, tz, time) {
time = time || TestTime;
var actual = lib.strftimeTZ(format, time, tz)
assert.equal(
expected, actual,
('strftime("' + format + '", ' + time + ') is ' + JSON.stringify(actual) + ', expected ' + JSON.stringify(expected))
)
}
assert.formatTZ('%F %r %z', '2011-06-07 06:51:45 PM +0000', 0)
assert.formatTZ('%F %r %z', '2011-06-07 06:51:45 PM +0000', '+0000')
assert.formatTZ('%F %r %z', '2011-06-07 08:51:45 PM +0200', 120)
assert.formatTZ('%F %r %z', '2011-06-07 08:51:45 PM +0200', '+0200')
assert.formatTZ('%F %r %z', '2011-06-07 11:51:45 AM -0700', -420)
assert.formatTZ('%F %r %z', '2011-06-07 11:51:45 AM -0700', '-0700')
assert.formatTZ('%F %r %z', '2011-06-07 11:21:45 AM -0730', '-0730')
assert.formatTZ('%F %r %:z', '2011-06-07 11:21:45 AM -07:30', '-0730')
ok('Time zone offset')
/// helpers
function words(s) { return (s || '').split(' '); }
function ok(s) { console.log('[ \033[32mOK\033[0m ] ' + s) }
// Pass a regex or string that matches the timezone abbrev, e.g. %Z above.
// Don't pass GMT! Every date includes it and it will fail.
// Be careful if you pass a regex, it has to quack like the default one.
function testTimezone(regex) {
regex = typeof regex === 'string' ? RegExp('\\((' + regex + ')\\)$') : regex
var match = TestTime.toString().match(regex)
if (match) {
var off = TestTime.getTimezoneOffset()
, hourOff = off / 60
, hourDiff = Math.floor(hourOff)
, hours = 18 - hourDiff
, padSpace24 = hours < 10 ? ' ' : ''
, padZero24 = hours < 10 ? '0' : ''
, hour24 = String(hours)
, padSpace12 = (hours % 12) < 10 ? ' ' : ''
, padZero12 = (hours % 12) < 10 ? '0' : ''
, hour12 = String(hours % 12)
, sign = hourDiff < 0 ? '+' : '-'
, minDiff = TestTime.getTimezoneOffset() - (hourDiff * 60)
, mins = String(51 - minDiff)
, tz = match[1]
, ampm = hour12 == hour24 ? 'AM' : 'PM'
, R = hour24 + ':' + mins
, r = padZero12 + hour12 + ':' + mins + ':45 ' + ampm
, T = R + ':45'
assert.format('%H', padZero24 + hour24, '18')
assert.format('%I', padZero12 + hour12, '06')
assert.format('%k', padSpace24 + hour24, '18')
assert.format('%l', padSpace12 + hour12, ' 6')
assert.format('%M', mins)
assert.format('%P', ampm.toLowerCase(), 'pm')
assert.format('%p', ampm, 'PM')
assert.format('%R', R, '18:51')
assert.format('%r', r, '06:51:45 PM')
assert.format('%T', T, '18:51:45')
assert.format('%Z', tz, 'GMT')
assert.format('%z', sign + '0' + Math.abs(hourDiff) + '00', '+0000')
assert.format('%:z', sign + '0' + Math.abs(hourDiff) + ':00', '+00:00')
}
}