Palindrome dates: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 10:
{{trans|Java}}
 
<langsyntaxhighlight lang="11l">V date = Time(2020, 2, 3)
print(‘First 15 palindrome dates after 2020-02-02 are:’)
V count = 0
Line 18:
print(‘date = ’date.format(‘YYYY-MM-DD’))
count++
date += TimeDelta(days' 1)</langsyntaxhighlight>
 
{{out}}
Line 41:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">TYPE Date=[
INT year
BYTE month
Line 131:
NextDay(d)
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Palindrome_dates.png Screenshot from Atari 8-bit computer]
Line 155:
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Uses the Algol 68G local time routine which is non-standard.
<langsyntaxhighlight lang="algol68">BEGIN # print future palindromic dates #
# a palindromic date must be of the form demn-nm-ed #
# returns a string representation of n with at least 2 digits #
Line 211:
OD
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 232:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
with Ada.Calendar.Formatting;
with Ada.Calendar.Arithmetic;
Line 265:
Date := Date + 1;
end loop;
end Palindrome_Dates;</langsyntaxhighlight>
 
=={{header|AppleScript}}==
===Procedural===
<langsyntaxhighlight lang="applescript">on palindromeDates(startYear, targetNumber)
script o
property output : {}
Line 300:
end palindromeDates
 
palindromeDates(2021, 15)</langsyntaxhighlight>
 
{{output}}
Line 307:
===Functional===
 
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
Line 423:
set my text item delimiters to dlm
str
end unlines</langsyntaxhighlight>
{{Out}}
<pre>Count of palindromic dates [2021..9999]: 284
Line 462:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">date := 20200202
counter := 0
 
