Skip to content

Commit 5d03b54

Browse files
SorrienTomFinley
authored andcommitted
Replaced calls to DateTime.Now with DateTime.UtcNow to be locale agnostic (#133)
* Removed calls to DateTime.Now The codebase now uses DateTime.UtcNow, instead of DateTime.Now, to be locale agnostic, except in cases where timezone info is actually needed. Also replaced one starttime measurement with stopwatch.
1 parent 8e60d97 commit 5d03b54

File tree

7 files changed

+16
-15
lines changed

7 files changed

+16
-15
lines changed

src/Microsoft.ML.Core/Data/ProgressReporter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public CalculationInfo(int index, string name, ProgressChannel channel)
359359
Index = index;
360360
Name = name;
361361
PendingCheckpoints = new ConcurrentQueue<KeyValuePair<DateTime, ProgressEntry>>();
362-
StartTime = DateTime.Now;
362+
StartTime = DateTime.UtcNow;
363363
Channel = channel;
364364
}
365365
}
@@ -584,7 +584,7 @@ public ProgressEvent(int index, string name, DateTime startTime, ProgressEntry e
584584
Index = index;
585585
Name = name;
586586
StartTime = startTime;
587-
EventTime = DateTime.Now;
587+
EventTime = DateTime.UtcNow;
588588
Kind = EventKind.Progress;
589589
ProgressEntry = entry;
590590
}
@@ -597,7 +597,7 @@ public ProgressEvent(int index, string name, DateTime startTime, EventKind kind)
597597
Index = index;
598598
Name = name;
599599
StartTime = startTime;
600-
EventTime = DateTime.Now;
600+
EventTime = DateTime.UtcNow;
601601
Kind = kind;
602602
ProgressEntry = null;
603603
}

src/Microsoft.ML.Data/Utilities/TimerScope.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void Dispose()
4646

4747
// REVIEW: This is \n\n is to prevent changes across bunch of baseline files.
4848
// Ideally we should change our comparison method to ignore empty lines.
49-
_ch.Info("{0}\t Time elapsed(s): {1}\n\n", DateTime.Now, elapsedSeconds);
49+
_ch.Info("{0}\t Time elapsed(s): {1}\n\n", DateTime.UtcNow, elapsedSeconds);
5050

