-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathstandalone.dart
123 lines (108 loc) · 3.57 KB
/
standalone.dart
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2014, the timezone project authors. Please see the AUTHORS
// file for details. All rights reserved. Use of this source code is governed
// by a BSD-style license that can be found in the LICENSE file.
/// TimeZone initialization for standalone environments.
///
/// ```dart
/// import 'package:timezone/standalone.dart';
///
/// initializeTimeZone().then((_) {
/// final detroit = getLocation('America/Detroit');
/// final now = new TZDateTime.now(detroit);
/// });
library timezone.standalone;
import 'dart:async';
import 'dart:io';
import 'package:path/path.dart' as ospath;
import 'package:timezone/timezone.dart';
export 'package:timezone/timezone.dart' show getLocation, setLocalLocation,
TZDateTime, Location, TimeZone, timeZoneDatabase;
final _packagesPrefix = 'packages${ospath.separator}';
final String tzDataDefaultPath = ospath.join('packages',
'timezone', 'data', tzDataDefaultFilename);
/// Load file
Future<List<int>> _loadAsBytes(String path) {
final script = Platform.script;
final scheme = Platform.script.scheme;
if (scheme.startsWith('http')) {
return new HttpClient().getUrl(
new Uri(
scheme: script.scheme,
host: script.host,
port: script.port,
path: path)).then((req) {
return req.close();
}).then((response) {
// join byte buffers
return response.fold(
new BytesBuilder(),
(b, d) => b..add(d)).then((builder) {
return builder.takeBytes();
});
});
} else if (scheme == 'file') {
final packageRoot = Platform.packageRoot;
if (packageRoot.isNotEmpty && path.startsWith(_packagesPrefix)) {
final p = ospath.join(packageRoot, path.substring(_packagesPrefix.length));
return new File(p).readAsBytes();
}
final p = ospath.join(ospath.dirname(ospath.fromUri(script)), path);
return new File(p).readAsBytes();
}
return new Future.error(new UnimplementedError('Unknown script scheme: $scheme'));
}
List<int> _loadAsBytesSync(String path) {
assert(!Platform.script.scheme.startsWith('http'));
final script = Platform.script;
final packageRoot = Platform.packageRoot;
if (packageRoot.isNotEmpty && path.startsWith(_packagesPrefix)) {
final p = ospath.join(packageRoot, path.substring(_packagesPrefix.length));
return new File(p).readAsBytesSync();
}
final p = ospath.join(ospath.dirname(ospath.fromUri(script)), path);
return new File(p).readAsBytesSync();
}
/// Initialize Time Zone database.
///
/// Throws [TimeZoneInitException] when something is worng.
///
/// ```dart
/// import 'package:timezone/standalone.dart';
///
/// initializeTimeZone().then(() {
/// final detroit = getLocation('America/Detroit');
/// final detroitNow = new TZDateTime.now(detroit);
/// });
/// ```
Future initializeTimeZone([String path]) {
if (path == null) {
path = tzDataDefaultPath;
}
return _loadAsBytes(path).then((rawData) {
initializeDatabase(rawData);
}).catchError((e) {
throw new TimeZoneInitException(e.toString());
});
}
/// Initialize Time Zone database (Sync).
///
/// Throws [TimeZoneInitException] when something is worng.
///
/// ```dart
/// import 'package:timezone/standalone.dart';
///
/// initializeTimeZoneSync();
/// final detroit = getLocation('America/Detroit');
/// final detroitNow = new TZDateTime.now(detroit);
/// ```
void initializeTimeZoneSync([String path]) {
if (path == null) {
path = tzDataDefaultPath;
}
try {
final rawData = _loadAsBytesSync(path);
initializeDatabase(rawData);
} catch (e) {
throw new TimeZoneInitException(e.toString());
}
}