1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-08-08 23:03:50 +00:00

Add ability to now macro to return same UTC string used in date fields (#2884)

* Add ability to now macro to return same UTC string used in the modified and created fields.

* Revert "Add ability to now macro to return same UTC string used in the modified and created fields."

This reverts commit 7b6ad7db4ae3058d5cf0d8858cdb24d44224ef69.

* Mods to formatDateString to adjust displayed date to UTC for [UTC]
format. Corresponding doc changes, and optimization for special
case.
This commit is contained in:
Marxsal 2017-07-01 10:09:16 -07:00 committed by Jeremy Ruston
parent 1919f0e9b0
commit 4db950cc45
3 changed files with 24 additions and 2 deletions

View File

@ -267,6 +267,9 @@ exports.formatDateString = function(date,template) {
[/^0ss/, function() { [/^0ss/, function() {
return $tw.utils.pad(date.getSeconds()); return $tw.utils.pad(date.getSeconds());
}], }],
[/^0XXX/, function() {
return $tw.utils.pad(date.getMilliseconds());
}],
[/^0DD/, function() { [/^0DD/, function() {
return $tw.utils.pad(date.getDate()); return $tw.utils.pad(date.getDate());
}], }],
@ -308,6 +311,9 @@ exports.formatDateString = function(date,template) {
[/^ss/, function() { [/^ss/, function() {
return date.getSeconds(); return date.getSeconds();
}], }],
[/^XXX/, function() {
return date.getMilliseconds();
}],
[/^[AP]M/, function() { [/^[AP]M/, function() {
return $tw.utils.getAmPm(date).toUpperCase(); return $tw.utils.getAmPm(date).toUpperCase();
}], }],
@ -324,6 +330,16 @@ exports.formatDateString = function(date,template) {
return $tw.utils.pad(date.getFullYear() - 2000); return $tw.utils.pad(date.getFullYear() - 2000);
}] }]
]; ];
// If the user wants everything in UTC, shift the datestamp
// Optimize for format string that essentially means
// 'return raw UTC (tiddlywiki style) date string.'
if(t.indexOf("[UTC]") == 0 ) {
if(t == "[UTC]YYYY0MM0DD0hh0mm0ssXXX")
return $tw.utils.stringifyDate(new Date());
var offset = date.getTimezoneOffset() ; // in minutes
date = new Date(date.getTime()+offset*60*1000) ;
t = t.substr(5) ;
}
while(t.length){ while(t.length){
var matchString = ""; var matchString = "";
$tw.utils.each(matches, function(m) { $tw.utils.each(matches, function(m) {

View File

@ -1,5 +1,5 @@
created: 20140418142957325 created: 20140418142957325
modified: 20140912145809035 modified: 20170630223008226
tags: Features tags: Features
title: DateFormat title: DateFormat
type: text/vnd.tiddlywiki type: text/vnd.tiddlywiki
@ -30,10 +30,13 @@ The ViewWidget accepts a `template` attribute that allows the format of date val
|`0mm` |Minutes with leading zero | |`0mm` |Minutes with leading zero |
|`ss` |Seconds | |`ss` |Seconds |
|`0ss` |Seconds with leading zero | |`0ss` |Seconds with leading zero |
|`XXX` |Milliseconds |
|`0XXX` |Milliseconds with leading zero |
|`am` or `pm` |Lower case AM/PM indicator | |`am` or `pm` |Lower case AM/PM indicator |
|`AM` or `PM` |Upper case AM/PM indicator | |`AM` or `PM` |Upper case AM/PM indicator |
|`TZD` |Timezone offset | |`TZD` |Timezone offset |
|`\x` |Used to escape a character that would otherwise have special meaning | |`\x` |Used to escape a character that would otherwise have special meaning |
|`[UTC]`|Time-shift the represented date to UTC. Must be at very start of format string|
Note that other text is passed through unchanged, allowing commas, colons or other separators to be used. Note that other text is passed through unchanged, allowing commas, colons or other separators to be used.

View File

@ -1,6 +1,6 @@
caption: now caption: now
created: 20141008141616791 created: 20141008141616791
modified: 20150221170300000 modified: 20170630223406157
tags: Macros [[Core Macros]] tags: Macros [[Core Macros]]
title: now Macro title: now Macro
type: text/vnd.tiddlywiki type: text/vnd.tiddlywiki
@ -14,4 +14,7 @@ The value doesn't update automatically, like a ticking clock. It updates wheneve
;format ;format
: A string specifying the desired [[format|DateFormat]], defaulting to `0hh:0mm, DDth MMM YYYY` : A string specifying the desired [[format|DateFormat]], defaulting to `0hh:0mm, DDth MMM YYYY`
''Note'': The format string `[UTC]YYYY0MM0DD0hh0mm0ssXXX` will return a date string representing the UTC time-stamp as it is used in the ~TiddlyWiki `created` and `modified` time-stamp fields.
<<.macro-examples "now">> <<.macro-examples "now">>