@@ -1177,3 +1177,127 @@ Checks if an UID is a valid 23 alphanumeric uid and with a brain:
1177
1177
>>> uid = serv.UID()
1178
1178
>>> api.is_uid(uid, validate = True )
1179
1179
True
1180
+
1181
+ Check if a Date is valid
1182
+ ------------------------
1183
+
1184
+ Do some imports first:
1185
+
1186
+ >>> from datetime import datetime
1187
+ >>> from DateTime import DateTime
1188
+
1189
+ Checks if a DateTime is valid:
1190
+
1191
+ >>> now = DateTime()
1192
+ >>> api.is_date(now)
1193
+ True
1194
+
1195
+ >>> now = datetime.now()
1196
+ >>> api.is_date(now)
1197
+ True
1198
+
1199
+ >>> now = DateTime(now)
1200
+ >>> api.is_date(now)
1201
+ True
1202
+
1203
+ >>> api.is_date(None )
1204
+ False
1205
+
1206
+ >>> api.is_date(' 2018-04-23' )
1207
+ False
1208
+
1209
+ Try conversions to Date
1210
+ -----------------------
1211
+
1212
+ Try to convert to DateTime:
1213
+
1214
+ >>> now = DateTime()
1215
+ >>> zpdt = api.to_date(now)
1216
+ >>> zpdt.ISO8601() == now.ISO8601()
1217
+ True
1218
+
1219
+ >>> now = datetime.now()
1220
+ >>> zpdt = api.to_date(now)
1221
+ >>> pydt = zpdt.asdatetime()
1222
+
1223
+ Note that here, for the comparison between dates, we convert DateTime to python
1224
+ datetime, cause DateTime.strftime() is broken for timezones (always looks at
1225
+ system time zone, ignores the timezone and offset of the DateTime instance
1226
+ itself):
1227
+
1228
+ >>> pydt.strftime(' %Y-%m-%d T%H:%M:%S' ) == now.strftime(' %Y-%m-%d T%H:%M:%S' )
1229
+ True
1230
+
1231
+ Try the same, but with utcnow() instead:
1232
+
1233
+ >>> now = datetime.utcnow()
1234
+ >>> zpdt = api.to_date(now)
1235
+ >>> pydt = zpdt.asdatetime()
1236
+ >>> pydt.strftime(' %Y-%m-%d T%H:%M:%S' ) == now.strftime(' %Y-%m-%d T%H:%M:%S' )
1237
+ True
1238
+
1239
+ Now we convert just a string formatted date:
1240
+
1241
+ >>> strd = " 2018-12-01 17:50:34"
1242
+ >>> zpdt = api.to_date(strd)
1243
+ >>> zpdt.ISO8601()
1244
+ '2018-12-01T17:50:34'
1245
+
1246
+ Now we convert just a string formatted date, but with timezone:
1247
+
1248
+ >>> strd = " 2018-12-01 17:50:34 GMT+1"
1249
+ >>> zpdt = api.to_date(strd)
1250
+ >>> zpdt.ISO8601()
1251
+ '2018-12-01T17:50:34+01:00'
1252
+
1253
+ We also check a bad date here (note the month is 13):
1254
+
1255
+ >>> strd = " 2018-13-01 17:50:34"
1256
+ >>> zpdt = api.to_date(strd)
1257
+ >>> api.is_date(zpdt)
1258
+ False
1259
+
1260
+ And with European format:
1261
+
1262
+ >>> strd = " 01.12.2018 17:50:34"
1263
+ >>> zpdt = api.to_date(strd)
1264
+ >>> zpdt.ISO8601()
1265
+ '2018-12-01T17:50:34'
1266
+
1267
+ >>> zpdt = api.to_date(None )
1268
+ >>> zpdt is None
1269
+ True
1270
+
1271
+ Use a string formatted date as fallback:
1272
+
1273
+ >>> strd = " 2018-13-01 17:50:34"
1274
+ >>> default_date = " 2018-01-01 19:30:30"
1275
+ >>> zpdt = api.to_date(strd, default_date)
1276
+ >>> zpdt.ISO8601()
1277
+ '2018-01-01T19:30:30'
1278
+
1279
+ Use a DateTime object as fallback:
1280
+
1281
+ >>> strd = " 2018-13-01 17:50:34"
1282
+ >>> default_date = " 2018-01-01 19:30:30"
1283
+ >>> default_date = api.to_date(default_date)
1284
+ >>> zpdt = api.to_date(strd, default_date)
1285
+ >>> zpdt.ISO8601() == default_date.ISO8601()
1286
+ True
1287
+
1288
+ Use a datetime object as fallback:
1289
+
1290
+ >>> strd = " 2018-13-01 17:50:34"
1291
+ >>> default_date = datetime.now()
1292
+ >>> zpdt = api.to_date(strd, default_date)
1293
+ >>> dzpdt = api.to_date(default_date)
1294
+ >>> zpdt.ISO8601() == dzpdt.ISO8601()
1295
+ True
1296
+
1297
+ Use a non-conversionable value as fallback:
1298
+
1299
+ >>> strd = " 2018-13-01 17:50:34"
1300
+ >>> default_date = " something wrong here"
1301
+ >>> zpdt = api.to_date(strd, default_date)
1302
+ >>> zpdt is None
1303
+ True
0 commit comments