Python – Dates and Times

Slides:



Advertisements
Similar presentations
QA50BBME Country Code (2 digits) Security Code (2 digits) Bank Identifier Code (4 digits) Bank Account No.(21 digits) IBAN (29 digits)
Advertisements

Modules and Objects Introduction to Computing Science and Programming I.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
Tutorial 11 Working with Operators and Expressions
Oracle9 i Datetime Functions Fresher Learning Program January, 2012.
Chapter 10 Working with Dates & Times Microsoft Excel 2003.
Chapter 14 Internationalization F Processing Date and Time –Locale –Date –TimeZone –Calendar and GregorianCalendar –DateFormat and SimpleDateFormat F Formatting.
Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat.
Sizing Basics  Why Size?  When to size  Sizing issues:  Bits and Bytes  Blocks (aka pages) of Data  Different Data types  Row Size  Table Sizing.
Tutorial 9: Sequential Access Files and Printing1 Tutorial 9 Sequential Access Files and Printing.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Conversion Functions.
16. Python Files I/O Printing to the Screen: The simplest way to produce output is using the print statement where you can pass zero or more expressions,
1 Chapter 3 – Object-Based Programming 2 Initializing Class Objects: Constructors Class constructor is a specical method to initialise instance variables.
Web Services from 10,000 feet Part I Tom Perkins NTPCUG CertSIG XML Web Services.
Tableau Server URL Parameterization and Limits. Background This short set of material covers how Tableau Server Views can be invoked via URLs while passing.
Academic Year 2015 Autumn. MODULE CC2006NI: Data Modelling and Database Systems Academic Year 2015 Autumn.
Working with Date and Time ISYS 475. How PHP processes date and time? Traditional way: – Timestamp Newer and object oriented way: – DateTime class.
IMS 3253: Dates and Times 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Date and Time Data Turning Strings into.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
To play, start slide show and click on circle Access 1 Access 2 Access 3 Access 4 Access Access
CompSci 230 S Programming Techniques
Introduction to Computing Science and Programming I
14 Shipping Time App Using Dates and Timers
Tutorial 14 – Shipping Time Application Using DateTimes and Timers
Creating and Using Objects, Exceptions, Strings
SQL – Python and Databases
Adapters and Converters
Built-in Functions.
Open Source Server Side Scripting MySQL Functions
Chapter 14 Internationalization
Objects and Classes.
Phonegap Bridge – Globalization
Unit 16 – Database Systems
SQL – Data types.
SEEM4570 Tutorial 05: JavaScript as OOP
© Copyright 2016, Fred McClurg All Rights Reserved
SQL – Dates and Times.
Three Minute Timer Two Minute Timer One Minute Timer
Data Types and Field Properties
SQL – Application Persistence Design Patterns
SQL – Parameterized Queries
Topics Introduction to File Input and Output
Phil Tayco Slide version 1.0 Created Oct 9, 2017
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
CIS 136 Building Mobile Apps
Date Functions Farrokh Alemi, Ph.D.
Fundamentals of Data Structures
Data Types and Field Properties
Tutorial 9 Sequential Access Files and Printing
Data Types and Field Properties
Introduction to Programming with Python
Midterm Exam Information
Phonegap Bridge – Globalization
Topics Introduction to File Input and Output
Data Types and Field Properties
Data Types and Field Properties
More on Structs Sometimes we use structs even when the fields are all of the same type. If the fields are different conceptually, that is, the data stands.
Topics Introduction to File Input and Output
The Web Wizard’s Guide To JavaScript
2 Hours Minutes Seconds Insert Text Here.
Strings and Dates in JavaScript
Trainer: Bach Ngoc Toan– TEDU Website:
SQL – Application Persistence Design Patterns
Data Types and Field Properties
Data Types and Field Properties
Temporal Data Part V.
Working with dates and times
2 Hours Minutes Seconds Insert Text Here.
Presentation transcript:

Python – Dates and Times

Working with time in Python The module for handling time is called "datetime" it contains four classes: date - year, month, and day time - hour, minute, second, microsecond, and timezone datetime - holds both date and time (see above) timedelta - holds a 'difference' in time between two of the above Details here: https://docs.python.org/3.4/library/datetime.html

datetime.date Two ways to construct: date(year, month, day) date.today() You can get the data out through attributes: date.year, date.month, date.day d = datetime.date(2000, 12, 15) d.year # 2000

datetime.time One constructor: time(hour, minute, second, microsecond) all are assumed 0 unless specified You can get the data out through attributes: time.hour, time.minute, time.second, time.microsecond t = datetime.time(14, 11, 56) t.minute # 11

datetime.datetime Two constructors: datetime(year, month, day, hour, minute, second, microsecond) datetime.now() gets the current date and time You can get the data out through attributes: Combined attributes of date and time dt = datetime.datetime.now() dt.day # 8 dt.hour # 1

datetime.timedelta One constructor: timedelta(days, hours, minutes, seconds, ...) Defaults to zero if arguments aren't supplied delta = datetime.timedelta(days=2) # Represents 2 days duration You can use it to calculate datetimes: datetime.datetime.now() + delta # yields a date 2 days from now

import datetime now = datetime. datetime. now() birth = datetime import datetime now = datetime.datetime.now() birth = datetime.datetime(1988, 11, 1, 9) #Nov 1st, 1988 9AM my_age = now - birth # age is a timedelta legal_drinking_age = datetime.timedelta(days = (21 * 365)) is_legal_to_drink = my_age > legal_drinking_age # Doesn't take leap days/seconds into account

Converting Python datetime to ISO 8601 All the datetime classes have a "strftime" method This method will output a string formatted according to a pattern dt = datetime.datetime(2015, 3, 4, 12, 35) dt.strftime("%Y-%m-%d %H:%M:%S") # yields '2015-03-04 12:35:00' You can use the datetime class method, "strptime", to convert a string to a datetime object. The method takes a date_string and a format, and it returns a datatime object dt2 = datetime.datetime.strptime('2015-03-04 12:35:00', "%Y-%m-%d %H:%M:%S") assert dt == dt2

Combining Python and SQLite datetimes SQL databases can't hold Python objects, so you must convert them to ISO 8601 and store the resulting TEXT. You can use the 'strftime' method to do this On retrieving a date encoded as TEXT you can convert the string back to a Python datetime class. You can use the 'strptime' method to do this Note, using SQL date/time functions may alter the format (i.e. add microsecond precision or timezone info) so you should refrain from using them.