Line 483:
output := v output
return output
}</langsyntaxhighlight>
{{out}}
<pre>
Line 504:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PALINDROME_DATES.AWK
BEGIN {
Line 537:
return(rts)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 563:
{{works with|QBasic}}
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="basic">
dateTest$ = ""
total = 0
Line 595:
NEXT anno
END
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 603:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="lb">
<lang lb>
dateTest = ""
mes = 0 : dia = 0 : anno = 0 : Pal = 0
Line 633:
next anno
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 641:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> INSTALL @lib$ + "DATELIB"
DIM B% 8
TestDate%=FN_today
Line 652:
TestDate%+=1
UNTIL VPOS=15
END</langsyntaxhighlight>
{{out}}
<pre>
Line 674:
=={{header|C}}==
This only works if time_t is a 64-bit type.
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <string.h>
Line 704:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 727:
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Collections.Generic;
Line 748:
bool IsValidDate(int y, int m, int d, out DateTime date) => DateTime.TryParse($"{y}-{m}-{d}", out date);
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 769:
=={{header|C++}}==
{{libheader|Boost}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <string>
#include <boost/date_time/gregorian/gregorian.hpp>
Line 797:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 820:
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">
<lang Clojure>
(defn valid-date? [[y m d]]
(and (<= 1 m 12)
Line 838:
(map date-str)
(take n)))
</syntaxhighlight>
</lang>
 
{{out}}
Line 846:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight Fsharplang="fsharp">// palindrome_dates.fsx
open System
 
Line 875:
|> Seq.take 15
|> Seq.iter print_date
</syntaxhighlight>
</lang>
{{out}}
<pre>> dotnet fsi palindrome_dates.fsx
Line 898:
A simple brute force solution that repeatedly increments a timestamp's day by one and checks whether it's a palindrome:
{{works with|Factor|0.99 2020-01-23}}
<langsyntaxhighlight lang="factor">USING: calendar calendar.format io kernel lists lists.lazy
sequences sets ;
 
Line 906:
[ "-" without dup reverse = ] lfilter ;
 
15 palindrome-dates ltake [ print ] leach</langsyntaxhighlight>
{{out}}
<pre>
Line 928:
A faster version that directly generates palindromic numbers such as <tt>20200202</tt> and keeps those which are valid dates:
{{works with|Factor|0.99 2020-01-23}}
<langsyntaxhighlight lang="factor">USING: calendar calendar.format continuations io kernel lists
lists.lazy math math.functions math.parser math.ranges sequences ;
 
Line 950:
 
"10,000th palindrome date after 2020-02-02: " write
10,000 palindrome-dates lnth timestamp>ymd print</langsyntaxhighlight>
{{out}}
<pre>
Line 958:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
Dim As String dateTest = ""
Dim As Integer Pal =0, total = 0
Line 987:
Next anno
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,011:
=={{header|Go}}==
Simple brute force as speed is not an issue here.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,043:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,064:
</pre>
Or, a more ambitious version.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,125:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 1,145:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Time.Calendar (Day, fromGregorianValid)
import Data.List.Split (chunksOf)
import Data.List (unfoldr)
Line 1,174:
mapM_ print $ take 15 palinDates
putStrLn "\nLast 15:"
mapM_ print $ take 15 (drop (n - 15) palinDates)</langsyntaxhighlight>
{{Out}}
<pre>Count of palindromic dates [2021..9999]: 284
Line 1,213:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
Line 1,234:
 
}
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,257:
=={{header|Javascript}}==
===Procedural===
<langsyntaxhighlight lang="javascript">/**
* Adds zeros for 1 digit days/months
* @param date: string
Line 1,300:
}
 
getPalindromeDates();</langsyntaxhighlight>
{{Out}}
<pre>2021-12-02 ​
Line 1,318:
2140-04-12 ​</pre>
===Functional===
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 1,374:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>Count of palindromic dates [2021..9999]: 284
Line 1,414:
=={{header|Julia}}==
Uses the built-in Dates package to check date validity but not for iteration.
<langsyntaxhighlight lang="julia">using Dates
function datepalindromes(nextcount=20)
Line 1,437:
datepalindromes()
</langsyntaxhighlight>{{out}}
<pre>
Date palindromes:
Line 1,463:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">today = DateList[Today];
res = {};
i = 0;
Line 1,474:
i++;
]
Column[DateString[#, {"Year", "-", "Month", "-", "Day"}] & /@ res]</langsyntaxhighlight>
{{out}}
<pre>2021-12-02
Line 1,493:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strformat, times
 
func digits(n: int): seq[int] =
Line 1,513:
echo &"{year}-{monthNum:02}-{dayNum:02}"
inc count
inc year</langsyntaxhighlight>
 
{{out}}
Line 1,536:
===Date calculation===
The more robust solution, using a date/time module.
<langsyntaxhighlight lang="perl">use Time::Piece;
my $d = Time::Piece->strptime("2020-02-02", "%Y-%m-%d");
 
Line 1,544:
print $d->strftime("%Y-%m-%d\n");
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,567:
Given the limited look-ahead required by the task, processing date-like strings can also work.
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,582:
say s/ /-/gr;
last if 15 == ++$cnt;
}</langsyntaxhighlight>
{{out}}
<pre>2021-12-02
Line 1,602:
=={{header|Phix}}==
While parse_date_string() copes with 1/2/4 digit years, it (reasonably enough) throws a wobbly given 5-digit years and beyond.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #004080;">timedate</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,616:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"first 15:\n%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">15</span><span style="color: #0000FF;">],</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"last 15:\n%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">15</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,633:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">NewList pdates.s()
 
Procedure.b IsLeap(y.i)
Line 1,663:
t$="Last 15:" : SelectElement(pdates(),ListSize(pdates())-15)
Next
Input() : End</langsyntaxhighlight>
{{out}}
<pre>Count of palindromic dates [2021..9999]: 284
Line 1,706:
Defined in terms of string reversal:
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Palindrome dates'''
 
from datetime import datetime
Line 1,748:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Count of palindromic dates [2021..9999]:
Line 1,790:
Or, defined in terms of integer operations, rather than string reversals:
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Palindrome dates'''
 
from functools import reduce
Line 1,917:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Count of palindromic dates [2021..9999]:
Line 1,957:
 
=={{header|QB64}}==
<syntaxhighlight lang=" qb64">
<lang QB64>
'Task
' Write a program which calculates and shows the next 15 palindromic dates
Line 2,021:
 
 
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 2,028:
Pretty basic, but good enough. Could start earlier but 3/2/1 digit years require different handling that isn't necessary for this task. (And would be pretty pointless anyway assuming we need 2 digits for the month and two digits for the day. ISO:8601 anybody?)
 
<syntaxhighlight lang="raku" perl6line>my $start = '1000-01-01';
my @palindate = {
Line 2,052:
 
say "\nTotal number of five digit year palindrome dates:\n" ~
+@palindate[$four .. $five]</langsyntaxhighlight>
{{out}}
<pre>2020-02-02
Line 2,083:
The &nbsp; '''date''' &nbsp; BIF &nbsp; (with the &nbsp; '''base''' &nbsp; argument) &nbsp; converts a date to the number of years since the beginning of
<br>the Gregorian calendar, &nbsp; the date is in the &nbsp; '''ISO''' &nbsp; format &nbsp; (International Standards Organization &nbsp; 8601:2004).
<langsyntaxhighlight lang="rexx">/*REXX program finds & displays the next N palindromic dates starting after 2020─02─02*/
/* ───── */
parse arg n from . /*obtain optional argumets from the CL*/
Line 2,095:
say 'a palindromic date: ' aDate /*display a palindromic date ──► term. */
#= # + 1 /*bump the counter of palindromic dates*/
end /*j*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,116:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 2,140:
 
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,163:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'date'
 
palindate = Enumerator.new do |yielder|
Line 2,173:
end
 
puts palindate.take(15)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,194:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// [dependencies]
// chrono = "0.4"
 
Line 2,211:
date = date.succ();
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,235:
=={{header|Sidef}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">var palindates = gather {
for y in (2020 .. 9999) {
var (m, d) = Str(y).flip.last(4).split(2)...
Line 2,251:
]) {
say ("\n#{a}\n", b.slices(5).map { .join(" ") }.join("\n"))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,268:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
func isPalindrome(_ string: String) -> Bool {
Line 2,292:
}
date = calendar.date(byAdding: .day, value: 1, to: date)!
}</langsyntaxhighlight>
 
{{out}}
Line 2,316:
=={{header|UNIX Shell}}==
printf format, rev and date commands are the keys :
<langsyntaxhighlight lang="shell">is_palyndrom_date() { date -d "$1" 1>/dev/null 2>&1 && echo "$1" ; }
 
for _H in {2..9}; do
Line 2,325:
done
done
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,350:
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">
<lang vb>
Sub MainPalindromeDates()
Const FirstDate As String = "2020-02-02"
Line 2,368:
IsDatePalindrome = StrReverse(Left(strDate, 4)) = Right(strDate, 4)
End Function
</syntaxhighlight>
</lang>
 
{{out}}The next 15 palindromic dates in yyyy-mm-dd format after 2020-02-02 are :
Line 2,391:
{{libheader|Wren-fmt}}
{{libheader|Wren-date}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
import "/date" for Date
 
Line 2,409:
count = count + 1
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,432:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func Rev(N);
int N;
[N:= N/10;
Line 2,459:
];
];
]</langsyntaxhighlight>
 
{{out}}
Line 2,482:
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="yabasic">
dateTest$ = ""
total = 0
Line 2,511:
next anno
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,519:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">TD,date,n := Time.Date, T(2020,02,02), 15;
while(n){
ds:=TD.toYMDString(date.xplode()) - "-";
if(ds==ds.reverse()){ n-=1; println(TD.toYMDString(date.xplode())); }
date=TD.addYMD(date,0,0,1);
}</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits