Bug 3741 – std.date YearFromTime broken or very slow

Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D1 (retired)
Platform
x86
OS
Linux
Creation time
2010-01-25T04:57:00Z
Last change time
2015-11-03T19:31:34Z
Assigned to
nobody
Creator
steve.teale

Comments

Comment #0 by steve.teale — 2010-01-25T04:57:11Z
YearFromTime is used in several places in std.date. If you run: import std.stdio; import std.date; import std.c.linux.linux; extern(C) int clock(); void main() { int t1 = clock(); int y; for (int i = 0; i < 100000; i++) { long t = getUTCtime(); y = YearFromTime(t); } int t2 = clock(); writefln("y = %d", y); writefln("elapsed %d", t2-t1); t1 = clock(); for (int i = 0; i < 100000; i++) { int tt = time(null); tm *ptm = gmtime(&tt); y = ptm.tm_year+1900; } t2 = clock(); writefln("y = %d", y); writefln("elapsed %d", t2-t1); } You will find that YearFromTime takes like 80 times longer. What's more calling localtime gets you all the other stuff too. It looks like it is approximating the year then doing some iterations to check/adjust it, but the iterations are actually doing the whole job.
Comment #1 by andrej.mitrovich — 2011-05-24T21:28:15Z
You can now use std.datetime.SysTime.year, as recommended in the "Migrating from std.date to std.datetime" article that will be up on DPL.org soon. import std.datetime; auto year = (cast(DateTime)Clock.currTime()).year; It takes 128 microseconds on my machine. Hope that's fast enough. Otherwise file a bug report for std.datetime.
Comment #2 by andrej.mitrovich — 2011-05-25T07:22:22Z
My mistake, this is D1. Reopened.
Comment #3 by andrei — 2015-11-03T19:31:34Z
It's unlikely this D1 issue will get worked on, if anyone plans to work on it feel free to reopen.