[m-rev.] for review: implement io__file_modification_time_2 in C#
Peter Ross
pro at missioncriticalit.com
Sat Nov 1 09:14:07 AEDT 2003
diff -u library/time.m library/time.m
--- library/time.m
+++ library/time.m
@@ -176,7 +176,7 @@
:- implementation.
-:- import_module int, exception.
+:- import_module int, exception, list, require, string.
:- pragma foreign_decl("C",
"
@@ -211,6 +211,8 @@
:- type time_t_rep ---> time_t_rep(c_pointer).
:- pragma foreign_type("C", time_t_rep, "time_t")
where comparison is compare_time_t_reps.
+
+ % The System.DateTime will hold the value in UTC.
:- pragma foreign_type(il, time_t_rep, "valuetype [mscorlib]System.DateTime")
where comparison is compare_time_t_reps.
@@ -241,12 +243,14 @@
Ret = (MR_Integer) clock();
MR_update_io(IO0, IO);
}").
+/* XXX need to add System.dll to the references list.
:- pragma foreign_proc("C#", time__c_clock(Ret::out, _IO0::di, _IO::uo),
[will_not_call_mercury, promise_pure, tabled_for_io],
"{
// XXX Ticks is long in .NET!
- Ret = (int) System.DateTime.Now.Ticks;
+ Ret = (int) System.Diagnostics.Process.GetCurrentProcess.UserProcessorTime.Ticks;
}").
+*/
%-----------------------------------------------------------------------------%
@@ -362,7 +366,7 @@
time__c_time(Ret::out, _IO0::di, _IO::uo),
[will_not_call_mercury, promise_pure, tabled_for_io],
"{
- Ret = System.DateTime.Now;
+ Ret = System.DateTime.UtcNow;
}").
:- pred time__time_t_is_invalid(time_t_rep).
@@ -405,7 +409,7 @@
"{
System.TimeSpan span;
span = T1 - T0;
- Diff = span.Ticks / System.TimeSpan.TicksPerSecond;
+ Diff = span.TotalSeconds;
}").
%-----------------------------------------------------------------------------%
@@ -461,8 +465,11 @@
WD = (int) t.DayOfWeek;
MD = t.Day;
YD = t.DayOfYear - 1;
- // XXX Don't know how to determine if in DST.
- N = -1;
+ if (System.TimeZone.CurrentTimeZone.IsDaylightSavingTime(t)) {
+ N = 1;
+ } else {
+ N = 0;
+ }
}").
@@ -506,7 +513,7 @@
Min::out, Sec::out, YD::out, WD::out, N::out),
[will_not_call_mercury, promise_pure],
"{
- System.DateTime t = Time.ToUniversalTime();
+ System.DateTime t = Time;
// we don't handle leap seconds
Sec = t.Second;
@@ -517,8 +524,8 @@
WD = (int) t.DayOfWeek;
MD = t.Day;
YD = t.DayOfYear - 1;
- // XXX Don't know how to determine if in DST.
- N = -1;
+ // UTC time can never have daylight savings.
+ N = 0;
}").
:- func int_to_maybe_dst(int) = maybe(dst).
@@ -569,6 +576,11 @@
[will_not_call_mercury, promise_pure],
"{
// We don't use YD, WD and N.
+ // XXX Ignoring N the daylight savings time indicator is bad
+ // On the day when you switch back to standard time from daylight
+ // savings time, the time '2:30am' occurs twice, once during daylight
+ // savings time (N = 1), and then again an hour later, during standard
+ // time (N = 0).
Time = new System.DateTime(Yr + 1900, Mnt + 1, MD, Hrs, Min, Sec);
}").
@@ -588,78 +600,60 @@
%:- func time__asctime(tm) = string.
time__asctime(TM) = Str :-
- TM = tm(Yr, Mnt, MD, Hrs, Min, Sec, YD, WD, DST),
- time__c_asctime(Yr, Mnt, MD, Hrs, Min, Sec, YD, WD,
- maybe_dst_to_int(DST), Str).
-
-:- pred time__c_asctime(int, int, int, int, int, int, int, int, int, string).
-:- mode time__c_asctime(in, in, in, in, in, in, in, in, in, out) is det.
-
-:- pragma foreign_proc("C",
- time__c_asctime(Yr::in, Mnt::in, MD::in, Hrs::in, Min::in, Sec::in,
- YD::in, WD::in, N::in, Str::out),
- [will_not_call_mercury, promise_pure],
-"{
- struct tm t;
- char* s;
-
- t.tm_sec = Sec;
- t.tm_min = Min;
- t.tm_hour = Hrs;
- t.tm_mon = Mnt;
- t.tm_year = Yr;
- t.tm_wday = WD;
- t.tm_mday = MD;
- t.tm_yday = YD;
- t.tm_isdst = N;
-
- s = asctime(&t);
-
- MR_make_aligned_string_copy(Str, s);
-}").
-:- pragma foreign_proc("C#",
- time__c_asctime(Yr::in, Mnt::in, MD::in, Hrs::in, Min::in, Sec::in,
- _YD::in, _WD::in, _N::in, Str::out),
- [will_not_call_mercury, promise_pure],
-"{
- // We don't use YD, WD and N.
- System.DateTime Time = new System.DateTime(Yr + 1900, Mnt + 1, MD,
- Hrs, Min, Sec);
+ TM = tm(Yr, Mnt, MD, Hrs, Min, Sec, _YD, WD, _DST),
+ Str = string__format("%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
+ [s(wday_name(WD)), s(mon_name(Mnt)), i(MD), i(Hrs),
+ i(Min), i(Sec), i(1900 + Yr)]).
+
+:- func wday_name(int) = string.
+
+wday_name(N) = Name :-
+ ( wday_name(N, Name0) ->
+ Name = Name0
+ ;
+ error("time: wday_name")
+ ).
+
+:- pred wday_name(int::in, string::out) is semidet.
+
+wday_name(0, "Mon").
+wday_name(1, "Tue").
+wday_name(2, "Wed").
+wday_name(3, "Thu").
+wday_name(4, "Fri").
+wday_name(5, "Sat").
+wday_name(6, "Sun").
+
+:- func mon_name(int) = string.
+
+mon_name(N) = Name :-
+ ( mon_name(N, Name0) ->
+ Name = Name0
+ ;
+ error("time: mon_name")
+ ).
+
+:- pred mon_name(int::in, string::out) is semidet.
+
+mon_name(0, "Jan").
+mon_name(1, "Feb").
+mon_name(2, "Mar").
+mon_name(3, "Apr").
+mon_name(4, "May").
+mon_name(5, "Jun").
+mon_name(6, "Jul").
+mon_name(7, "Aug").
+mon_name(8, "Sep").
+mon_name(9, "Oct").
+mon_name(10, "Nov").
+mon_name(11, "Dec").
- // XXX this may need to be formatted differently.
- Str = Time.ToString();
-}").
%-----------------------------------------------------------------------------%
%:- func time__ctime(time_t) = string.
-time__ctime(time_t(Time)) = Str :-
- time__c_ctime(Time, Str).
-
-:- pred time__c_ctime(time_t_rep, string).
-:- mode time__c_ctime(in, out) is det.
-
-:- pragma foreign_proc("C",
- time__c_ctime(Time::in, Str::out),
- [will_not_call_mercury, promise_pure],
-"{
- char *s;
- time_t t;
-
- t = Time;
-
- s = ctime(&t);
-
- MR_make_aligned_string_copy(Str, s);
-}").
-:- pragma foreign_proc("C",
- time__c_ctime(Time::in, Str::out),
- [will_not_call_mercury, promise_pure],
-"{
- // XXX this may need to be formatted differently.
- Str = Time.ToString();
-}").
+time__ctime(Time) = asctime(localtime(Time)).
%-----------------------------------------------------------------------------%
--------------------------------------------------------------------------
mercury-reviews mailing list
post: mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the reviews
mailing list