5151
using (var pipe = _host.StartPipe<TelemetryMessage>("TelemetryPipe"))
5252
{

src/Microsoft.ML.FastTree/Training/EnsembleCompression/LassoBasedEnsembleCompressor.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Collections.Generic;
7+
using System.Diagnostics;
78

89
namespace Microsoft.ML.Runtime.FastTree.Internal
910
{
@@ -164,7 +165,7 @@ public unsafe void SetTreeScores(int idx, double[] scores)
164165

165166
private LassoFit GetLassoFit(IChannel ch, int maxAllowedFeaturesPerModel)
166167
{
167-
DateTime startTime = DateTime.Now;
168+
Stopwatch stopWatch = Stopwatch.StartNew();
168169

169170
if (maxAllowedFeaturesPerModel < 0)
170171
{
@@ -450,8 +451,8 @@ private LassoFit GetLassoFit(IChannel ch, int maxAllowedFeaturesPerModel)
450451
// First lambda was infinity; fixing it
451452
fit.Lambdas[0] = Math.Exp(2 * Math.Log(fit.Lambdas[1]) - Math.Log(fit.Lambdas[2]));
452453

453-
TimeSpan duration = DateTime.Now - startTime;
454-
ch.Info("Elapsed time for compression: {0}", duration);
454+
stopWatch.Stop();
455+
ch.Info("Elapsed time for compression: {0}", stopWatch.Elapsed);
455456

456457
return fit;
457458
}

src/Microsoft.ML.FastTree/TreeEnsemble/Ensemble.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public string ToTreeEnsembleIni(FeaturesToContentMap fmap,
193193

194194
protected int AppendComments(StringBuilder sb, string trainingParams)
195195
{
196-
sb.AppendFormat("\n\n[Comments]\nC:0=Regression Tree Ensemble\nC:1=Generated using FastTree\nC:2=Created on {0}\n", DateTime.Now);
196+
sb.AppendFormat("\n\n[Comments]\nC:0=Regression Tree Ensemble\nC:1=Generated using FastTree\nC:2=Created on {0}\n", DateTime.UtcNow);
197197

198198
string[] trainingParamsList = trainingParams.Split(new char[] { '\n' });
199199
int i = 0;

src/Microsoft.ML.Maml/MAML.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ internal static int MainCore(TlcEnvironment env, string args, bool alwaysPrintSt
145145
Path.GetTempPath(),
146146
"TLC");
147147
var dumpFilePath = Path.Combine(dumpFileDir,
148-
string.Format(CultureInfo.InvariantCulture, "Error_{0:yyyyMMdd_HHmmss}_{1}.log", DateTime.Now, Guid.NewGuid()));
148+
string.Format(CultureInfo.InvariantCulture, "Error_{0:yyyyMMdd_HHmmss}_{1}.log", DateTime.UtcNow, Guid.NewGuid()));
149149
bool isDumpSaved = false;
150150
try
151151
{

src/Microsoft.ML.ResultProcessor/ResultProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ private static bool ValidateMamlOutput(string filename, string[] rawLines, out L
584584
Results = runResults,
585585
PerFoldResults = foldResults,
586586
Time = 0,
587-
ExecutionDate = DateTime.Now.ToString()
587+
ExecutionDate = DateTime.UtcNow.ToString()
588588
};
589589
}
590590

src/Microsoft.ML.StandardLearners/Standard/Online/OnlineLinear.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ protected virtual void InitCore(IChannel ch, int numFeatures, LinearPredictor pr
206206
Contracts.Assert(Iteration == 0);
207207
Contracts.Assert(Bias == 0);
208208

209-
ch.Trace("{0} Initializing {1} on {2} features", DateTime.Now, Name, numFeatures);
209+
ch.Trace("{0} Initializing {1} on {2} features", DateTime.UtcNow, Name, numFeatures);
210210
NumFeatures = numFeatures;
211211

212212
// We want a dense vector, to prevent memory creation during training
@@ -253,13 +253,13 @@ protected virtual void BeginIteration(IChannel ch)
253253
Iteration++;
254254
NumIterExamples = 0;
255255

256-
ch.Trace("{0} Starting training iteration {1}", DateTime.Now, Iteration);
256+
ch.Trace("{0} Starting training iteration {1}", DateTime.UtcNow, Iteration);
257257
// #if OLD_TRACING // REVIEW: How should this be ported?
258258
if (Iteration % 20 == 0)
259259
{
260260
Console.Write('.');
261261
if (Iteration % 1000 == 0)
262-
Console.WriteLine(" {0} \t{1}", Iteration, DateTime.Now);
262+
Console.WriteLine(" {0} \t{1}", Iteration, DateTime.UtcNow);
263263
}
264264
// #endif
265265
}
@@ -269,7 +269,7 @@ protected virtual void FinishIteration(IChannel ch)
269269
Contracts.Check(NumIterExamples > 0, NoTrainingInstancesMessage);
270270

271271
ch.Trace("{0} Finished training iteration {1}; iterated over {2} examples.",
272-
DateTime.Now, Iteration, NumIterExamples);
272+
DateTime.UtcNow, Iteration, NumIterExamples);
273273

274274
ScaleWeights();
275275
#if OLD_TRACING // REVIEW: How should this be ported?
@@ -378,7 +378,7 @@ protected virtual void ProcessDataInstance(IChannel ch, ref VBuffer<Float> feat,
378378
if (_numIterExamples % 5000000 == 0)
379379
{
380380
Host.StdOut.Write(" ");
381-
Host.StdOut.Write(DateTime.Now);
381+
Host.StdOut.Write(DateTime.UtcNow);
382382
}
383383
Host.StdOut.WriteLine();
384384
}

0 commit comments

Comments
 (0)