| Bytes | Lang | Time | Link |
|---|---|---|---|
| 083 | Bash on linux | 250102T130218Z | Themooni |
| 136 | C++ clang | 250110T204329Z | jdt |
| 178 | Go + zgo.at/tz | 250110T170230Z | bigyihsu |
| 113 | C# | 250102T191332Z | Ezlandin |
| 085 | Bash | 250106T185755Z | AlexJ |
| 115 | Charcoal | 250106T110637Z | Neil |
| 104 | JavaScript Node.js | 250102T110253Z | Arnauld |
| 159 | Wolfram Language Mathematica | 250102T134039Z | Introduc |
| 085 | R | 250103T093836Z | pajonk |
Bash on linux, 83 bytes
-2 bytes by corvus_192 by using /usr/share/zoneinfo/posix/ + find
-6 bytes by AlexJ who needs rep to comment, by optimizing the path and command further
-2 bytes by me by optimizing the regex from [0:]{8} to 00:00:, which also allows removing the E flag from grep.
cd /*/*/z*x/;find * -exec sh -c "TZ={} date -d$1UTC|grep -q 00:00:" \; -print -quit
Explained:
cd /*/*/z*x/; #in /usr/share/zoneinfo-posix/posix:
find * -exec sh -c #for every file in the tree, execute:
"TZ={} date -d$1UTC #construct a date in that timezone from UTC
|grep -q 00:00:" #and check that it contains "00:00:00"
\; -print -quit #if it does, print the filename and stop finding additional files
}
C++ (clang), 222 220 136 bytes
#import<bits/stdc++.h>
#define f(t,o)for(auto&z:std::chrono::get_tzdb().zones)o=std::format("{:%H:%M}",z.to_local(t))>"00:00"?o:z.name()
Go + zgo.at/tz, 178 bytes
import(."time";T"zgo.at/tz")
func f(t Time)string{for _,z:=range T.Zones{Z:=z.Zone
l,e:=LoadLocation(Z)
if e!=nil{continue}
h,m,s:=t.In(l).Clock()
if h+m+s<1{return Z}}
return""}
- Expects a Go
time.Timeas input. The current year must be set in the input. - Outputs an arbitrary full time zone name within the offset. Returns an empty string as falsey.
The zgo.at/tz package provides a tz.Zones slice which has many time zone names. The solution loops through each zone, constructs a Location, and gets the input time in that Location. If it is midnight there, it returns early with the zone name.
20 bytes are wasted on an error check because there's a single zone name in the library that Go doesn't know about.
Some things I've learned:
- Go has no way in the standard library of accessing names of time zones, hence the use of a 3rd-party library. The other alternative is manually parsing the local time zone files, which would be extremely costly.
- If you do
time.Parse, you need to include the year; otherwise it defaults to year 0000, leading to non-hour/15-minute time zone offsets.
C#, 135 131 130 120 113 bytes
-4 bytes, alias System.TimeZoneInfo to 'Z' instead of returning a string
-1 byte thanks to @jdt, use 'dynamic' instead of 'DateTime' as the type of 't'
-10 bytes thanks to @Themoonisacheese, check Ticks < 1 rather than Ticks == 0, use Linq 'Last' instead of 'Where' and 'First'
-7 bytes from @jdt, use 'ConvertTime' instead of 'ConvertTimeFromUtc'
using Z=System.TimeZoneInfo;Z F(dynamic t)=>Z.GetSystemTimeZones().Last(x=>Z.ConvertTime(t,x).TimeOfDay.Ticks<1);
Explanation
using Z = System.TimeZoneInfo; // Alias TimeZoneInfo as 'Z' to save space
Z F(dynamic t)
{
return Z.GetSystemTimeZones() // Get all time zones
.Last(x => Z.ConvertTime(t, x).TimeOfDay.Ticks < 1); // Find the last time zone where the time is midnight when converted into time zone x
}
Bash, 85 bytes
Don't have enough rep to comment, slightly shorter Bash implementation based on Themoonisacheese. env is unnecessary and the directory can be slightly adjusted:
cd /*/*/z*x/;find * -exec sh "TZ={} date -d$1UTC|grep -qE [0:]{8}" \; -print -quit
Charcoal, 115 bytes
≔↨I⪪S:⁶⁰θF¬﹪θ¹⁵§⪪”}∧ΠX⁴G_↙'Y »oa?⦃ρi▷êC~Q_⁸a^d✂⎚~X·*M⊞◧H⁶⧴5⦃Z⌕‹[M→⁷¤)C⊞r⊘(Y=⟧t⌈G\⁼H¶℅^b²:IK←⊟MV⁻VιT'D⟧↧E↙8Y✂↙” θ¿ⅈT
Try it online! Link is to verbose version of code. Explanation:
≔↨I⪪S:⁶⁰θ
Convert the input into a number of minutes since midnight.
F¬﹪θ¹⁵
If that's a multiple of 15, then...
§⪪”...” θ
... used that to index into a compressed look-up table of time zone abbreviations. The table is 97 entries long but cyclically indexed so for instance 360 minutes is actually found at position 69. (Note that some abbreviations are used for multiple time zones, so for instance CST is Central Standard Time, not China nor Cuba.)
¿ⅈT
If a time zone was found then output its final T.
JavaScript (Node.js), 104 bytes
-1 thanks to @Shaggy
-3 thanks to @l4m2
Expects a string in HH:MM format. Returns either a string or undefined.
h=>Intl.supportedValuesOf(s="timeZone").find(t=>new Date([1,h]).toLocaleTimeString("es",{[s]:t})<"0:01")
Commented
h => // h = input string
Intl.supportedValuesOf( // get the list of supported values for ...
s = "timeZone" // ... "timeZone"
) //
.find(t => // for each time zone t:
new Date([1, h]) // build a date from the string "1,HH:MM"
.toLocaleTimeString( // and turn it into a time string
"es", // using the Spanish locale
// (24-hour format without the leading 0)
{ [s]: t } // and the time zone t
) //
< "0:01" // is it midnight there?
) // end of find()
Alternate version (also 104 bytes)
This one is internally using the Finnish format HH.MM.SS.
h=>Intl.supportedValuesOf(s="timeZone").find(t=>new Date([1,h]).toLocaleTimeString("fi",{[s]:t})<.001+s)
Wolfram Language (Mathematica), 179159 bytes
(u=TimeZone->"UTC";d=DateList@u;d[[-3;;-2]]=#;DateSelect[Cases[TimeZoneConvert[d~DateObject~u,#]&/@EntityList["TimeZone"],_DateObject],#Hour==0&&#Minute==0&])&
This code takes into account day light savings.
It converts a {h,m} input into a UTC DateObject as of today. It then filters a list of all timezones available with a {12,00} hour and minute component.
Note:
This does not work on TIO as it requires an internet connection to download
EntityList["TimeZone"].There is a bug in Mathematica
EntityList["TimeZone"]producing invalid timezones. To work around this I usedCases[...,_DateObject]to filter out the invalid timezones. Code would be 19 bytes shorter without the bug. (I have reported the bug to Wolfram).
Edit:
-20 bytes thanks to inspiration from att to reduce the code to generate the DateObject d
R, 113 85 bytes
\(t,o=OlsonNames())o[format(do.call(c,Map(as.POSIXlt,o,x=Sys.Date())),"%H:%M")==t][1]
Works on machines with R in UTC time.
Abuses the fact that "using c on POSIXlt objects converts them to the current time zone" (from documentation).