|
- Author:
- larsivi (IP: 84.48.50.144)
- Timestamp:
- 05/22/08 16:16:14 (16 years ago)
- Comment:
Copied from trunk
Legend:
- Unmodified
- Added
- Removed
- Modified
-
LocaltimeExample
v7 |
v8 |
|
1 | 1 | {{{ |
---|
2 | 2 | #!d |
---|
| 3 | /******************************************************************************* |
---|
| 4 | |
---|
| 5 | localtime.d |
---|
| 6 | |
---|
| 7 | *******************************************************************************/ |
---|
| 8 | |
---|
3 | 9 | private import tango.io.Stdout; |
---|
4 | 10 | |
---|
8 | 14 | |
---|
9 | 15 | Example code to format a local time in the following format: |
---|
10 | | "Wed Dec 31 16:00:00 GMT-0800 1969" |
---|
| 16 | "Wed Dec 31 16:00:00 GMT-0800 1969". The day and month names |
---|
| 17 | would typically be extracted from a locale instance, but we |
---|
| 18 | convert them locally here for the sake of simplicity |
---|
11 | 19 | |
---|
12 | 20 | ******************************************************************************/ |
---|
14 | 22 | void main () |
---|
15 | 23 | { |
---|
16 | | // set current local time |
---|
17 | | auto date = WallClock.toDate; |
---|
| 24 | /// list of day names |
---|
| 25 | static char[][] days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; |
---|
| 26 | |
---|
| 27 | /// list of month names |
---|
| 28 | static char[][] months = |
---|
| 29 | [ |
---|
| 30 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
---|
| 31 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", |
---|
| 32 | ]; |
---|
| 33 | |
---|
| 34 | // retreive local time |
---|
| 35 | auto dt = WallClock.toDate; |
---|
18 | 36 | |
---|
19 | 37 | // get GMT difference in minutes |
---|
20 | | auto tz = WallClock.zone / Time.TicksPerMinute; |
---|
| 38 | auto tz = cast(int) WallClock.zone.minutes; |
---|
21 | 39 | char sign = '+'; |
---|
22 | 40 | if (tz < 0) |
---|
25 | 43 | // format date |
---|
26 | 44 | Stdout.formatln ("{}, {} {:d2} {:d2}:{:d2}:{:d2} GMT{}{:d2}:{:d2} {}", |
---|
27 | | date.asDay, |
---|
28 | | date.asMonth, |
---|
29 | | date.day, |
---|
30 | | date.hour, |
---|
31 | | date.min, |
---|
32 | | date.sec, |
---|
| 45 | days[dt.date.dow], |
---|
| 46 | months[dt.date.month-1], |
---|
| 47 | dt.date.day, |
---|
| 48 | dt.time.hours, |
---|
| 49 | dt.time.minutes, |
---|
| 50 | dt.time.seconds, |
---|
33 | 51 | sign, |
---|
34 | 52 | tz / 60, |
---|
35 | 53 | tz % 60, |
---|
36 | | date.year |
---|
| 54 | dt.date.year |
---|
37 | 55 | ); |
---|
38 | 56 | } |
|
|
|
|
|
Copyright © 2006-2024 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic