summary refs log tree commit diff stats
path: root/nim/ntime.pas
blob: 9135c26c3fb96c9f4035aa8a30fbda177c0c7d3e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
//
//           The Nimrod Compiler
//        (c) Copyright 2008 Andreas Rumpf
//
//    See the file "copying.txt", included in this
//    distribution, for details about the copyright.
//
unit ntime;

interface

{$include 'config.inc'}

uses
{$ifdef win32}
  windows,
{$else}
  sysutils,
  {$ifdef fpc}
    dos,
  {$endif}
{$endif}
  nsystem, strutils;

function DateAndClock: string;
// returns current date and time (format: YYYY-MM-DD Sec:Min:Hour)

function getDateStr: string;
function getClockStr: string;

implementation

{$ifdef mswindows}
function GetDateStr: string;
var
  st: SystemTime;
begin
  Windows.GetLocalTime({$ifdef fpc} @ {$endif} st);
  result := IntToStr(st.wYear, 4) + '-' + IntToStr(st.wMonth, 2) + '-'
    + IntToStr(st.wDay, 2)
end;

procedure GetDate(var Day, Month, Year: int);
var
  st: SystemTime;
begin
  Windows.GetLocalTime({$ifdef fpc} @ {$endif} st);
  Day := st.wDay;
  Month := st.wMonth;
  Year := st.wYear
end;

procedure GetTime(var Hours, Minutes, Seconds, Millisec: int);
var
  st: SystemTime;
begin
  Windows.GetLocalTime({$ifdef fpc} @ {$endif} st);
  Hours := st.wHour;
  Minutes := st.wMinute;
  Seconds := st.wSecond;
  Millisec := st.wMilliseconds
end;
{$else} // not windows
function GetDateStr: string;
var
  wMonth, wYear, wDay: Word;
begin
  SysUtils.DecodeDate(Date, wYear, wMonth, wDay);
  result := IntToStr(wYear, 4) + '-' + IntToStr(wMonth, 2) + '-'
    + IntToStr(wDay, 2)
end;

procedure GetDate(var Day, Month, Year: int);
var
  wMonth, wYear, wDay: Word;
begin
  SysUtils.DecodeDate(Date, wYear, wMonth, wDay);
  Day := wDay;
  Month := wMonth;
  Year := wYear
end;

procedure GetTime(var Hours, Minutes, Seconds, Millisec: int);
var
  wHour, wMin, wSec, wMSec: Word;
begin
  SysUtils.DecodeTime(Time, wHour, wMin, wSec, wMSec);
  Hours := wHour; Minutes := wMin; Seconds := wSec; Millisec := wMSec;
end;
{$endif}

function GetClockStr: string;
var
  Hour, Min, Sec, MSec: int;
begin
  GetTime(Hour, min, sec, msec);
  result := IntToStr(Hour, 2) + ':' + IntToStr(min, 2) + ':' + IntToStr(Sec, 2)
end;

function DateAndClock: string;
begin
  result := GetDateStr() + ' ' + getClockStr()
end;

end.